0

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.

Duncan Groenewald
  • 8,496
  • 6
  • 41
  • 76
  • why don't you make a helper method (or extension method) which passes in the language and returns you the appropriate "text"? set the language to use in some global variable when the app starts up or based on user configuration and use that to pass into this helper method (or extension method). Or perhaps use a dictionary and store the language as the key followed by some identifier (i.e English_ErrorMsg) and the value being the appropriate text read from the app properties? – Ahmed ilyas Apr 29 '14 at 00:41

2 Answers2

1

.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.

Community
  • 1
  • 1
akton
  • 14,148
  • 3
  • 43
  • 47
  • This looks promising - I was using the Settings file instead of a Resources file. An idea if I can change this while the application is running, needs to be done when user selects a language in the App. – Duncan Groenewald Apr 29 '14 at 01:07
  • @DuncanGroenewald To switch to a different language, just change `Thread.CurrentThreat.CurrentUICulture`. You can also embed the localizations in different assemblies and ship them independently of the app. – akton Apr 29 '14 at 01:41
0

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#.

Community
  • 1
  • 1
Noctis
  • 11,507
  • 3
  • 43
  • 82