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.