Multiple Language Support in .Net WinForm/WPF applications
In this article I will talk about multiple language support in .Net Windows Forms or WPF applications.
Multiple Language Support in .Net
We all need sometimes multiple language support when we develop application in both Windows Forms or WPF windows. We want all controls of our application appear in a specific language when we click on just one button.
Today we are gonna talk about that and you are gonna learn how to do it. You can apply the way Im gonna tell you in both Windows Forms applications and WPF applications. Now we are gonna develop a simple WPF application. The application will support two languages: English and Turkish.
First of all, we design user interface in a randomly preferred language. My application’s interface is a simple Login window and looks like below:
There are four TextBlock controls(Labels in WinForms). Two of them are located in top right corner of the window are named as lblTurkish and lblEnglish. The other two TextBlock controls are named as lblUsername and lblPassword. And the Button has the name btnLogin.
After we have finished the design part, we can create aour Resource files which is the main point of our application. We add a folder named Resources to our project, and inside it we create two resource file with .resx extension. We name them as Res.En and Res.Tr
We open the Res.En file and then add the tags for controls to be translated into desired languages. And give values to corresponding tag in English:
With same tags, we set values of controls to be translated in Turkish in our Res.Tr file:
Since our design and resource files are ready we can pass through to the coding part. Our variables are below:
ResourceManager variable implies that which resource to be used. CultureInfo variable implies which language to be used. isLanguageTurkish variable implies if the language is set to Turkish.
Our method(SetLanguage) that sets the language of user interface looks like below:
If Turkish language is selected by user Turkish CultureInfo instance is initialized, else (if isLanguageTurkish is false) English CultureInfo instance is initialized. And then giving the tags from the resource files that we have already created we get the values that match these tags and set Text or Content properties of our controls to be translated with these values.
We call SetLanguage method in Window_Loaded event after we define the default language and initialize an instance of ResourceManager class . Window_Loaded event look like below:
We set isLanguageTurkish varible’s value to true so that we imply that the default language of our application is Turkish. You can change it if you would like to.
In WPF most of the controls except Button don’t have Click event so we use the PreviewMouseUp event which does almost the same job.
lblTurkish and lblEnglish TextBlocks’ PreviewMouseUp events look like below:
In these events we imply if the language of user interface is Turkish and call SetLanguage method to set the application’s language to desired language.
User interfaces of our program after clicking on lblTurkish and lblEnglish TextBlocks look like below:
I hope you’ll find this article useful.