Culture Resource Manager

1.2k Views Asked by At

Problem

I am trying to implement a system in my program to switch language. I found out that i can use CultureInfo and ResourceManager to achieve that. I built up this code after a couple of hours having problem with the resource not found, finally i found and answer here on stackoverflow and i arranged the following code:

CultureInfo culture;
culture = CultureInfo.CreateSpecificCulture("it-IT");

CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;

Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;

Assembly resourceAssembly = Assembly.Load("MY ASSEMBLY NAME");
ResourceManager manager = Properties.Resources.ResourceManager;
string[] manifests = resourceAssembly.GetManifestResourceNames();

string manifest = manifests[0].Replace(".resources", string.Empty);
manager = new ResourceManager(manifest, resourceAssembly);

string greeting = String.Format("The current culture is {0}.\n{1}",
                                            Thread.CurrentThread.CurrentUICulture.Name,
                                            manager.GetString("HelloString"));

MessageBox.Show(greeting);

Since this is a really big program with a lot of pages, windows and usercontrols, i need to access the language from a lot of different files. The code i posted above, should look into the root of my Solution and look for a file named it-IT.resx. it says that the current culture is it-IT but it doesn't write the value of HelloString, but it doesn't give any error so it's definitely a problem with Resource Manager but i don't know why it doesn't crash saying it doesn't find the resource. I am sure that inside the resx file there is a value called HelloString.

1

There are 1 best solutions below

0
On

Is there a reason to load the types dynamically with Assembly.Load()? It is asking for trouble. Another way would be adding a static assembly reference.

This approach has many advantages:

  • the resource names can be accesses as property names, it is comfortable
  • the calling code uses always existing resources
  • in the calling code there are no references to non-existing resources

To be able to use resources from another assembly the resource access modifier needs to be set to public:

enter image description here

If the resources file is named RText, as in the example above, the value of a resource can retrieved from another project with:

var val = ProjectWithResourcesNamespace.RText.HelloString;