.NET - localization without pain. (N) gettext + poedit

  • Tutorial

Creating a new project, I had to use either * .resx for WinForms, or I2Localization for Unity, or other solutions for localizing applications. All these solutions are similar in that you have to invent a key-localization, insert it into the code and into the dictionary. At first, everything is fine, but over time, this process begins to annoy. However, looking at the key in the code is not always clear what I mean.

About the situation when you need to add localization to a large project where it did not exist at all, I will not even say how difficult it is.

I do not know why, but it turns out that there is a ready-made solution such as gnu / gettext for a long time. Asking their friends and colleagues (those who work with .NET), most have not even heard of it. Therefore, I decided to share with this handy tool.

The principle is simple. You write the code with strings in English, run a utility that scans the source and provides you with the ability to translate. No need to invent any keys. The text in English is the key.

Let's get started

1) Install the NGettext package via Nu-get:
PM> Install-Package NGettext

NGettext is a GNU / Gettext cross-platform implementation for .NET.

2) Add an additional file to your project that simplifies the syntax a bit:

Also add a directory to the project where the translations will be stored:
MyProj \ Loc \ ru-RU \ LC_Messages

In my case, this picture is obtained:

3) Add paths to the T.cs file:

    var localesDir = Path.Combine(Directory.GetCurrentDirectory(), "Loc");
    _Catalog = new Catalog("Test", localesDir, new CultureInfo("ru-RU"));

Simplified. For example, only Russian. (It is possible to read dictionaries from the assembly itself)

4) We write our code using localization. Instead of “text” we write T ._ (“text”)

        publicstaticvoidMain(string[] args)
            Console.WriteLine(T._("Hello, World!"));

5) Now we need to translate all our text. Download PoEdit . Create a translation file:
File → New → folder LC_MESSAGES → Test.po

Specify the folder in which our sources are located. Their program will scan:

It is also necessary to specify a keyword that poEdit will search for translation:

Add the translation we need and save.

Add translation files to the project. Make them Copy always:
(It is possible to embed them in the assembly itself)

Done. Run:

Setup is ready. Then everything is simple. Write code - edit translation

You can also find ready-made libraries for localizing interfaces:

Additional information about using NGettext
Information about GNU / Gettext

Also popular now: