Changing Language during runtime

  • Thread starter Thread starter Ben Smith
  • Start date Start date
B

Ben Smith

Can I ask how people have managed to deal with an application that has to
change a language from a menu selection during runtime. Since
CurrentThread.CurrentUICulture is not supported in within the Compact
Framework so it not as easy as setting this in the application.

All advice on approaches and tools specifically for the CF would be
appreciated.

Thanks, Ben
 
If you havent already found how to do it, here is one way.

To support multiple languages and switch at run time:
Create resource files (.resx) in the project with string resources. For example name the files something like "Langauge.en.resx" for English and "Language.fr.resx" for French. The "Language" bit can be anything but must be used in the name of each resx file. The "en" defines the culture code (language) of the file must be present. The names of each entry must match. i.e. if you have a resource called strName it must appear in each resx file.

When you build the solution, it will create "en" and "fr" satellite assembly folders in the output folder and the satellite folders will contain the satellite assemblies for each resx but with identical names.

Add the following code to use and switch languages.

In your Program.cs...
Code:
using System.Globalization; 
using System.Resources;
...
public static ResourceManager rmLanguage = null;
public static CultureInfo currentCulture = null;
...
initialize ResourceManager and CultureInfo somewhere in Program.cs
Code:
currentCulture = new CultureInfo("en");// set default lang
rmLanguage = new ResourceManager("YourNamespace.Language", this.GetType().Assembly);
To get a string in the current language
Code:
string langStr = Program.rmLanguage.GetString("strText", Program.currentCulture);
Where strText is a string Resource that exists in all language resx files.

To change the current language (from a menu handler perhaps)
Code:
 Program.currentCulture = new CultureInfo("fr");//for French
or
Code:
 Program.currentCulture = new CultureInfo("en");//for English


The next time you call GetString("strText", Program.currentCulture), it will return the resource in the current culture's language.

Barrett Lee
 
Last edited:
Back
Top