How to make multi-language app in Winforms?
Asked Answered
S

4

28

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.

Super answered 7/10, 2015 at 9:51 Comment(0)
V
43

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 Resources.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:

Valdes answered 7/10, 2015 at 10:40 Comment(0)
E
21

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

Exorable answered 8/11, 2017 at 19:14 Comment(1)
helpful....! It works ...Dysphoria
P
9

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

Parlay answered 12/1, 2018 at 19:33 Comment(3)
Why not in Resources folder??Tuberosity
I don't think the directory matters at allEarleanearleen
Just want to point out that, for Chinese, one can use CultureInfo.CurrentUICulture.ThreeLetterWindowsLanguageName to differentiate between traditional and simplified Chinese.Mister
M
-2

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

        }
Mapp answered 7/8, 2021 at 10:21 Comment(1)
this solution involves too many copy/paste codeBrainwash

© 2022 - 2024 — McMap. All rights reserved.