-1

My question is how to change language without repeating this code when I need to change my language. Here are two buttons for Bulgarian and English language. I need to add one more language French how to add it without repeating the code...

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);
}    
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Liverpool
  • 265
  • 7
  • 21
  • Take a look at your code. The only difference in those methods is in the first line which in one used `bg-BG` and in other one used `en-US`, so you can simply encapsulate the code in a method `void Localize(string culture){ CultureInfo ci = new CultureInfo(culture); .....}` and call it using Localize("bg-BG"); or any other culture you need. – Reza Aghaei Aug 01 '16 at 22:48
  • Also you can use [localization](https://msdn.microsoft.com/en-us/library/y99d1cd3(v=vs.100).aspx) feature of form. – Reza Aghaei Aug 01 '16 at 22:50
  • Also as a good approach to change language at runtime without closing and reopening a form also having design-time support, take a look at [this post](http://stackoverflow.com/a/33948879/3110834). In that post you can see an extender control which allows you to set text for controls at design-time and then at run-time lets you to switch between different cultures without closing an reopening the form. – Reza Aghaei Aug 01 '16 at 22:58

3 Answers3

0

you can use the resourcemanager. see msdn documentation https://msdn.microsoft.com/en-us/library/y99d1cd3(v=vs.71).aspx

Dans
  • 216
  • 1
  • 7
0

This is not good approach to localize an application but in this case you can try to substract functionality to the new method:

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);
}

and call it from your buttons:

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"))  
}
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
  • it works for the furst language but for english i have i different resource manager: ResourceManager rm = newResourceManager("CoffeeShop.Lang.Languages", a) – Liverpool Aug 02 '16 at 09:01
  • You can pass `ResourceManager ` also as a parameter. Use `ApplyLanguage (CultureInfo ci, ResourceManager rm)` – Roman Marusyk Aug 02 '16 at 09:02
  • Thank you so much now i can change the text of the buttons,but i have one more problem,I have textbox which displays what i coffee i have selected,how to make textbox text change language? – Liverpool Aug 02 '16 at 09:34
0

Are you sure that you want to show all languages simultaneously? Or are you trying to have it show only the labels, buttons, of whatever-is-the-current language?

Most folks use the built in ability of Visual Studio to handle these tasks... just click on the form, select a new language, and edit the text. This will create separate resource files for each language for that form.

That way you don't have to change anything in your code... and it will automagically pick the correct language and show the correct labels, buttons, etc.

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

egray
  • 390
  • 1
  • 4