我的問題是當我需要更改我的語言時,如何在不重複此代碼的情況下更改語言。這裏有兩個保加利亞語和英語的按鈕。我需要添加一個語言法語如何添加它,而無需重複代碼...在Windows窗體中更改語言運行時
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);
}
看看你的代碼。這些方法的唯一區別是在第一行中,在一個使用'bg-BG',另一個使用'en-US',所以你可以簡單地將代碼封裝在一個方法'void Localize(string culture){CultureInfo ci =新CultureInfo(文化); .........'並用Localize(「bg-BG」)調用它;或您需要的任何其他文化。 –
也可以使用[本地化](https://msdn.microsoft.com/en-us/library/y99d1cd3(v = vs.100).aspx)窗體的功能。 –
此外,作爲在運行時更改語言而不關閉並重新打開同時具有設計時支持的窗體的好方法,請查看[本文](http://stackoverflow.com/a/33948879/3110834)。在該文章中,您可以看到一個擴展控件,它允許您在設計時爲控件設置文本,然後在運行時允許您在不關閉重新打開窗體的情況下在不同文化之間切換。 –