0

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.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
LifeRewind
  • 19
  • 6
  • Have you tried debugging the code? Not showing the message may not be connected to localization. – Martin Staufcik Aug 27 '16 at 18:23
  • How can I debug the code? I tried but I couldn't find any error – LifeRewind Aug 27 '16 at 18:45
  • Set a breakpoint at the line `MessageBox.Show(greeting);` and inspect the value of `greeting`. – Martin Staufcik Aug 27 '16 at 18:51
  • I tried that already. I tried to see the value of manifest and it's "My assembly name.g". I can't understand what that g stands for – LifeRewind Aug 27 '16 at 22:13
  • @LifeRewind, you'd normally see x.g.resources in a WPF application and they would contain the compiled WPF BAML resources. Your assembly probably has multiple manifest resource streams and you want to skip over the .g.resources ones and look at the x.Properties.Resources.resources ones. I'd recommend you grab a tool like ILSpy that will allow you to visually inspect all of the resource streams in an assembly so you can more easily see what you need to skip over. – ScheuNZ Aug 28 '16 at 06:23

1 Answers1

0

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;
Martin Staufcik
  • 8,295
  • 4
  • 44
  • 63