Article
One Community, One World
Developing a Localized Multi-Language Smart Device Applications
by Ahmed Waly  on Dec 17 2006 12:00AM
  PageViews 7879   |    Add to Fav   |     Send to Friend   |    Download Code
Description:
Learn how to develop a smart device application localized to your language, and how to use Resource files in your application.

Resources Used:
Microsoft Visual Studio 2005
.NET Compact Framework 2.0
.NET Compact Framework 2.0 SP1
Microsoft .NET Framework SDK v1.1
Microsoft ActiveSync 3.8 or later (Optional)
Localized Windows Mobile 5.0 Pocket PC Emulator Images

Introduction:
Many developers were asking on the forums and groups for help on localizing their own developed applications especially for the non-Native English speaking nations. That was what made me thinking on writing this article, to explain how to use this feature, specially that I had this problem before, and managed to solve it and use localization in my .NET CF 1.0 application to support Arabic UI (My Native Language :D) and other languages like Indian, Persian, French, beside the default English language UI.

How It Works?
We have several ways to make multiple languages UI application. Here I'll explain some of them... First way, is the localization, which depends on enabling the localization of the forms, and adding new languages, and changing on the UI text for each language. And when you want to change the UI language of the application, you will have to change the language settings on the device's Regional settings to the supported language. Other way, is by using a resource file, in which you store on it a string values, images, and other objects, and you allow an option in your application to change the UI Language and depending on the selected language, you load the related objects from the resource file. Here on this article, I'll explain how to make both ways, with sample code.

Let's Start...
Now, we are going to make a simple application showing how to support multiple languages in you application UI and how to use resource files too. Start visual studio 2005, and create new smart device project (PPC2003 or PPC'05 what ever you need). Set Form1 localizable property to true, and make the language to default. Add labels as in the figure and write any text with English language.  


Figure 1: Setting form to localizable

Now, Choose French (Just as example). Take care that choosing a language for a certain country will differ from other country. I mean if you selected for example French (Canada), then you must change the Regional settings on the device to French (Canada) too, and it will not work if you selected French (France) for example, but selecting just French as the Form language from the beginning, will work for any French (x-Country).  


Figure 2: Changing the form's language

Now, change the label's text to a French version of this text like in figure 3. 


Figure 3: Changing UI in the new language

You will notice a new resource file added to the form automatically Called Form1.fr.resx to hold the new language settings, strings, and objects

Now you are done, run your application and set its regional settings language to any language than the French, you will notice that the app Form1 displays the English text, because it was set as the default settings. 


Figure 4: Running the app in default language

Now change the device's Regional settings again to French Language and run the application, and you will notice that the form UI displays the text in French language. 


Figure 5: Running the app after changing language regional settings to French

This way is the localization way.

The Second Method is the Resource files way, but it requires that any used language must be supported on the Device/Emulator you are using; else it will display the characters as a squares or unrecognized marks.

If your language doesn't exist on the default emulator, you can download a localized emulator images for other languages from Microsoft website, you will find the link on the Resources section on this Article.

Now, add another windows form, call it Form2, and add a Resource file name it “Resource1”.


Figure 6: Adding Resource file and Form2 to project

Add menu items in both forms links to each other. On Form2, add controls like in figure 7  


Figure 7: adding controls to Form2

Now, on the Resource file, add two string values name the first "str_English", and set its value to what ever you want to display in English. And name the other "str_German" and set its value to the same text but in German language, as in figure 8.  


Figure 8: Adding string values to the Resource file

On the Form2 Code, add the following code to implement the Resource Manager object: 

C#
//On Using directives Section: 
using System.Reflection; 
using System.Resources; /

//On Global declaration Section:
//ResourceManager is used to access the Resource files. 
ResourceManager rm;

//On the constructor Section:
public Form2()     
    {        
         InitializeComponent();          

        //Resource Manager , its parameters are BaseName which is          
        //"AppNameSpace.ResourceFileName", and The Assembly Object.        
         rm = new ResourceManager("LocalizationDemo.Resource1", Assembly.GetExecutingAssembly());     
    } 



Notice that the Basename parameter of the resource manager object must be in this form: "ApplicationNameSpace.ResourceFileName" e.g.: "LocalizationDemo.Resource1", and if you placed the resource file on a folder in the project, include the folder name in the basename too as:

"ApplicationNameSpace.FolderName.ResourceFileName" e.g.:"LocalizationDemo.ReourcesFolder.Resource1".

On each radio button's "Checked Changed" event handler, add the following code to load the string objects from the Resource file:  

C#
private void radio_English_CheckedChanged(object sender, EventArgs e)         
    {              
        if (radio_English.Checked == true)                    
            {                            
                lbl_Title.Text = "English Language";                           
                lbl_Message.Text = rm.GetString("str_English");                     
            }              
        else if (radio_German.Checked == true)                      
            {                           
                lbl_Title.Text = "German Language";                            
                lbl_Message.Text = rm.GetString("str_German");                       
            }        
    } 

private void radio_German_CheckedChanged(object sender, EventArgs e)         
    {                
        if (radio_English.Checked == true)                        
            {                              
                lbl_Title.Text = "English Language";                              
                lbl_Message.Text = rm.GetString("str_English");                         
            }                 
        else if (radio_German.Checked == true)                         
            {                               
                lbl_Title.Text = "German Language";                               
                lbl_Message.Text = rm.GetString("str_German");                          
            }          
    } 



Now, run the application, and depending on which language radio button the user select, it will load the related string object from the resource file, so when changing the selection, the labels will change too to the selected language. 


Figure 9: After Selecting English radio button


Figure 10: After Selecting German radio button

Other important Notes:
  • Unlike The culture Info Class of the .NET framework, the .NET compact framework doesn't support changing the CurrentUICulture; it gets it only, and doesn't allow setting it.
  • You can get the Current Culture and use it if you want to know which language set in the device now, to know what string to load from resource files like in the previous sample like this:  

C#
//Using directives 
using System.Globalization; 
//This will return the code of the selected culture (language), 
//e.g.: “en” for English language.
CultureInfo.CurrentCulture.Parent.Name 



That's it, End of the lesson.

Conclusion:

You can support multiple languages in your application's UI by either using Resource files, or using localization, or both ways together.  

Comments