Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localization in C# Application

Im creating a C# Application based on WinForms / KryptonForms, and as the application is halfway in development i thought to myself I best sort the localization out.

As a born and bred PHP Programmer (and I know C# is a whole new level), I would create a class to detect the language and auto assign the language pack to the application. and then use the language objects to access the values.

I'm wondering if I can get some examples on the easiest / Best methods of doing this.

I personally would like something along the lines of the Application Settings

Where usually I would do MyApplication.Properties.Settings.Default.SomeKey I was hoping of a MyApplication.Languages.Current.ApplicationTitle and MyApplication.Languages.en.ApplicationTitle for example.

Also by only loading a single resource per language file to improve speed would be beneficial as well.

So the language loads in English, Spanish user is promoted that this application is in English, would he like to change it to the Spanish language, he clicks YES, the settings get updated and application restarts and loads the single Spanish language pack.

What are your thoughts on this?


Edit:

The application is based on the XMPP Protocols and uses agsXMPP Libraries. From my understanding each user that sends me their presence should also send the language their system is on.

So basically if there's any way to "grasp" the fact of storing a single word and using the __("some string") in my application would be possible, but for the mean time I'm just looking at the GUI text.

like image 823
RobertPitt Avatar asked Jul 14 '10 08:07

RobertPitt


3 Answers

The way we do localization is:

  • Set the "Localizable"-Property of a WinForm to true (which will generate a new resource file for that Form, holding e.g. the text of labels, buttons, but also z-order etc)
  • Create a FormName.de.resx file ("de" because we're german) and then store the strings that will need to be localized in there (access to this resource-file works via the ResourceManager-class)
  • As for non-WinForms code that needs to be localized, we simply create separate resource files

Upon compiling your app, a AppName.resources.dll is created. This dll holds all the resources of your application and can then be used with tools such as Visual Localize to translate the strings into another language like english or spanish etc.

like image 70
Christian Avatar answered Sep 17 '22 18:09

Christian


You should use resources.

You can change language dynamically:

1) in console app:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
double a = 100.12;
Console.WriteLine("{0} - {1}", Thread.CurrentThread.CurrentCulture, a);

Thread.CurrentThread.CurrentCulture = new CultureInfo("ru-RU");
Console.WriteLine("{0} - {1}", Thread.CurrentThread.CurrentCulture, a);
Console.ReadLine();

2) in winforms app we can reopen form to apply localization resources (use Localizable and Language properties in the form designer to auto generate resources for every language):

if (Thread.CurrentThread.CurrentCulture.Name == "en-US")
{
    Thread.CurrentThread.CurrentCulture = new CultureInfo("ru-RU");
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("ru-RU");
}
else
{
    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
}

double a = 100.12;
textBox1.Text = a.ToString(Thread.CurrentThread.CurrentCulture);

Form1 f = new Form1();
f.ShowDialog();

3) using many threads with own localization

private void button1_Click(object sender, EventArgs e)
{
    // for example main thread language is en-US        

    Thread t = new Thread(StartForm);
    t.CurrentUICulture = new CultureInfo("ru-RU");
    t.Start();
    //t.Join();
}

public static void StartForm()
{
    Form1 f = new Form1();
    f.ShowDialog();
}
like image 38
garik Avatar answered Sep 20 '22 18:09

garik


We use Resources.MyResources.SomeString which gets automatically translated in the right language. Resource files are named MyResources.de-DE.resx, MyResources.nl-BE.resx etc. Same method as the Project Properties basically.

Sample translation code:

public void TranslateForm()
{
        menuItem11.Text = Resources.MyResources.Nieuw;
        menuItem12.Text = Resources.MyResources.Verwijderen;
        menuItem13.Text = Resources.MyResources.Kopieren;
}

Or you could do it manually like:

menuItem11.Text = Translator.GetString("New", "de-DE" );

...

    public static string GetString( string varname )
    {
        string resourceName = typeof(Vertaling).Namespace + ".Resources.MyResources";
        ResourceManager rm = new ResourceManager(resourceName, Assembly.GetExecutingAssembly());
        return rm.GetString(varname);
    }

    public static string GetString( string varname, string taalCode )
    {
        string resourceName = typeof(Vertaling).Namespace + ".Resources.MyResources";
        ResourceManager rm = new ResourceManager(resourceName, Assembly.GetExecutingAssembly());
        return rm.GetString(varname, new CultureInfo(taalCode) );
    }
like image 24
Run CMD Avatar answered Sep 18 '22 18:09

Run CMD