views:

190

answers:

1

I'm creating a multilingual application using Windows Forms. I have created my multilingual content by marking forms as localizable and switching Language to something other then default and changing the form texts.

This works great but there is a small problem with that. On one of the forms I must set up a text to be displayed in a form constructor. And now the problem is that if I enter it in the default language it will appear in that language even if culture if switched to something else.

I would like to know what's the easiest or most effective way of localizing such string.

Just an example of what kind of message I need to localize:

class MyForm : Form
{
    public MyForm()
    {
        if (myAdapter.ConnectionStatus == ConnectionStatus.OK)
        {
            statusLabel.Text = "Connected";
        }
        else 
        {
            statusLabel.Text = "Not connected";
        }
    }
}

In this example I would like to have "Connected" and "Not connected" localized based on a current culture info.

+7  A: 

When you mark a form as localizable, you are creating a different resource file for each language. You should add a string key to your resource file for "Connected" and "Not Connected" and manually pull from your resource file instead of hard coding the string.

ResourceManager resmgr = new ResourceManager("MyResource", Assembly.GetExecutingAssembly());

private CultureInfo englishCulture = new CultureInfo("en-US");
private CultureInfo frenchCulture = new CultureInfo("fr-FR");
private CultureInfo spanishCulture = new CultureInfo("es-ES");

Then to get a string, call GetString() with a key and a culture

string msg = resmgr.GetString("FormLabel.Connected",frenchCulture);
string msg = resmgr.GetString("FormLabel.NotConnected", frenchCulture);

You are probably managing your culture already, so you would utilize that, then you just have to specify what key to use to get the right value from the resource file.

NerdFury