Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 586
mahmoud Avatar asked Oct 07 '15 09:10

mahmoud


People also ask

How do you create a multi language application?

Multilanguage support is easy done for android. Create a new values directory for the language with the suffix of the language code. For german: values-de or french: values-fr than copy your string. xml into that and translate each entry.


2 Answers

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
like image 80
Reza Aghaei Avatar answered Sep 18 '22 23:09

Reza Aghaei


Using a resource file might be easier in some cases.

  1. Add a new resource file to the project in Visual Studio. eg. en_local.resxfor english fr_local.resx for french.

  2. 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: en_local.resx fr_local.resx

  3. In the code, use public static ResourceManager rm = new ResourceManager("WindowsFormsApp1.en_local", Assembly.GetExecutingAssembly()); to select the language resource.

  4. When you need to output any string to the application, use function GetString(), for example label1.Text = rm.GetString("welcome");

like image 39
wwjih123 Avatar answered Sep 20 '22 23:09

wwjih123