Search code examples
c#wpfproperties.settings

How to store language specific text in C#/WPF application


I need to be able to display language specific UI text when a user uses the application. Currently I am storing the text in a Properties.Settings file for each language. Is there any way to create a variable that points to the settings file so that I just set this variable to point to the selected language settings file and the application then uses this variable to retrieve the required text strings.

Using Properties.Settings.Default.textString means I have to do something like the following:

if (language == "English") {
   String text = Properties.SettingsEnglish.Default.textString;
} else 
if (language == "SomethingElse") {
   String text = Properties.SettingsSomethingElse.Default.textString;
}

I would prefer doing something like this because it would require only performing the check only once:

Properties varSettings;

if (language == "English") {
   var = Properties.SettingsEnglish;
} else 
if (language == "SomethingElse") {
   var = Properties.SettingsSomethingElse;
}


...
String text = varSettings.Default.textString;

Any suggestions on the best way to do this - bearing in mind this must be a user selectable application option rather than a OS level language specific install option.


Solution

  • .Net applications already have a powerful localization mechanism built in, discussed at How to use localization in C#. To summarise:

    1. Add resource files to your project and give them a language indicative suffix appropriate suffix, e.g. "strings.fr.resx" for French or "strings.de.resx" for German.
    2. Add strings to the resx files with appropriate names, e.g. "header", "label1", "label2".
    3. Save the resource file.
    4. Set Thread.CurrentThread.CurrentUICulture to the desired culture.
    5. Load the strings from the file, using the class automatically created, e.g. "strings.header".

    Do not forget non-string localization aspects like time and date formats, too.

    For an overview, see http://msdn.microsoft.com/en-us/library/h6270d0z(v=vs.110).aspx.