In this C# .NET tutorial we will learn a easy way of adding language localization to your program.
We will be working with Windows Forms and xml files.
For this tutorial it is adviced to create a new project so you can easily follow and understand each step, without having to make big changes in any existing code.
Here is how it works:
In your windows form add a few labels and give them some text to be displayed (something other than label1, label2 etc)
After you have created the GUI, and added all the text to your form, you should name each label to something describing its text content.
If one label holds a welcome message, you could name it "welcome" without the quotes.
Do this for every label in your form.
In my form I chose to add two labels, "welcome" and "author"
In your windows form add a public static dictionary called LocalizedStrings:
public static Dictionary LocalizedStrings = new Dictionary();
This dictionary object takes a string as a key and a string as a value, this will all be explained soon.
Next we will create an xml file, you could name it whatever you like, in this tutorial however we will name it "language.xml".
The contents of the xml should look like this:
<labels>
<language type="english">
<welcome>Welcome to this application, I hope you like it!</welcome>
<author>This program was created by Jonas</author>
</language>
<language type="swedish">
<welcome>Välkommen till den här applikationen, jag hoppas du gillar den!</welcome>
<author>Det här programet skapades av Jonas</author>
</language>
</labels>
Next we need an xml reader class, here is a very basic one I wrote for this very purpose
using System;
using System.Linq;
using System.Xml.Linq;
using System.Windows.Forms;
using System.Collections.Generic;
public static class XmlReader
{
//we call this method every time we switch language
public static void RepopulateDictionary(Dictionary dictionary, string language)
{
dictionary.Clear(); //Clear our dictionary, we are repopulating it
var xml = from c in XElement.Load("language.xml").Elements("language") select c; //Load the xml and select the language elements
//Loop through the language elements
foreach (var Language in xml)
{
if (Language.Attribute("type").Value == language) //is this the element containing the language we are looking for?
{
foreach (XNode node in Language.Nodes())
{
string[] output;
char[] separators = new char[2]; //This will hold our string separators
//We need to remove the from the tag and split the tag from the content
separators[0] = '<';
separators[1] = '>';
string val = node.ToString(); //Obtain the entire element into a string
output = val.Split(separators); //Split the string using our separators and place it in our output array
//Here comes some of the magic, we add the tag as a key and the contents as a value to the dictionary
//Remember how we named our labels the same as the tags in the xml?
//Now each label can find their Text attribute by using their Name attribute as a key in the dictionary
dictionary.Add(output[1], output[2]);
}
}
}
}
//Here is the method used to retrieve all possible languages, so we can for example load them into a combobox
public static void GetLanguages(ComboBox comboBox)
{
var xml = from c in XElement.Load("language.xml").Elements("language") select c;
//For each language in the xml we add the name of it to the combobox so the program can know what languages are available
foreach (var language in xml)
{
comboBox.Items.Add(language.Attribute("type").Value);
}
}
}
Now we have all we need to load data from the xml.
Next up is to actually initiate these methods to load the correct strings into the labels
In your windows form, add a combobox and name it whatever you like. For this tutorial I left it at the default value, comboBox1.
In your windows form code, add this:
public Form1()
{
InitializeComponent();
comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
XmlReader.GetLanguages(comboBox1);
comboBox1.SelectedIndex = 0; //we do this to because in the SelectedIndexChanged event handler, we will repopulate the dictionary
//Finally we assign each label its proper value by calling this method found later in this tutorial
LoadLabels();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
XmlReader.RepopulateDictionary(LocalizedStrings, comboBox1.SelectedItem.ToString()); //We repopulate the dictionary with the value of the selected item
LoadLabels(); //we also need to load the values into our labels again
}
private void LoadLabels()
{
foreach (Control label in Controls) //Loop through each control in the form
{
if (label is Label) //we also need to make sure the control really is a label
{
label.Text = LocalizedStrings[label.Name]; //We set the text of the label by geting the value from the dictionary. We use the name of the label as the key to read from in the dictionary.
}
}
This concludes this tutorial on how to add localization to your programs, I hope you enjoyed it and found it useful.
There is alot of ways to expand these functions, but I'm sure you'll figure them out yourself.
If you find any errors please let me know!