This is an issue that is fairly easy to implement but it can also cause a lot of gray hair,mostly because microsoft doesn’t help you all the way.
As with all coding this can be achieved in numerous ways but here is the way that worked for me:
Here’s what you need.
- A number of resource files. Just select to add a new file and choose one of type resource file
- Now create as many files as you have languages
- Make the translations by defining a key that you will call and the value
- Name each file correctly. That is <Resourcefilename>.<your language code>.resx
If unsure what language code you should use, look at this link http://msdn.microsoft.com/en-us/library/ms533052(v=vs.85).aspx
You should use the short one with small letters. Example “de” for German “en” for english and so on
- UICulture, this is a neat little property hiding under Thread.CurrentThread.CurrentUICulture. This one should contain your correct
CultureInfo object that you have set in your operating system. If you want the user to change language at any time; setting this property
is what does it. Beware though that you need to use the capitalized version of the language code when assigning UICulture with a new
CultureInfo object, for example de-DE.
- Now, you thought you were done right? with console projects and web you are but with silverlight there is one little issue remaining..You need to edit the csproj file. In there there is an xml element <SupportedCultures>
that needs to have an entry. Supporting for example english and german it would say en,de
How to call the resource file
So with all this done you can access the resource properties in two ways that I know of
1) Call by Resource filename
This is the simple way. The resource file looks like static instance and the getting the value of the key is done by calling Resourcefilename.nameofkey
2) Call by instantiating a ResourceManager object and call the GetString method
A resource manager object is created with the help of a string and an assembly object. Of course there are many constructors to choose from but this is the one I use.
The string used is the path to the resource file, like so AssemblyName.Path.SubPath.Resourcefilename. For example a resource file called Strings under the catalog Resources would need to have the following path to instantiate a ResourceManager object correctly, Resources.Strings. The file ending resx is omitted.