22

I need a very quick introduction to localization in a class library

I am not interested in pulling the locale from the user context, rather I have users stored in the db, and their locale is also setup in the db....

my functions in the class library can already pull the locale code from the user profile in the db... now I want to include use resx depending on locale...

I need a few steps to do this correctly...

And yeah - I have already googled this, and some research, but all the tutorials I can find are way too complex for my needs.

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
JL.
  • 78,954
  • 126
  • 311
  • 459
  • I came across some strange and alien looking teqniques from MSDN... using AL... surely I can accomplish this just using standard visual studio? – JL. Jul 16 '09 at 17:05

3 Answers3

26

Unfortunately, this subject is way too complicated. ;) I know, I've done the research as well.

To get you started though,

  1. create a Resources directory in your assembly.

  2. Start with English and add a "Resources File" (.resx) to that directory. Name it something like "text.resx". In the event that the localized resource can't be found, the app will default to pulling out of this file.

  3. Add your text resources.

  4. Add another resources file. Name this one something like "text.es.resx" Note the "es" part of the file name. In this case, that defines spanish. Note that each language has it's own character code definition. Look that up.

  5. Add your spanish resources to it.

Now that we have resource files to work from, let's try to implement.

In order to set the culture, pull that from your database record. Then do the following:

String culture = "es-MX"; // defines spanish culture
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

This could happen in the app that has loaded your assembly OR in the assembly initialization itself. You pick.

To utlize the resource, all you have to do is something like the following within your assembly:

public string TestMessage() {
  return Resources.Text.SomeTextValue;
}

Ta Da. Resources made easy. Things can get a little more complicated if you need to change usercontrols or do something directly in an aspx page. Update your question if you need more info.

Note that you could have resource files named like "text.es-mx.resx" That would be specific to mexican spanish. However, that's not always necessary because "es-mx" will fall back to "es" before it falls back to the default. Only you will know how specific your resources need to be.

NotMe
  • 87,343
  • 27
  • 171
  • 245
  • Sounds good so far, except Resouces.Text.SomeTextValue - where does that come from, what namespaces do I need to include? – JL. Jul 17 '09 at 11:24
  • Just fixed step 1. The full namespace for the SomeTextValue would be: MyAssemblyName.MyResourcesDirectoryName.MyResourceFile.MyTextValue. So, using my example, it would be: MyAssemblyName.Resources.Text.SomeTextValue. – NotMe Jul 17 '09 at 18:46
  • 1
    By performing the CurrentCulture call, the system will automatically figure out whether to use text.resx or text.es.resx – NotMe Jul 17 '09 at 18:47
  • 1
    What about serving different user after each other, that would require different languages? Would I need to change the culture on the thread each time, beforehand? – Marcel Aug 30 '12 at 08:02
  • 1
    @Marcel: Yes you can do it like that. – P-L Mar 12 '13 at 19:00
  • 1
    Thanks. This is a very succinct and straightforward answer. :) – Richard Marskell - Drackir Dec 23 '15 at 22:12
1

Name your resxes with the culture in them (eg. resource_en-GB.resx) and select which resource to query based on the culture.

NikolaiDante
  • 18,469
  • 14
  • 77
  • 117
0

In order to access the Resource file from code, you need to open the resource file and then change the "Access Modifier" dropdown to "public", mine was "no code generation". After that you can access like: Resources.FileName.ResourceName.