I just can\'t seem to get localization to work.
I have a class library. Now I want to create resx files in there, and return some values based on the threa
A fix and elaboration of @Fredrik Mörk answer.
strings.resx
Resource file to your project (or a different filename)Access Modifier
to Public
(in the opened strings.resx
file tab)Hello
, value Hello
)Visual Studio auto-generates a respective strings
class, which is actually placed in strings.Designer.cs
. The class is in the same namespace that you would expect a newly created .cs
file to be placed in.
This code always prints Hello
, because this is the default resource and no language-specific resources are available:
Console.WriteLine(strings.Hello);
Now add a new language-specific resource:
strings.fr.resx
(for French)Hello
, value Salut
)The following code prints Salut
:
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("fr-FR");
Console.WriteLine(strings.Hello);
What resource is used depends on Thread.CurrentThread.CurrentUICulture
. It is set depending on Windows UI language setting, or can be set manually like in this example. Learn more about this here.
You can add country-specific resources like strings.fr-FR.resx
or strings.fr-CA.resx
.
The string to be used is determined in this priority order:
strings.fr-CA.resx
strings.fr.resx
strings.resx
Note that language-specific resources generate satellite assemblies.
Also learn how CurrentCulture
differs from CurrentUICulture
here.