Master C# Logo banner
Welcome to MasterCSharp.com - Master C#, the easy way... - by Saurabh Nandu

 


Localization of Windows Forms in C# with VS.NET

Add Comment
 

 
<div align="center"> <table cellpadding="3" width="75%" class="outline" cellspacing="2"> <tr> <td width="50%" class="outline"><b>Download File</b></td> <td width="50%" class="outline"><b>SDK</b></td> </tr> <tr> <td width="50%" class="outline"> <a class="wbox" href="../../file/localizationdemo.zip"> localizationdemo.zip</a> (30k)</td> <td width="50%" class="outline">v1.0.3705</td> </tr> </table> </div> <p><span class=wboxheado>Introduction</span><br> Creating applications that are ready for the international market has become a key tenant in any&nbsp; application today. The need is to create applications that can cater to various different countries, cultures as well as reduce the cost of developing and debugging separate applications for each country. Creating applications that are ready to cater to different countries of the world is called as <b> Internationalization</b>. <p> <i>Internationalization</i> has two separate issues, which are often very confusing for developers to understand. These issues are <i> Globalization</i> and <i>Localization</i>. <br> In layman’s terms :<br> <b>Globalization</b> refers to creating applications where the currency format, date format, numerical system might differ. For example, the currency symbol for USA Dollar is $ while the currency symbol for UK Pound is £, hence your application supporting globalization should be able to automatically pickup the correct format from the users operating system and use it.<br> <b>Localization</b> refers to creating applications where the screen text, strings, pictures might differ from language to language. (Please note I am not referring to programming languages here, but spoken languages! )<p>In this short article I will cover the creation of a simple “Localized” Windows Forms application. So as you might have already guessed, it will deal with creating a Windows Forms application that displays differently in different languages.<br> Since my knowledge of foreign languages except English is Zero, I will be working with Hindi (I am not good at typing this too..) and English !!<p> <span class=wboxheado>Prerequisites</span><br> 1) VS.NET v7.0.9466 (.NET SDK v1.0.3705)<br> 2) Windows 2000 (I guess admin access might be required..). <br> And please backup your stuff!!<br> 3) Basic understanding of C# and Windows Forms…<br> 4) If you do not have the necessary font's installed on your computer, parts of this article may look garbled! Only way out is to install the necessary fonts.<p> <span class=wboxheado>Setting-up Windows 2000 to support Hindi Language</span><br> One of the good features of Windows 2000 is that you can work with multiple languages from within the same OS. But by default not all languages get installed (to save space and startup time..) so you will have to first install the Hindi language fonts if they are not already installed.<br> <br> Go to <i>Start menu -&gt; Control Panel -&gt; Regional Options</i> to start the <b>Regional Options</b> dialog (Figure 1).<p align="center"><br> <img border="0" src="../../img/localozationofwinforms1.gif" width="406" height="479"><br> <b>Figure 1:</b> Regional Options Dialog </p> <br> In the <i>Regional Options dialog's</i> <b>General </b> <i>tab</i>, check if the <b>Indic</b> language is selected; if not, select it and click <b>OK</b>. This will install the necessary Indian fonts for Hindi, Marathi, Gujarati, Konkani, Tamil etc. This step might ask you for the Windows 2000 Installation CD, so keep it handy, and it might also ask for a reboot.<br> <br> Once the machine reboots and the necessary font’s are installed again start the same <i>Regional Options </i>dialog, and go to <b>Input Locales</b> <i>tab</i> and click <b>Change</b>. The <b>Text Services</b> dialog that shows, will allow you to select keyboard settings to use. Click <b>Add</b> to add a new <i>Keyboard Layout</i>, from the <b>Add Input Language</b> dialog, select <i>Hindi</i> and click <i>OK</i>.<p align="center"> <img border="0" src="../../img/localozationofwinforms2.gif" width="405" height="479"><br> <b>Figure 2:</b> Text Services Dialog</p>Figure 2, shows the <i>Text Services</i> dialog updated after I have added the Hindi keyboard. Here you can setup the <b>Language Bar</b>, and <b>Key Settings</b> so that you can easily shift between Hindi and English keyboards, but this is clearly out of the scope of this article, so explore this stuff yourself. <br> Also you will find that the <i>Default Input Language</i> dialog let’s you select the default language for your keyboard. Later in this article I will ask you to shift from English to Hindi keyboard layout’s, this is the place you do that, I will not be mentioning it again!!<p> <span class=wboxheado>How does .NET Handle Localization??</span><br> .NET applications rely on Resource files (*.resx) to deal with Localization. The logic is simple, every bit of resource like label string, menu string, pictures, error messages, control size and location, etc are stored in these XML formatted resource files. Hence for each different language your application will support you create a new resource file and place the relevant translation (translation to be done by a human translator) of the strings and pictures into the resource files. At runtime the appropriate resource file is selected and the resources are used from it to display the necessary UI, making your single application localized.<br> .NET follows a consistent naming pattern for resource files so that the correct resource file is selected. The default resource file is saved as &lt;ClassName&gt;.resx. (Where ClassName is the name of the class that host’s the <i>Main</i> method to start the application.) Now for other languages you have to utilize the <i>Culture Name</i> for that language. MSDN documentation states that, “<i>The culture names follow the RFC 1766 standard in the format &quot;&lt;languagecode2&gt;-&lt;country/regioncode2&gt;&quot;, where &lt;languagecode2&gt; is a lowercase two-letter code derived from ISO 639-1 and &lt;country/regioncode2&gt; is an uppercase two-letter code derived from ISO 3166.</i>”<br> Hence our Hindi resource file will be named &lt;ClassName&gt;.hi.resX. (Where hi is the culture name for Hindi ).<br> If you have the VS.NET Documentation installed check out the following link to get a full list of Culture Names.<br> [ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemGlobalizationCultureInfoClassTopic.htm ]</p> <p> <span class=wboxheado>Let’s Kick some Code!</span><br> Finally, we start writing our application!! I am assuming that you are familiar with VS.NET, and so I will not be describing small little steps… The source code is attached, you can refer to it if you have doubts!<br> <br> 1) Start VS.Net and create a new <b>Windows Application</b> project, call it as <b>LocalizationDemo</b>.<br> <br> 2) Drag 1 GroupBox, 2 RadioButtons and 1 Button on to the Designer, and make the Form look like figure 3. </p> <p align="center"> <img border="0" src="../../img/localozationofwinforms3.gif" width="316" height="340"><br> <b>Figure 3: </b>Form Design</p> 4) Now, right-click the <i>LocalizationDemo</i> Project in the <i>Solution Explorer</i> and add a <b>New Windows Form</b> with the name <i>Form2.cs</i>.<br> <br> 5) Add a Label, PictureBox and a Button on Form2 as shown in figure 4. I have set the PictureBox to show a custom bitmap (English.bmp) I have created.<p align="center"> <img border="0" src="../../img/localozationofwinforms4.gif" width="316" height="337"><br> <b>Figure 4: </b>Form2 Designer</p> 6) Double-click the <i>Close</i> button in the Designer window so that we can write the EventHandler for the <i>Click</i> event of the button. In the event handler, we write nothing fancy but just a simply call the ‘Hide’ method on the form, as shown below. <br> &nbsp;<table cellpadding="0" cellspacing="0" width="100%" class="code"> <tr> <td ><br> this.Hide();<br> &nbsp;</td> </tr> </table> <p> 7) Switch back to the Form2 Designer window and select the form in the Designer; and from the <i>Properties</i> window set the <b>Localizable </b>property of this form to true, indicating that this form supports <i>Localization</i>.<br> <br> 8) While Form2 is selected in the Designer window, from the <i>Properties</i> window, change the <b>Language</b> property from (default) to Hindi. This will prompt VS.Net to add another Resource file in the project with the name <i>Form2.hi.resx</i>. (I hope you remember why the particular naming style is adopted !!). <br> Please remember that all the changes that you make from here on to the components property will only get reflected while the user is viewing the application with the culture Hindi. In case you have added some new controls, you will have to switch back to the <i>Default Language</i> property of the Form and again make the changes in the default language also. This way you have to keep switching between the different languages you support in you applications and make the necessary UI changes to support all the different languages your application supports.<br> <br> 9) Now time to code in Hindi!! Go to <i>Regional Options</i> and change the default Keyboard to Hindi (I hope you remember how to do this!!). <br> <br> 10) Many times on English version of VS.NET source code files are saved in W<b>estern European (Windows) - Codepage 1252</b> format, hence if you try to directly enter Unicode Characters in <i>Properties</i> window it does not work correctly. Hence switch to <i>code view</i> for Form2 and then from <i>File menu -&gt; Advanced Save Options</i> dialog change the <b>Encoding</b> to <b>Unicode (UTF-8 with Signature) - Codepage 65001</b> and click <i>OK</i>. This step will ensure that in future the source code file is saved in UTF-8 format.<br> <br> 11) Switch back to Design view and change the <b>Text</b> property of the Label to reflect &#2344;&#2350;&#2360;&#2381;&#2340;&#2375; (Hello in Hindi) and also change the <b>Font</b> to <b>Mangal</b>. Then change the <b>Text</b> property of the button to &#2348;&#2306;&#2342; (Close in Hindi). Lastly, I have also changed to PictureBox to show another picture. The changes shown look like figure 5.</p> <p align="center"> <img border="0" src="../../img/localozationofwinforms5.gif" width="316" height="342"><br> <b>Figure 5: </b>Hindi translation of Form2</p> And if you are wondering how I wrote &#2344;&#2350;&#2360;&#2381;&#2340;&#2375; and &#2348;&#2306;&#2342;, well with a little bit of practice you too will get away with it, but I will give it out this time, use the following keystrokes to make the word:<br> &#2344;&#2350;&#2360;&#2381;&#2340;&#2375; - vcmdls<br> &#2348;&#2306;&#2342; – yxo<br> <br> 12) Go back to the <i>Regional Options</i> dialog and re-set the default keyboard to <i>English</i> so that we can resume with our coding in English! If you forget this step, then you might start getting varying response from application that are not designed to support Localization!! You might even have to restart VS.NET, if it still keeps typing in Hindi!<br> <br> 13) Close the Form2 designer, and open the Form1 in the VS.NET designer window. Once the Form1 is open in the Designer window, double-click on the <i>Start Application</i> button so that we can write the Event Handler for the <i>Click</i> event of the button.<br> <br> 14) The code snip below is the EventHandler of the <i>Start Application</i> button’s <i>Click</i> event. Here, depending on which RadioButton is checked, I create an instance of the <b>CultureInfo</b> class and assign it to the <b>CurrentUICulture </b>property of the current thread.<br> This will make the necessary switch between the different UI’s. <br> <br> &nbsp;<table width="100%" class="code"> <tr> <td width="100%"><pre>private void button1_Click(object sender, System.EventArgs e) {<br> if( this.radioButton1.Checked )<br> {<br> <span class=cmt>//Get the Default installed culture</span><br> System.Threading.Thread.CurrentThread.CurrentUICulture = <br> System.Globalization.CultureInfo.InstalledUICulture ; <br> }<br> else if( this.radioButton2.Checked )<br> {<br> <span class=cmt>//Get the Hindi culture</span><br> System.Threading.Thread.CurrentThread.CurrentUICulture = <br> new System.Globalization.CultureInfo(&quot;hi&quot;) ;<br> }<br> Form2 f2 = new Form2();<br> f2.Show();<br> }</pre></td> </tr> </table> <p> 14) That’s It !! Build your code and run it, if you followed the steps correctly the application should build and work correctly!!<p> <span class=wboxheado>Some Improvements</span><br> Here is a list of improvements you could make,<br> 1) Currently, I am asking the user to give the language preference every time he runs the application. It would be a better design if you would ask for the user’s language preference only once at install time, and then store the language preference into the application configuration file.<br> 2) This applications takes it for granted that the default language of the user is English, in your application you should not make this assumption, instead you should build an additional English (en) resource file separately an not rely on the default resource file.<p> <span class=wboxheado>Conclusion</span><br> .NET has provided an extensive platform that support Internationalization. VS.NET harnesses this and helps you easily create applications supporting Internationalizations.<br> This article showed you one way of working with Localization through VS.NET.

Comments

Add Comment