Localization does not work on some projects - c#

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

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"?

Get localized string resources before run (C# Winforms)

I need to get localized string resources before run (C# Winforms). I have custom control of button with printing text on event OnPaint. I can set culture for set location before running in the constractor of custom control and see erea for text in designer:
var culture = new CultureInfo("de-DE");
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
But when I try to get string resource before running I get neutral string resource instead of the German:
ResourceManager p = new ResourceManager("myProject.Properties.Resources",
Assembly.GetExecutingAssembly());
Text = p.GetString(ResourceNameForText, new CultureInfo("de-DE"));
How can I get the German string resurce in designer before running?
We have found a solution. We have created an open property in custom control, which calls stream reading and parsing method from Resources.de-DE.resx during design-time mode only.

managing MultiLanguage resources

I have created two resource files in my project GlobalRes.ge.resx and GlobalResources.en.resx
I receive language as an input parameters . I want to know how can I read my values based on language. for example if string lang = "en" then by globalres.welcome I should see WOLCOME but if I choose lang = "ge" then globalres.welcome should be willkommen
(I have already created the welcome line in both files)
The Resource Designer will load the appropriate text based on the CurrentUICulture
System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
bCancel.Text = Resource.Cancel;
In .NET 4.5 and later you can use the following properties to set the DefaultThreadCurrentCulture & DefaultThreadCurrentUICulture culture.
CultureInfo.DefaultThreadCurrentCulture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
CultureInfo.DefaultThreadCurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo("en-US");

How to read global resource file

I have a window based application in c#. Where i am displaying some message box based on language selected.Its working fine with Form level resources but what if i want to access global resource file(project level).
ResourceManager res_man = new ResourceManager("Resources",typeof(Form2).Assembly);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("ar-SA");
string s = res_man.GetString("String1");
MessageBox.Show("Arabic-" + s);
I tried this but any how not working
For updated ans
Access Modifier is by default internal. Did you use the PublicResXFileCodeGenerator ?
You can set the Access Modifier to public when you open the RESX file in Visual Studio. There is a dropdown box that can be found at the top of the form which changes the Access Modifier.
And after that you will be able to access :
var resxManager = new ResourceManager(typeof(Resource));
var currentString = resxManager.GetString("Practitioner", CultureInfo.GetCultureInfo("en-US"));
Here can be a related issue : Visual Studio - Resx File default 'internal' to 'public'
try to change Current Culture in place of UI culture -
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("ar-SA");
I am using following code for fetching specific value from Global Resource for specific culture -
ResourceManager myManager = new ResourceManager(typeof(Resources.Strings));
String currencySymbol = myManager.GetString("Currency", CultureInfo.GetCultureInfo("en-GB"));

Change cultureinfo and get correct resx translation

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.

Categories