Shweta
Shweta

Reputation: 21

Localization in winforms

Resource files are not getting created for the newly added forms when the localized property is set to true in VS 2012.

When I add a new form to the project, set the Localizable property to true and build the application, .resx files are not getting created.

Upvotes: 1

Views: 715

Answers (1)

DotNet Developer
DotNet Developer

Reputation: 3018

Carefully follow this walkthrough. The experiment I did below in VS 2012 is working fine.

Step1.

Put a Label onto Form1

Set Form1.Localizable = true

Set Form1.Language = Default

Set label's text = "Hello world!"

enter image description here

Step2.

Set Form1.Language = Russian

Set label's text = "Привет мир!"

enter image description here

After these steps resource files become visible in Solution Explorer

enter image description here

Now add following code into Form1's constructor

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            switch (MessageBox.Show(
                "Press 'Yes' for default language, 'No' for Russian.",
                "Language Option", MessageBoxButtons.YesNo))
            {
                case System.Windows.Forms.DialogResult.Yes:
                    System.Threading.Thread.CurrentThread.CurrentUICulture =
                        System.Globalization.CultureInfo.CreateSpecificCulture("");
                    break;
                case System.Windows.Forms.DialogResult.No:
                    System.Threading.Thread.CurrentThread.CurrentUICulture =
                        System.Globalization.CultureInfo.CreateSpecificCulture("ru");
                    break;
            }
            InitializeComponent();
        }
    }
}

Run the application and see the result.

The main purpose of the code is to show that CurrentUICulture must be set before the method InitializeComponent is called. In real applications, however, setting CurrentUICulture property, usually, takes place on program startup. So the code must be moved to where the program starts.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            switch (MessageBox.Show(
                   "Press 'Yes' for default language, 'No' for Russian.",
                   "Language Option", MessageBoxButtons.YesNo))
            {
                case System.Windows.Forms.DialogResult.Yes:
                    System.Threading.Thread.CurrentThread.CurrentUICulture =
                        System.Globalization.CultureInfo.CreateSpecificCulture("");
                    break;
                case System.Windows.Forms.DialogResult.No:
                    System.Threading.Thread.CurrentThread.CurrentUICulture =
                        System.Globalization.CultureInfo.CreateSpecificCulture("ru");
                    break;
            }
            Application.Run(new Form1());
        }
    }
}

If you define UI language setting for your application then you can use the value of the setting here and set UI language. It will affect all forms you have defined in your application.

Upvotes: 1

Related Questions