views:

3326

answers:

4

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 try return some values based on the thread culture.

Please explain to me , how this is done.

Thanks you

A: 

It might help if you describe what is not working.

Have you tride looking up tutorials? This thread should help you: http://forums.asp.net/t/1278099.aspx

You may also have a look at: http://www.java2s.com/Tutorial/CSharp/0460__GUI-Windows-Forms/CreateaResxresourcefile.htm

https://media.sdn.sap.com/html/submitted_docs/PDK_for_dotNET_10/Tutorials/Localizing%20Portal%20Component.htm

Steven
A: 

Well, you didn't mention any specific problem, so we can give some specific help.

This link might help. It has some sample files and sample code on using them.

Samuel Carrijo
+27  A: 
  • Add a Resource file to your project (you can call it "strings.resx")
  • Add a string resouce in the resx file and give it a good name (example: name it "Hello" with and give it the value "Hello")
  • Save the resource file

Run this code:

Console.WriteLine(strings.Hello);

It should print "Hello".

Now, add a new resource file, named "strings.fr.resx" (note the "fr" part; this one will contain resources in French). Add a string resource with the same name as in strings.resx, but with the value in French (Name="Hello", Value="Salut"). Now, if you run the following code, it should print Salut:

Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("fr-FR");
Console.WriteLine(strings.Hello);

What happens is that the system will look for a resource for "fr-FR". It will not find one (since we specified "fr" in your file"). It will then fall back to checking for "fr", which it finds (and uses).

The following code, will print "Hello":

Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
Console.WriteLine(strings.Hello);

That is because it does not find any "en-US" resource, and also no "en" resource, so it will fall back to the default, which is the one that we added from the start.

You can create files with more specific resources if needed (for instance strings.fr-FR.resx and strings.fr-CA.resx for French in France and Canada respectively). In each such file you will need to add the resources for those strings that differ from the resource that it would fall back to. So if a text is the same in France and Canada, you can put it in strings.fr.resx, while strings that are different in Canadian french could go into strings.fr-CA.resx.

Fredrik Mörk
Now this is a great answer.. Thank you so much, you deserve a bottle of jack Daniels for this one! I've been battling on this for ages... This is so much easier than having to create and compile resource files... to be honest, I would have ended up using a non standard Microsoft Approach for this, just because MS don't document localization properly... thanks again...!!!
JL
Excellent! I learned two things from this first the C# localization and how to answer someone problem so simply. Thanks.
Ramiz Uddin
The answer could make reference to the "behind-the-scenes" plumbing that is being done by Visual Studio here: resx.designer.cs file, making the intellisense work; satellite assemblies compiled with the class library, that need to be deployed with the compiled assembly and any later projects that use it, etc... The answer is nice and simple, but it doesn't help explain where things might go wrong eg if you don't use Visual Studio.
Tao
+4  A: 

It's quite simple, actually. Create a new resource file, for example Strings.resx. Use the apprioriate file template, so Visual Studio will automatically generate an accessor class (the name will be Strings, in this case). This is your default language.

Now, when you want to add, say, German localization, add a localized resx file. This will be typically Strings.de.resx in this case. If you want to add additional localization for, say, Austria, you'll additionally create a Strings.de-AT.resx.

Now go create a string - let's say a string with the name HelloWorld. In your Strings.resx, add this string with the value "Hello, world!". In Strings.de.resx, add "Hallo, Welt!". And in Strings.de-AT.resx, add "Servus, Welt!". That's it so far.

Now you have this generated Strings class, and it has a property with a getter HelloWorld. Getting this property will load "Servus, Welt!" when your locale is de-AT, "Hallo, Welt! when your locale is any other de locale (including de-DE and de-CH), and "Hello, World!" when your locale is anything else. If a string is missing in the localized version, the resource manager will automatically walk up the chain, from the most specialized to the invariant resource.

You can use the ResourceManager class for more control about how exactly you are loading things. The generated Strings class uses it as well.

OregonGhost