Get localized string resources before run (C# Winforms) - c#

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.

Related

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

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 make multi-language app in Winforms?

I have an application and I need to use two languages in that application.
For example :
English
Arabic
But I don't know how could I do that. Anybody can help me for this?
I need some examples in C# Windows Forms.
Using Localizable and Language Property of Form
Form class have Localizable and Language Property. If you set Localizable property to true, you can add controls to form for default language and set properties for default language. Then you can select another languages and change properties for those languages. This way, value or localizable properties will store in separate resource files for different cultures.
Note: A property is considered as localizable if it's decorated with [Localizable(true)] attribute. For example BackColor property is not localizable, but Text property is localizable.
Localizing Messages and Images using Resx Resource Files
The project has a Rseources.Resx file under Properties folder which you can use for localizing images and messages. Also you can add .resx Resource files to project. For example you can create a Strings.resx file and add some string key and values to it, then copy it as strings.en.resx and strings.fa.resx and edit values for those languages. Then you can use those resource values, For example:
MessageBox.Show(Properties.Resources.AreYouSure);
Will show the value of AreYouSure from Resources.Resx file with the current UI culture language.
If a resource key not found for a culture or the specified culture not found for the resource file, value of the key in neutral culture of the Resx file will be used.
Change the language at Run-time
You can set the culture of a application to Persian using:
System.Threading.Thread.CurrentThread.CurrentCulture =
System.Globalization.CultureInfo.GetCultureInfo("fa");
System.Threading.Thread.CurrentThread.CurrentUICulture =
System.Globalization.CultureInfo.GetCultureInfo("fa");
You should put the above code at start of your application or before showing a form.
More information
For more information and Example:
Globalizing Windows Forms
Walkthrough: Localizing Windows Forms
How to: Set the Culture and UI Culture for Windows Forms Globalization
Using a resource file might be easier in some cases.
Add a new resource file to the project in Visual Studio.
eg. en_local.resxfor english fr_local.resx for french.
Open the resource file, in the strings, name your string and put different translation in the value cell. For example: next station's value inen_local.resx is next station but in fr_local.resx can be Prochaine station.
example as below:
In the code, use public static ResourceManager rm = new ResourceManager("WindowsFormsApp1.en_local", Assembly.GetExecutingAssembly());
to select the language resource.
When you need to output any string to the application, use function GetString(), for example label1.Text = rm.GetString("welcome");
There are some missing parts in wwjih123's answer.
In VS2017
1-First of all create resource in projects root folder (Not in Resources folder). Name it like lang_en, lang_tr, lang_fr etc...
2-then object properties window leave Build action as Embedded Resource
3-inside the lang_tr.resx file add new string lbl_error and value "Hata" in turkish (whatever you like)
4- inside the class define variables as:
ResourceManager res_man; // declare Resource manager to access to specific cultureinfo
5-in class initialization after InitializeComponent();
Console.WriteLine("You are speaking {0}",
System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName);
res_man = new ResourceManager("MyApp.lang_"+ System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName, Assembly.GetExecutingAssembly());
lblError.Text = res_man.GetString("lbl_error");
if your ui language is Turkish it will automatically load the lang_tr.resx,
if english the lang_en.resx file will be loaded
etc...
good luck
Create an extension class and do as the following:
public static class TranslateToKurdish
{
public static void ToKurdish(this Control control,string kurdishText,float fontSize=10)
{
switch (control)
{
case TextBox textBox:
textBox.PlaceholderText = kurdishText;
textBox.RightToLeft = RightToLeft.Yes;
textBox.PlaceholderText = kurdishText;
textBox.Font = new Font("Calibri", fontSize, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
break;
case Label label:
label.Text = kurdishText;
label.RightToLeft = RightToLeft.Yes;
label.Font = new Font("Calibri", fontSize, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
break;
case Button button:
button.Text = kurdishText;
button.RightToLeft = RightToLeft.Yes;
button.Font = new Font("Calibri", fontSize, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
break;
}
}
}
then you can use in Form
if (userLanguage == stringLanguage)
{
isKurdishLanguage = true;
RightToLeft = RightToLeft.Yes;
RightToLeftLayout = true;
btnTruckTracking.Font = new Font("Calibri", 13.5F, FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
btnTruckTracking.ToKurdish(#"بارهه‌ڵگره‌كان",12);
btnSearch.ToKurdish(#"گـــه‌ڕان",12);
BtnProduct.ToKurdish(#"بـــه‌رهــه‌م",12);
btnCompany.ToKurdish(#"كــۆمپـانیـایه‌كـان",12);
btnUsers.ToKurdish(#"به‌كارهێنه‌ران",12);
btnClose.ToKurdish(#"داخســـتن",12);
}

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"));

Windows Form Culture not setting up

I am trying to get right string values from culture resource file but it's not working, always returning english resources,
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
but
Resources.Resource1.myResource;
still getting english resources, I have two files Resource1.resx and Resource1.fr-FR.resx
I think that you need
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR")
Thread.CurrentThread.CurrentUICulture
Gets or sets the current culture used by the Resource Manager to look
up culture-specific resources at run time.

Categories