Click here to Skip to main content
14,639,337 members
Articles » Platforms, Frameworks & Libraries » .NET Framework » Utilities
Article
Posted 18 Nov 2015

Stats

14K views
12 bookmarked

.NET Library Localizer

Rate this:
4.81 (6 votes)
Please Sign up or sign in to vote.
4.81 (6 votes)
20 Nov 2015CPOL
Localization of .NET assemblies in the cloud and translation

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.

  1. Overview
  2. Setup a project
  3. Load your DLL in the Cloud
    1. Using code
    2. Using GUI tool
    3. 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

  1. 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#.
  2. Delete Class1.vb or Class1.cs and create a new class, name it Test.vb or Test.cs
  3. Open project properties and then open the Assembly Information editor; specify the Neutral Language (for this walkthrough) as English (United States)
  4. PM> Install-Package Newtonsoft.Json
  5. Add the helper class in your project:
    1. If you are using VB.NET, download this file (Helper.vb.zip)
    2. if you are using C#, download this file (Helper.cs.zip)
  6. 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

  1. 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#.
  2. Now, you need to add 3 references before you connect to the API:
    1. First, download NLL.zip and add reference to NLL.dll file
    2. PM> Install-Package Newtonsoft.Json
    3. PM> Install-Package DotNetZip
  3. 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 strings 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).

Image 1

Options for CSharp project (The default namespace is different from root namespace in Visual Basic, leave it blank).

Image 2

Testing the Result

Now that we have the result DLL from the API, we shall test it.

  1. 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.
  2. 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();
    
  3. 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. 4. Run the project and see it in action...

    Image 3

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.

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


Comments and Discussions

 
QuestionHave you consider to post this as a tip? Pin
Nelek20-Nov-15 3:27
protectorNelek20-Nov-15 3:27 
Questioncan you please add point of interest Pin
asimonassi19-Nov-15 19:39
Memberasimonassi19-Nov-15 19:39 
AnswerRe: can you please add point of interest Pin
Mohamed Amine SEBBANE20-Nov-15 1:35
MemberMohamed Amine SEBBANE20-Nov-15 1:35 
GeneralMy vote of 4 Pin
Abhishek Kumar Goswami19-Nov-15 2:04
professionalAbhishek Kumar Goswami19-Nov-15 2:04 
Good Work
GeneralRe: My vote of 4 Pin
Mohamed Amine SEBBANE19-Nov-15 7:01
MemberMohamed Amine SEBBANE19-Nov-15 7:01 
GeneralMy vote of 5 Pin
Santhakumar M18-Nov-15 7:50
professionalSanthakumar M18-Nov-15 7:50 
GeneralRe: My vote of 5 Pin
Mohamed Amine SEBBANE18-Nov-15 9:53
MemberMohamed Amine SEBBANE18-Nov-15 9:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.