What Is It?
.NET Library Localizer is a cloud API for processing compiled .NET libraries made with C# or VB.NET.
What Does It Do?
It facilitates translation of .NET assemblies (DLLs) made with framework versions from 4.0 up to 4.6.
- Overview
- Setup a project
- Load your DLL in the Cloud
- Using code
- Using GUI tool
- Testing the result
Overview
This article is intended for developers who make .NET software using Visual Studio with nuget installed.
An Internet connection is required in order to connect to the API.
The minimum framework version for using the API is 4.0.
Setup a Project
- Open Visual Studio and create a new Class Library project, name it
TestLibrary.VB
if you are using VB.NET or TestLibrary.CSharp
for C#. - Delete Class1.vb or Class1.cs and create a new class, name it Test.vb or Test.cs
- Open project properties and then open the Assembly Information editor; specify the Neutral Language (for this walkthrough) as English (United States)
-
PM> Install-Package Newtonsoft.Json
- Add the helper class in your project:
- If you are using VB.NET, download this file (Helper.vb.zip)
- if you are using C#, download this file (Helper.cs.zip)
- Open Test.vb or Test.cs and add a function that returns a
string
.
VB.NET Example
Function GetString() As String
Return "Hello World!"
End Function
CSharp Example
public string GetString() { return "Hello World!"; }
The result so far...
Load your DLL in the Cloud
First, you need to compile your DLL project, then copy either TestLibrary.VB.dll or TestLibrary.CSharp.dll to a new folder in addition to Newtonsoft.Json.dll.
Using Code
- Open Visual Studio and create a new Console Application project, name it
NLL.Com.VB
if you are using VB.NET or NLL.Com.CSharp
if you are using C#. - Now, you need to add 3 references before you connect to the API:
- First, download NLL.zip and add reference to NLL.dll file
-
PM> Install-Package Newtonsoft.Json
-
PM> Install-Package DotNetZip
- Open Module1.vb or Program.cs and edit
Sub Main()
or static void Main(string[] args);
here is the code to insert...
For VB.NET
(Download Project)
Dim NLLClient As New NLL.Client
NLLClient.Load("<Path to TestLibrary.VB.dll>", "TestLibrary.VB", "en-US")
File.WriteAllBytes("TestLibrary.VB.dll", NLLClient.DLLFile)
File.WriteAllBytes(NLLClient.LanguageFileName, NLLClient.LanguageFile)
Console.WriteLine(String.Format("OK! (Elapsed: {0}...)", NLLClient.Elapsed.ToString))
Console.ReadLine()
For CSharp
(Download Project)
NLL.Client NLLClient = new NLL.Client();
NLLClient.Load("<Path to TestLibrary.CSharp.dll>", string.Empty, "en-US");
File.WriteAllBytes("TestLibrary.CSharp.dll", NLLClient.DLLFile);
File.WriteAllBytes(NLLClient.LanguageFileName, NLLClient.LanguageFile);
Console.WriteLine(string.Format("OK! (Elapsed: {0}...)", NLLClient.Elapsed.ToString()));
Console.ReadLine();
Run the application, and wait for the result.
The result would be two files saved in the application folder, the modified library "TestLibrary.VB.dll" or "TestLibrary.CSharp.dll" with another file named "TestLibrary.VB en-US" or "TestLibrary.CSharp en-US".
The second file contains simply JSON data representing the string
s in the input DLL. Make two copies of this file in the same folder, name the first "TestLibrary.VB fr-FR" or "TestLibrary.CSharp fr-FR" and the second "TestLibrary.VB es-ES" or "TestLibrary.CSharp es-ES".
Now use any text editor (like Notepad) to open the two files you created so far. Look for "Hello World!" and change it to "Bonjour le monde!" inside the file named "TestLibrary.VB fr-FR" or "TestLibrary.CSharp fr-FR" and to "Hola Mundo!" in "TestLibrary.VB es-ES" or "TestLibrary.CSharp es-ES".
That's it! You have now a fully localizable DLL with translations.
It's not over yet, let's go to the test!
Using GUI Tool
Download the GUI Tool.
Make sure to enter all the values properly for each project language.
Example for VB.NET project (the root namespace is required if not left blank in the VB project, otherwise the server returns an error).
Options for CSharp project (The default namespace is different from root namespace in Visual Basic, leave it blank).
Testing the Result
Now that we have the result DLL from the API, we shall test it.
- Add a reference to TestLibrary.VB.dll or TestLibrary.CSharp.dll back from the server to a new Console Application project called
ConsoleDemo.VB
or ConsoleDemo.CSharp
. - Open Module1.vb or Program.cs, and update
Sub Main()
or static void Main(string[] args)
with the following code:
For VB.NET
Dim Test As New TestLibrary.VB.Test
Dim CultureName As String = "en-US"
Console.WriteLine(CultureName)
TestLibrary.VB.HandCode.Helper.ChangeLanguage(CultureName)
Console.WriteLine(Test.GetString)
Console.WriteLine()
CultureName = "fr-FR"
Console.WriteLine(CultureName)
TestLibrary.VB.HandCode.Helper.ChangeLanguage(CultureName)
Console.WriteLine(Test.GetString)
Console.WriteLine()
CultureName = "es-ES"
Console.WriteLine(CultureName)
TestLibrary.VB.HandCode.Helper.ChangeLanguage(CultureName)
Console.WriteLine(Test.GetString)
Console.ReadLine()
For CSharp
TestLibrary.CSharp.Test Test = new TestLibrary.CSharp.Test();
string CultureName = "en-US";
Console.WriteLine(CultureName);
HandCode.Helper.ChangeLanguage(CultureName);
Console.WriteLine(Test.GetString());
Console.WriteLine();
CultureName = "fr-FR";
Console.WriteLine(CultureName);
HandCode.Helper.ChangeLanguage(CultureName);
Console.WriteLine(Test.GetString());
Console.WriteLine();
CultureName = "es-ES";
Console.WriteLine(CultureName);
HandCode.Helper.ChangeLanguage(CultureName);
Console.WriteLine(Test.GetString());
Console.ReadLine();
- Inside the project, add the three translation files ("TestLibrary.VB en-US", "TestLibrary.VB fr-FR" and "TestLibrary.VB es-ES" or "TestLibrary.CSharp en-US", "TestLibrary.CSharp fr-FR" and "TestLibrary.CSharp es-ES") to the root of your project, select all three and switch to file properties, select Copy Always in Copy to Output Directory.
- 4. Run the project and see it in action...
Points of Interest (as suggested by "asimonassi")
I did not intend to create something that will replace resource files in Visual Studio, I still use them myself for images and files, but this technique might be a useful complement or alternative in some cases; for example:
- If you want to allow other developers to translate the strings your library without the source code.
- Another advantage is that you don't have to rebuild the project every time you make changes to any of the translation files.
- The library continues to work even after obfuscation; you can make additional changes to the translation files after obfuscation and it still going to work.
Do you have any other ideas on how it might be useful? Please tell me via a comment below.
Thank you.