A very simple thing, and I can’t get it to work. I want to globalise my dll thus I’m using resource files + the ResourceManager.
I call the resourcemanager like this:
var p = new ResourceManager("Appname.Default", Assembly.GetExecutingAssembly());
Get the strings like this
System.Diagnostics.Debug.WriteLine(p.GetString("greeting"));
System.Diagnostics.Debug.WriteLine(p.GetString("greeting", new CultureInfo("nl")));
System.Diagnostics.Debug.WriteLine(p.GetString("greeting", new CultureInfo("nl-NL")));
System.Diagnostics.Debug.WriteLine(p.GetString("greeting", new CultureInfo("en")));
And it returns 4 times the same string. My files are called
Default.resx
Default.en.resx
Default.nl.resx
Default.nl-NL.resx
All file settings are the same, but as mentioned – only the resource in the Default file is used.
What am I overlooking here?
There are a few ways of using resource files, one of which is using
.resxfiles. These files get localized automatically, based on the value ofThread.CurrentThread.CurrentUICulture. The default.resxfile gets compiled into the assembly it is part of (for example your main executable), while the localized resources (Default.nl-NL.resx) get compiled into their own directory (based on the culture identifier,nl-NLin this case) into an assembly, called<AssemblyName>.resources.dll.Addressing values from those resources is as easy as
<ResourceName>.<KeyName>, for exampleDefault.Greeting. To test it, you change the culture, using:Which will output
On program startup, the UI Culture is set to the culture of the computer it’s running on, so you won’t have to specify the language yourself to always present the localized resources. So,
.resxfiles seem the way to go.When using the
ResourceManagerfromvar p = new ResourceManager("Appname.Default", Assembly.GetExecutingAssembly());, you will have to read.resourcesfiles. If there is no (in your case)Appname.Default.resourcesfile, thep.GetStringwill fail. So I guess you have created one.resourcesfile earlier, but haven’t converted the localized.resxfiles to.resourcesfiles.If you want to use the ResourceManager to be able to specify the culture, you can use: