Localization Of Windows Forms In C

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Localization Of Windows Forms In C as PDF for free.

More details

  • Words: 2,405
  • Pages: 10
Localization of Windows Forms in C# with VS.NET

Introduction Creating applications that are ready for the international market has become a key tenant in any 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 Internationalization. Internationalization has two separate issues, which are often very confusing for developers to understand. These issues are Globalization and Localization. In layman’s terms : Globalization 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. Localization 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! ) 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. 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 !!

Prerequisites 1) VS.NET v7.0.9466 (.NET SDK v1.0.3705) 2) Windows 2000 (I guess admin access might be required..). And please backup your stuff!! 3) Basic understanding of C# and Windows Forms… 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.

Setting-up Windows 2000 to support Hindi Language 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. Go to Start menu -> Control Panel -> Regional Options to start the Regional Options dialog (Figure 1).

Figure 1: Regional Options Dialog In the Regional Options dialog's General tab, check if the Indic language is selected; if not, select it and click OK. 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. Once the machine reboots and the necessary font’s are installed again start the same Regional Options dialog, and go to Input Locales tab and click Change. The Text Services dialog that shows, will allow you to select keyboard settings to use. Click Add to add a new Keyboard Layout, from the Add Input Language dialog, select Hindi and click OK.

Figure 2: Text Services Dialog Figure 2, shows the Text Services dialog updated after I have added the Hindi keyboard. Here you can setup the Language Bar, and Key Settings 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. Also you will find that the Default Input Language 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!!

How does .NET Handle Localization?? .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. .NET follows a consistent naming pattern for resource files so that the correct resource file is selected. The default resource file is saved as .resx. (Where ClassName is the name of the class that host’s the Main method to start the application.) Now for other

languages you have to utilize the Culture Name for that language. MSDN documentation states that, “The culture names follow the RFC 1766 standard in the format "-", where is a lowercase twoletter code derived from ISO 639-1 and is an uppercase two-letter code derived from ISO 3166.” Hence our Hindi resource file will be named .hi.resX. (Where hi is the culture name for Hindi ). If you have the VS.NET Documentation installed check out the following link to get a full list of Culture Names. [mshelp://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemGlobalizationCultureInfoClassTopic.htm ]

Let’s Kick some Code! 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! 1) Start VS.Net and create a new Windows Application project, call it as LocalizationDemo. 2) Drag 1 GroupBox, 2 RadioButtons and 1 Button on to the Designer, and make the Form look like figure 3.

Figure 3: Form Design 4) Now, right-click the LocalizationDemo Project in the Solution Explorer and add a New Windows Form with the name Form2.cs.

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.

Figure 4: Form2 Designer 6) Double-click the Close button in the Designer window so that we can write the EventHandler for the Click 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. this.Hide();

7) Switch back to the Form2 Designer window and select the form in the Designer; and from the Properties window set the Localizable property of this form to true, indicating that this form supports Localization. 8) While Form2 is selected in the Designer window, from the Properties window, change the Language property from (default) to Hindi. This will prompt VS.Net to add another Resource file in the project with the name Form2.hi.resx. (I hope you remember why the particular naming style is adopted !!). 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 Default Language 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. 9) Now time to code in Hindi!! Go to Regional Options and change the default Keyboard to Hindi (I hope you remember how to do this!!).

10) Many times on English version of VS.NET source code files are saved in Western European (Windows) - Codepage 1252 format, hence if you try to directly enter Unicode Characters in Properties window it does not work correctly. Hence switch to code view for Form2 and then from File menu -> Advanced Save Options dialog change the Encoding to Unicode (UTF-8 with Signature) - Codepage 65001 and click OK. This step will ensure that in future the source code file is saved in UTF-8 format. 11) Switch back to Design view and change the Text property of the Label to reflect नमस्ते (Hello in Hindi) and also change the Font to Mangal. Then change the Text property of the button to बंद (Close in Hindi). Lastly, I have also changed to PictureBox to show another picture. The changes shown look like figure 5.

Figure 5: Hindi translation of Form2 And if you are wondering how I wrote नमस्ते and बंद, 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: नमस्ते - vcmdls बंद – yxo 12) Go back to the Regional Options dialog and re-set the default keyboard to English 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! 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 Start Application button so that we can write the Event Handler for the Click event of the button.

