Creating Multilingual Applications in Visual Studio

Steps to make it multilingual

If you want to make it multilingual, create the default value in English.

This time, we will create a bilingual application with Japanese and English support. The sample application looks like this

 

The property Language is set to “Default”. In this case, the default status is Japanese, which means that if the PC language is other than Japanese or English, the default Japanese will be displayed.

For example, if the PC is in a German environment, Japanese will be displayed. Moreover, since the font is Japanese, there is a possibility of garbled characters. Of course, it is possible to support Japanese, English, and German, but it is practically impossible to support all languages.

If the PC does not support any of the application languages, it is preferable to display the form in English, which is the universal language.

Therefore, we will change the Japanese text of the form to English.

The text on the form screen in the initial display, where Language is the default, should be in English, for example, “りんご” → “Apple”.

However, if you have already created a Japanese-language application and want to make it multilingual later, it is not easy to change the default to English and then add English. The explanation that follows should be taken in reverse.

 

Multilingualize control text

Change the Language property to “Japanese” from the pull-down menu. There is also “Japanese (Japan),” but since Japanese (Japan) is also related to the location, it is safe to select “Japanese.

If you change Language to “Japanese,” Localizable under it will change to “True. If it remains “False”, change it to “True”.

 

Change the English text on the form to Japanese text such as “Apple” to “りんご”. After the change, a file named Form1.ja.resx will be created. The difference information from the default Form1.resx is saved in this file.

 

The contents of Form1.ja.resx looks like this.

 

Multilingualize text in code other than controls

If it is a control, the process so far is fine, but let’s say you have a message in your code.

private void button1_Click(object sender, EventArgs e) 
{ 
    MessageBox.Show("本当に閉じますか?");
}

Texts that are not controls, such as these, will be multilingualized in a separate procedure.

Right-click on the solution and select “Add” > “New Item”.

 

Select the assembly resource file, name it “Resource.ja.resx” and click the Add button.

 

The Resource.ja.resx file will be added directly under the solution.

 

Move Resource.ja.resx from directly under the solution to the Properties folder.

The original Resource.resx in Properties is in English (default) and Resource.ja.resx is the Japanese file with the difference information.

 

Open the English (default) file Resources.resx. Enter the name of the variable in Name and the actual English text you want to display in Value.

Name: msg_clolse
Value: Are you sure you want to close?

 

Open the file Resources.ja.resx for Japanese. Enter the same variable name as Resources.resx in the Name field and the actual Japanese text you wish to display in the Value field.

Name: msg_clolse
Value: 本当に閉じますか?

 

private void button1_Click(object sender, EventArgs e) 
{ 
    MessageBox.Show(Properties.Resources.msg_close);
}

Change the direct entry in the code to Properties.Resources.Variable Name.

 

Dynamic language switching

Add a combo box with “ja” and “en” options for language switching.

A change of choice calls the change language event, and the ChangeLanguage method redisplays the form in the selected language.

//Language index change event
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    string langName=comboBox1.Text;
    if (langName != null && langName != "")
    {
        //Change Language
        ChangeLanguage(comboBox1.Text);
    }
}

//Change Language
private void ChangeLanguage(string langName)
{
    System.Globalization.CultureInfo culture = System.Globalization.CultureInfo.GetCultureInfo(langName);
    System.Threading.Thread.CurrentThread.CurrentUICulture = culture;

    Form1 myform = new Form1();
    myform.Show();
    this.Hide();
}

「ja」Japanese

「en」English

Since this is just a sample of language switching, the form before language switching is hidden, so if Application.Exit is described in the Form_Closing event, all hidden forms will be closed as well.

private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
    Application.Exit();
}

コメント

Copied title and URL