Sinatr
Sinatr

Reputation: 21979

Dynamic localization of messages

I have made a simple localization of messages. All messages are stored in the static class Lng

public static partial class Lng
{
    public static readonly string AppName = "My application";
    public static class Category1
    {
        public static readonly string ConfirmDelete = "Are you sure want to delete?";
    }
}

In code usage is as simple as referencing fields

MessageBox.Show(Lng.Category1.ConfirmDelete, ...

Then there is a manager, which does following:

It's irrelevant of how language files looks likes, but here is a reflection part

TranslateLng("Lng.", typeof(Lng));
...

private static void TranslateLng(string parent, Type type)
{
    foreach (Type nested in type.GetNestedTypes())
    {
        string child = string.Format("{0}{1}.", parent, nested.Name);
        TranslateLng(child, nested);
        foreach (var field in nested.GetFields())
        {
            string key = child + field.Name;
            DefaultAdd(key, (string)field.GetValue(null)); // store value in default language dictionary (if not created yet)
            field.SetValue(null, GetValue(key)); // get value for currently selected language
        }
    }

This system has one problem: all messages are defined in one class, which required manual management (deleting and updating messages when updating code which uses them).

And I was thinking to change manager to register strings dynamically and simplify usage to something like

MessageBox.Show(Lng.Text("Are you sure want to delete?"), ...

So that text is defined right where it used, duplicated text can be handled by manager and so on.

There are however 2 problems:

Upvotes: 0

Views: 655

Answers (1)

NeddySpaghetti
NeddySpaghetti

Reputation: 13495

I recently worked on a project with internationaliztion and we used Resources in con junction with the Sisulizer program with great success. Having the resources solves your key problem as you manually enter the key when you extract the resources. You also get great support from Resharper which makes the whole process a breeze.

Sisulizer is then used to extract resources as well as strings hard-coded in our Win Forms and WPF classes. It can export a CSV which you can give your translators and it also supports pseudo translation, which makes testing such apps very easy as well.

Upvotes: 1

Related Questions