Question

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.

Was it helpful?

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.

OTHER TIPS

Basically , i'm sure this was answered already (for example: here) , but the gist is usually the same.

You'll need a resource dictionary that hold the strings values (for example: error_message_1, error_message_2, greeting, favorite_peekachu), and then you can either have a file per language, and in the setting set that file, or have a big "if-else-otherwise" loop in code ( which makes me shudder just to think about).

I remember reading some good articles on android about this actually, I guess the idea is the same :)

Here's a link to an article on CodeProject .

Edit:
This answer is the one I was looking for: Fredrik Mörk on How to use localization in C#.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top