Change cultureinfo and get correct resx translation - c#

Im trying to use the following code to get translation from my swedish resx but it will only return the english translation from my default resx. I have the translations in both file with the exact same key. I call this from a command line c# program. Anyone know why it will not translate?
public String GetString(String resxPackageName, String xmlKey)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("sv-SE", false);
ResourceManager rm = new ResourceManager("MyPackage.CustomerPortal.Followup", this.GetType().Assembly);
return rm.GetString("CurrentPriceTagTranslation");
}

I think that if you don't specify a culture in GetString method it will use the CurrentUICulture of the calling thread, if you change it everything should work.

Related

Why wpf ResourceManager GetString (localization) returns english only

I have a localization issue.
Here,in below code (auto generated in designer file) the str always returns english values even if the application is in other locale.
.
ResourceManager rm = new System.Resources.ResourceManager("A.b.Properties.Resources", typeof(Resources).Assembly);
.
.
var culture = Thread.CurrentThread.CurrentUICulture; //zh-CN selected as locale
var str = rm.GetString(key, culture); //always returns english :(
.
My resource file sits under the zh-CN folder wherever the executables got built:
"..\zh-CN\A.b.resources.dll" (checked the dll internally, it has the all the strings-values available in cn language)
Project name is: A.b
Namespace in the designer file is: A.b.Properties
I am not able to figure out why the application returns only the english values and not the locale specific ones!
I tried it a lot but unable to figure out whats wrong!
Though i, noticed that the _resourceSets [zh-CN] object Values has all english items in it.
Any idea/reference is much much appreciated.
At application startup, my application determines my preferred language culture, gets the system CultureInfo object for it, and sets the following two values. Are you doing something like this?
System.Threading.Thread.CurrentThread.CurrentUICulture = desiredCulture;
CultureInfo.DefaultThreadCurrentUICulture = desiredCulture;
Also, I don't know if it matters but all of my language-specific resource DLLs live in subfolders that are just the TwoLetterISOLanguageName code from the CultureInfo in question.
So for example, my French translations live in the "fr" subfolder. Are you sure your resources should not live in a "\zh" subfolder, instead of "\zh-cn"?

Localization does not work on some projects

I am trying to make my project multilingual. To do this, I created resource files for English and for other languages, for example:
langu.resx
langu.uk-UA.resx
In some of my projects, everything works fine. But some projects don't want to change the language.
With this System.Threading.Thread.CurrentThread.CurrentUICulture returns the correct culture
I tried changing the language in the program with the following code:
ResourceManager RM = new ResourceManager("PasteCurb.Properties.Lang.langu", Assembly.GetExecutingAssembly());
string day = RM.GetString("btnApplyText");
CultureInfo ci = new CultureInfo("uk-UA");
string dayrr = RM.GetString("btnApplyText",ci);
But in the variable dayrr, I get the value in English, instead of Ukrainian.
Anyone have any ideas?
Set the culture info like below and than you can access a text string by the name:
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("uk-UA");
var text = Properties.Resources.btnApplyText;
When you access resources directly by using the ResourceManager be sure that the root name of the resource file is defined correctly. By default the root name doesn't include the language identifier.
It should look like below:
var rm = new ResourceManager("PasteCurb.Properties.Resources", Assembly.GetExecutingAssembly());
string dayrr = rm.GetString("btnApplyText", new CultureInfo("uk-UA"));
For more information see CultureInfo.CurrentCulture Property

Add / switch between embedded / linked resources at running time

I have the resources.resx in my c# code with some strings filled:
Text1,"Some Text"
and i can call it during running time by
Properties.Resources.Text1
which results in
"Some Text"
Now i want to have Text1 a different output (another language for example or something)
so that Properties.Resources.Text1 results in "Different Text".
How can i achieve this?
EDIT1: i discovered this but i was looking for a different approach with the resource files.
If you want to use different Resource Files, you can use the ResourceManager:
ResourceManager rm;
if (Configuration.Default.Culture == "en-US")
rm = new ResourceManager(typeof(Resource1));
else
// ...
String label = rm.GetString("Text1");
Save the the Culture in the User Settings, add a configuration file and define a user variable.
Configuration.Default.Culture= "en-US";
Configuration.Default.Save();
Updated question according to information
I am afraid you have to add another resource file for other culture. just look into this thread
How to use localization in C#
In reference to a comment:
get the current cultureinfo and load resources like this:
Thread.CurrentThread.CurrentCulture.ClearCachedData();
CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
if(currentCulture.Name == "en-US")
Console.WriteLine(resources.Text1);
else if if(currentCulture.Name == "ja-JP")
Console.WriteLine(resourcesJapan.Text1);

Always returns the string with the default language from a resource file

I have two resources files, Emails.es.resx (es-ES) and Emails.eu.resx (eu-ES), and I have a problem to retrieve a string from eu-ES file with myLib.Emails.ResourceManager.GetString("textKey", "eu-ES");
At local this works properly but at server, is a web app + IIS, not works, always return a correct value of "textKey" but with default language es-ES.
I cleaned the solution and rebuilded all, but without results.
Does anyone knows what could happen?
Make sure you set the correct culture. Suppose you have given option somewhere at website default page where user can choose language. On language change save the selected language in application state somewhere.. session or catch or whatever suite you. Set culture as following
explained at following link:
protected void Page_PreInit(object sender, System.EventArgs e) {
System.Globalization.CultureInfo lang = null;
lang = new System.Globalization.CultureInfo("zh-CN");
System.Threading.Thread.CurrentThread.CurrentCulture = lang;
System.Threading.Thread.CurrentThread.CurrentUICulture = lang;
}
Now retrieve your resources.. it should return correct one.
Rename your files to Emails.es-ES.resx and Emails.eu-ES.resx

Problem with changing language using CultureInfo

Language is not getting changed when we are giving specific culture like "fr-FR" through Resource File.please help me out of it if any one knows ,Thanks in advance.
CultureInfo cinfo = new CultureInfo("fr-FR");
Thread.CurrentThread.CurrentCulture = cinfo;
Thread.CurrentThread.CurrentUICulture = cinfo;
The below code indicate that accessing the value through resource1 file according to the culture .
_inboxpage.Text = Resource1.Ready;
The resource files need to have a special naming convention to work transparently with different culture information.
You create a separate resource file for each language that you want to support or for a language and culture. Have one separate neutral resource file for the application to fall back upon in case the required key/value pair is not found.
ex:
Resources.resx //neutral resource file
Resources.fr.resx //french specific file
Resources.fr-FR.resx //French language for France
and so on.
You can get more details here:http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx
Finally i got a solution that while doing Localization in Plugin Application we need to copy that culture folders like "fr-FR" to corresponding Main Application then it will works fine.

Categories