Problem: globalization in C # using VS2005

I want to globalize my application. I created a small form that asks the user for 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 to prevent the application from calling Form1 until the user clicks OK in the LangForm. For a more detailed explanation 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 displayed. I want Form1 to wait for the user to click on Ok in the LangForm.

Problem 2:

When should I have my language tested? It is not allowed to check "initializeComponent ()" so I have to check after this function and then set the layout of the controls to match the language.

Problem 3:

As part of the application process, I would display a message before each "MessageBox.Show (" "); I have to check the language or is there another way where I can set the language once.

Problem 4:

I have been looking for interfaces for the MessageBox since I actually want to change its layout. How do I find templates for the MessageBox?

Thanks in advance.

+1


source to share


2 answers


Display the language selection form as a dialog. Create a Program.cs file as follows:

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

      



Add this line to the _btnOK click handler:

this.DialogResult = DialogResult.OK;

      

+1


source


To block until the form is closed, use .ShowDialog()

c LangForm

. Then I set the culture ( Thread.CurrentThread.CurrentCulture

and Thread.CurrentThread.CurrentUICulture

) between this form and the creation of new forms. By doing this, something from resx should load properly.

To change the layout MsgBox

(outside of the norm) you will 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 that using official culture codes will make it easier to use things like CultureInfo

; if you want to use your own shortlist, then use an enum and write the 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);
    }
}

      

+1


source







All Articles