BDeveloper
BDeveloper

Reputation: 1287

problem :globalization in C# using VS2005

I want to globalize my application. I have created a small form which asks the user their language. I have a number of problems :

Problem 1:

In program.cs

new SplashScreen(_tempAL);
new LangForm(_lang);
Application.Run(new Form1(_tempAL, _lang)); 

I want the application not to call Form1 until the user clicks on OK in LangForm . For more explaintion in LangForm :

    public LangForm(char _langChar)
    {
        InitializeComponent();
        _ch = _langChar;
        this.TopMost = true;

        this.Show();

    }

    private void _btnOk_Click(object sender, EventArgs e)
    {
        string _langStr = _cbLang.SelectedText;
        switch (_langStr)
        {
            case "English":
                _ch = 'E';
                this.Hide();
                break;
            case "Arabic":
                _ch = 'A';
                this.Hide();
                break;
            case "Frensh":
                _ch ='F';
                this.Hide();
                break;
        }
        _pressedOk = true;
    }

    private void _btnCancel_Click(object sender, EventArgs e)
    {
        this.Close();
        Application.Exit();
    }

Now when I debug, the application calls LangForm and then Form1 so both forms are shown. I want Form1 to wait until the user clicks on Ok in LangForm.

Problem 2:

When should I check on the language? It's not allowed to check in "initializeComponent()" so should I check after this function and then set controls location according to the language.

Problem 3:

Within application process I displays some message so before each "MessageBox.Show("");" I should check for the language or there is another way where I may set the language once.

Problem 4:

I have searched for interfaces for MessageBox as actually I want to change its layout. How can I find templates for MessageBox?

Thanks in-advance.

Upvotes: 1

Views: 250

Answers (2)

Hans Passant
Hans Passant

Reputation: 941277

Display the language selection form as a dialog. Make your Program.cs file look like this:

[STAThread]
static void Main() {
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  if (DialogResult.OK == new LangForm().ShowDialog()) {
    Application.Run(new Form1());
  }
}

Add this line to your _btnOK click handler:

this.DialogResult = DialogResult.OK;

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1062550

To block until a form has closed, use .ShowDialog() on the LangForm. I would then set the culture (Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture) between this form closing and creating the new form. Having done this, anything from resx should load correctly.

For changing the layout of MsgBox (beyond the norm) you would have to write your own (it doesn't support this).

Something like:

[STAThread]
static void Main() {
    Application.EnableVisualStyles();

    // find the culture we want to use
    bool cont;
    string languageCode;
    using (LangForm lang = new LangForm()) {
        cont = lang.ShowDialog() == DialogResult.OK;
        languageCode = lang.LanguageCode; // "en-US", etc
    }
    if (!cont) return;

    // set the culture against the UI thread
    Thread.CurrentThread.CurrentCulture =
        Thread.CurrentThread.CurrentUICulture =
            CultureInfo.GetCultureInfo(languageCode);

    // show the main UI
    using (MainForm main = new MainForm()) {
        Application.Run(main);
    }
}

Note the use of the official culture codes will make it easier to use things like CultureInfo; if you want to use your own short-list, then use an enum, and write a method somewhere:

public static string GetCultureCode(MyCulture culture) {
    switch(culture) {
        case MyCulture.French: return "fr-FR";
        case MyCulture.English: return "en-GB";
        //...
        default: throw new NotSupportedException("Unexpected culture: " + culture);
    }
}

Upvotes: 1

Related Questions