14) The code snip below is the EventHandler of the Start Application button’s Click event. Here, depending on which RadioButton is checked, I create an instance of the CultureInfo class and assign it to the CurrentUICulture property of the current thread. This will make the necessary switch between the different UI’s. private void button1_Click(object sender, System.EventArgs e) { if( this.radioButton1.Checked ) { //Get the Default installed culture System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.InstalledUICulture ; } else if( this.radioButton2.Checked ) { //Get the Hindi culture System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("hi") ; } Form2 f2 = new Form2(); f2.Show(); }

14) That’s It !! Build your code and run it, if you followed the steps correctly the application should build and work correctly!!

Some Improvements Here is a list of improvements you could make, 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. 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.

Conclusion .NET has provided an extensive platform that support Internationalization. VS.NET harnesses this and helps you easily create applications supporting Internationalizations. This article showed you one way of working with Localization through VS.NET.

Saurabh Nandu - 16 June 2002 Your Ratings / Comments

Viewer Ratings/Comments

Rating 5 5 4 1 4

Just Comments

1 Just Comments

5

4 5 4 3

5

Just Comments Just Comments 5 5

5

Description fdsg - ewq on 6/24/2002 10:44:00 AM Excellent. Your writing skill has definitely improved Saurabh. Very clearly written article Sai on 7/2/2002 12:54:00 PM Nicely done. - Tokunboh Odusami on 7/14/2002 3:32:00 AM - gb on 8/11/2002 10:07:00 AM Nice demonstration - xi on 9/25/2002 7:50:00 AM could you please email me with an answer to the following questions if you have them. FIrstly how do you localise the "canned" dialogs provided in .NET (for example the standard print dialog) Secondly when you distribute your app do you see the installation oif the ruhntime in the correct language ? cheers Richard - richard bradwell on 10/22/2002 3:51:00 PM Excellent , funtastic done. One question.... The applcaion as you said requires the support to hindi language from microsoft. Providing the microsft support just includes installing the fonts. I tried with different fonts, but it doesn't work. Why? - chandan on 10/24/2002 1:27:00 AM no - mohammad on 10/28/2002 9:17:00 AM Excellent! I used your code to help me in my work. But I have a question. This is possible to create a form and switch language and not creating a new form each time? Here is my problem, I created a bilingual application and I add the possibility to switch between english and french. When I click on the button to switch language, it's create a new form. Do you know how I can display only one form at the time? Thanks! :) - Nathalie on 11/5/2002 11:23:00 AM - Denny on 11/16/2002 8:24:00 AM This is fantastic article describing every step in detail. Precautions and references are also given. It gives neat example with which nay one can start building. Great - balachandar on 11/20/2002 9:14:00 AM Not to bad at all. Just wondering is that all there is to it? Are there any other techniques that would be usefull. - Alex Busu on 1/10/2003 10:59:00 PM How to change the language in the Windows Xp as there is no option like that in the W2K Zeeshan on 1/23/2003 8:40:00 AM Your site is the only one I found that explained that I needed to do the translation myself. Hence knowing multiple languages and changing my keyboard settings etc. Microsoft discussed all the fluff but never said you must hand type your resource files in the language of choice. Thanks a bunch. - Audean on 1/30/2003 10:40:00 AM i am font designer who run on linux mangal font - karambir singh Rohilla on 2/11/2003 5:08:00 AM hello - abc on 3/5/2003 2:19:00 PM - ali on 3/29/2003 5:34:00 AM Why sai is maraving you - santy on 5/10/2003 11:16:00 AM to Nathalie - it is possible to do what you want. See http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=211 to change language in current form, I did (as he recommended) 1) you need a resource manager variable in the form class private ResourceManager m_ResourceManager = new ResourceManager("TabbedWindow.Form1", System.Reflection.Assembly.GetExecutingAssembly()); 2) then in the event handler, you need to set the Culture, and re-get the strings of the particular elements you want to change (I changed some tabs in mine) ex: //Get the French culture System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-FR") ; this.tabPage1.Text = m_ResourceManager.GetString("tabPage1.Text"); this.tabPage2.Text = m_ResourceManager.GetString("tabPage2.Text"); this.tabPage3.Text = m_ResourceManager.GetString("tabPage3.Text"); this.tabPage4.Text = m_ResourceManager.GetString("tabPage4.Text"); - Todd Harvey on 5/31/2003 7:25:00 PM Its just excellant. Though I have a question here, how do you manage the database driven application. Is this conversion meant just for the static fields like labels ,etc not for the user

Related Documents