0

i have a Globalization project under solution, and there is another MainProject. Under Globalization there are 2 resource files, Resources.resx and Resources.ar.resx. Both files have same keys. I want to output the results based on language input i get in model(API).

Declared this as global: ResourceManager rm;

Then i have this function which will check for language input from model, and select resource file accordingly.

private void CheckResourceManager(string lang)
{                    
    if (lang =="EN")
    {
        rm = new ResourceManager(
            "Globalization.Resources", 
            Assembly.GetExecutingAssembly());
    }
    else
    {
        rm = new ResourceManager(
            "Globalization.Resources.ar", 
            Assembly.GetExecutingAssembly());
    }
}

In the Api function i first check for CheckResourceManager(model.Language); and when there is a message required then: throw new Exception(rm.GetString("WrongUserName"));

Now issue is "Globalization.Resources.ar" in function is not reading the resource, like it can;t locate the file, what should i use here. If i add resource files in same project, then it will work. Advise Please. Thanks


Proper approach is from below answer, but for me this method is working fine.

Declared:

ResourceManager rm;
CultureInfo originalCulture = CultureInfo.CurrentCulture;
Assembly localizeAssembly = Assembly.Load("Globalization");

Changed the function to:

private void CheckResourceManager(string lang)
{
    if (lang =="EN")
    {
        System.Threading.Thread.CurrentThread.CurrentCulture = originalCulture;
        rm = new ResourceManager("Globalization.Resources", localizeAssembly);
    }
    else
    {
        System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("ar");
        rm = new ResourceManager("Globalization.Resources", localizeAssembly);
    }
}

and finally when i need to get some value:

throw new Exception(rm.GetString("WrongUserName",CultureInfo.CurrentCulture));

Thank You.

Dima
  • 1,717
  • 15
  • 34
Nisar Afridi
  • 167
  • 1
  • 4
  • 20
  • Now when i added solution name like `PartnerWebApplication.Globalization.Resources.ar` i get following error. `Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "PartnerWebApplication.Globalization.Resources.ar.resources" was correctly embedded or linked into assembly "PartnerWebApplication" at compile time, or that all the satellite assemblies required are loadable and fully signed.` – Nisar Afridi May 23 '17 at 07:57
  • Try : right click on `.resx` file, select `Properties` and set `Build Action` to `Embedded Resource` – tchelidze May 23 '17 at 08:06

1 Answers1

1

The whole point of ResourceManager is to swap to a different culture based on the UI culture of the current thread. You don't need to make a separate function to do it. It won't read a localized culture explicitly - it is convention based. To control the UI culture of the current thread, you need to set it explicitly in an authorization filter:

System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("ar")

Also, passing the ExecutingAssembly means that you are passing in the top level assembly, which most likely is not the assembly your resources are located in. You should use a type within the assembly to get an Assembly instance:

new ResourceManager(
        "Globalization.Resources", 
        typeof(ClassWithinResourcesAssembly).Assembly)

See the following documentation for more information about how to use the ResourceManager:

Dima
  • 1,717
  • 15
  • 34
NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • Yes this is right way. though for me i just require few values, so i didnt get into UI Culture etc. changed the function with `System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("ar"); rm = new ResourceManager("Globalization.Resources", localizeAssembly);` now its fine, for both english and arabic. – Nisar Afridi May 23 '17 at 09:58