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.
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: