在Windows窗体中更改语言运行时

时间:2016-08-01 22:40:23

标签: c# winforms

我的问题是当我需要更改语言时如何更改语言而不重复此代码。以下是保加利亚语和英语的两个按钮。我需要添加一种语言法语如何添加它而不重复代码...

private void BtnBg_Click(object sender, EventArgs e)
{
    CultureInfo ci = new CultureInfo("bg-BG");
    Assembly a = Assembly.Load("CoffeeShop");
    ResourceManager rm = new ResourceManager("CoffeeShop.Languages.Languages", a);
    BtnCapuchino.Text = rm.GetString("Cappucino", ci);
    BtnCinnamon.Text = rm.GetString("Cinnamon", ci);
    BtnEspresso.Text = rm.GetString("Espresso", ci);
    BtnDecaffeinedCoffee.Text = rm.GetString("DecaffeinedCoffee", ci);
    BtnMilk.Text = rm.GetString("Milk", ci);
    BtnSugar.Text = rm.GetString("Sugar", ci);
    BtnBack.Text = rm.GetString("Clear", ci);
    Bulgarian.Text = rm.GetString("LanguageBulgarian", ci);
    textBox1.Text = rm.GetString("Bill", ci);
    CoffeeShop.ActiveForm.Text = rm.GetString("CoffeeShop", ci);
    BtnBuy.Text = rm.GetString("Buy", ci);
    ShowInformation(this, null);
}

private void BtnEng_Click(object sender, EventArgs e)
{
    CultureInfo ci = new CultureInfo("en-US");
    Assembly a = Assembly.Load("CoffeeShop");
    ResourceManager rm = new ResourceManager("CoffeeShop.Languages.Lang", a);
    BtnCapuchino.Text = rm.GetString("Cappucino", ci);
    BtnCinnamon.Text = rm.GetString("Cinnamon", ci);
    BtnEspresso.Text = rm.GetString("Espresso", ci);
    BtnDecaffeinedCoffee.Text = rm.GetString("DecaffeinedCoffee", ci);
    BtnMilk.Text = rm.GetString("Milk", ci);
    BtnSugar.Text = rm.GetString("Sugar", ci);
    BtnBack.Text = rm.GetString("Clear", ci);
    Bulgarian.Text = rm.GetString("LanguageBulgarian", ci);
    textBox1.Text = rm.GetString("Bill", ci);
    CoffeeShop.ActiveForm.Text = rm.GetString("CoffeeShop", ci);
    Bulgarian.Text = rm.GetString("LanguageBulgarian", ci);
    BtnBuy.Text = rm.GetString("Buy", ci);
    ShowInformation(this, null);
}    

3 个答案:

答案 0 :(得分:0)

您可以使用资源管理器。 请参阅msdn文档 https://msdn.microsoft.com/en-us/library/y99d1cd3(v=vs.71).aspx

答案 1 :(得分:0)

这不是本地化应用程序的好方法,但在这种情况下,您可以尝试将功能减去新方法:

private void ApplyLanguage (CultureInfo ci)
{
    Assembly a = Assembly.Load("CoffeeShop");
    ResourceManager rm = new ResourceManager("CoffeeShop.Languages.Lang", a);
    BtnCapuchino.Text = rm.GetString("Cappucino", ci);
    BtnCinnamon.Text = rm.GetString("Cinnamon", ci);
    BtnEspresso.Text = rm.GetString("Espresso", ci);
    BtnDecaffeinedCoffee.Text = rm.GetString("DecaffeinedCoffee", ci);
    BtnMilk.Text = rm.GetString("Milk", ci);
    BtnSugar.Text = rm.GetString("Sugar", ci);
    BtnBack.Text = rm.GetString("Clear", ci);
    Bulgarian.Text = rm.GetString("LanguageBulgarian", ci);
    textBox1.Text = rm.GetString("Bill", ci);
    CoffeeShop.ActiveForm.Text = rm.GetString("CoffeeShop", ci);
    Bulgarian.Text = rm.GetString("LanguageBulgarian", ci);
    BtnBuy.Text = rm.GetString("Buy", ci);
    ShowInformation(this, null);
}

并通过按钮调用它:

private void BtnBg_Click(object sender, EventArgs e)
{
  ApplyLanguage(new CultureInfo("bg-BG"))
}
private void BtnEng_Click(object sender, EventArgs e)
{
  ApplyLanguage(new CultureInfo("en-US"))  
}

答案 2 :(得分:0)

您确定要同时显示所有语言吗?或者你是否试图让它只显示当前语言的标签,按钮?

大多数人使用Visual Studio的内置功能来处理这些任务...只需单击表单,选择一种新语言,然后编辑文本即可。这将为该表单的每种语言创建单独的资源文件。

这样您就不必更改代码中的任何内容......它会自动选择正确的语言并显示正确的标签,按钮等。

https://msdn.microsoft.com/en-us/library/y99d1cd3(v=vs.100).aspx