Marc
Marc

Reputation: 993

Set CurrentUICulture so it uses Resources.resx

I'm working on localization in a small test WPF app and want to have English and French. I created Resources.en-CA.resx and Resources.fr-CA.resx.

When my test app starts, I have a startup window with a dropdown to select English or French. When the user clicks OK, a method is called to switch the culture based on the selected language, and then it loads another window which will show some resource strings in the selected language.

switch (selectedLanguage)
{
    case "English":
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-CA");
        break;

    case "French":
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-CA");
        break;
}

The user can close then close this window and select a different language from the startup window and click OK to show the window again but with the new language they selected.

This is working but I also have a Resources.resx which is basically just a duplicate of Resources.en-CA.resx. I want to know if I can get rid of Resources.en-CA.resx, and if so, what do I set CurrentUICulture to so that it will use the values from Resources.resx when English is selected.

switch (selectedLanguage)
{
    case "English":
        //What would I do here?
        break;

    case "French":
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-CA");
        break;
}

I'm accessing the resources strings in code using

Properties.Resources.SomeLocalizedString

Upvotes: 2

Views: 1426

Answers (1)

Racil Hilan
Racil Hilan

Reputation: 25361

Yes, you can get rid of the Resources.en-CA.resx file if you like. You don't need to change anything as your current code will continue to work in the same way.

The localization in .Net is implemented with a fallback feature from the more specific to the more generic. If It cannot find the resource file for the specified language and local (e.g. Resources.fr-CA.resx ), it looks for the resource file of the language alone (e.g. Resources.fr.resx) and if it cannot find it, it uses the default resource file (e.g. Resources.resx).

For more details, see Hierarchical Organization of Resources for Localization.

Upvotes: 1

Related Questions