Localization - Step by Step Approach

Last post 01-08-2008 11:58 PM by sameer_iworktech. 3 replies.

Sort Posts:

  • Cool [cool] Localization - Step by Step Approach

    06-06-2007, 8:27 AM
    • Loading...
    • anandh_efi
    • Joined on 10-18-2006, 5:51 AM
    • Bangalore
    • Posts 5
     

    You can implement localization either by placing the resource file (i.e. .resx file) in App_GlobalResources folder or by placing the .resx files under a new project. Here is the step by step method for implementing it.

     Implementation using “App_GlobalResources”:  
    1. Open visual studio 2005. click File->Add->New Website
    2. In the default.aspx, add a new label and let its Id be “MyLabel”.
    3. now right click the website project ( not the website solution)->Add new Item
    4. In the pop up window select Resource file and name it as LocalString.resx and click the “ADD” button
    5. As soon as you click, an alert pop up will be displayed saying that the resource file should be placed in App_globalresource folder.
    6. Click YES.
    7. Now let’s add the French resource file.
    8. Right click App_globalresource and select add new item
    9. In the pop up window select resource file and rename it to LocalString.fr.resx(make sure that the name “LocalString (or the name which you have given)” is same for all the resource file. This naming is very important.
    10. Similarly you can add resource file for other languages.
    11. Now right click the localString.resx and open it with XML editor.
    12. At the bottom of the xml before <root/> tag add the below mentioned tag

     <data name ="hello">

    <value>Hello</value>

    </data>

    1.  Similarly right click the localString.fr.resx and open it with XML editor.
    2. Right click the Website ->Add New Item->Class.
    3. Name the class as MyClass
    4. The class should contain the below mentioned code.

      using System;
     using System.Data;
     using System.Configuration;
     using System.Web;
    using System.Web.Security;
     using System.Web.UI; 
     using System.Web.UI.WebControls;
      using System.Web.UI.WebControls.WebParts;

    using System.Web.UI.HtmlControls;
       using System.Resources;  
       using System.Reflection;
         /// <summary> 
       /// Summary description for Class1
        /// </summary>     
    namespace MyResource
       {   
      public class MyClass 
      {   
        private static ResourceManager RM; 
           public static void initialise()  
         {       
        RM = new ResourceManager("Resources.LocalString",       Assembly.Load("App_GlobalResources"));
        }        
    public static string Getstring(string key)
            { 
              return RM.GetString(key); 
             }   
      }

      } 

    1.   The function initialize ( ) is used to initialize the resourcemanager class. The naming of the parameter that you pass while creating the RM object is very important. If the naming convention is not proper then you will get “System.Resources.MissingManifestResourceException”.
    2. The first parameter that we pass to resource manager constructor should be named like “Resources.the_name_of_the_resourcefile_without_.resx_extension” . here my resource file name is LocalString.therefore I have passed the argument as "Resources.LocalString"
    3. The second parameter Assembly.Load("App_GlobalResources")); loads the corresponding sattelite assembly.

                     Note: Do not rename the App_GlobalResources folder name

     

      Folder Structure:
      • E:\WebSite
      • ·    App_Code
      •  o       MyClass.cs
      • ·        App_Data
      • ·        App_Globalresources
      •   o       LocalString.fr.resx
      •   o       LocalString.resx
      • ·        Bin
      • ·        Default.aspx
      •                o       Default.aspx.cs
      • ·        Global.asax
      • ·        Web.Config
    1. In the Default.aspx.cs page insert this code

                MyLabel.Text = MyResource.MyClass.Getstring("hello");

    1. The resource manager should be intialised before the application starts.
    2. this can be done by right click website-> Global Application Class->add
    3. In the Global.asax,inside the function Application_Start( ),insert the code MyResource.MyClass.initialise();
    4. Now just run the application.you can see hello getting displayed in the browser.
    5. In the web.config insert <globalization uiCulture="auto"/>. This is very important.if you don’t specify this always hello will be displayed no matter what language you select in the browser.
     
    1. change the browser language to fr(French) .this can be done by(for IE 6.0)  tools->internet option->language->add ->French(france)[fr]->move up. Make sure it is at the top.
    2. Refresh the page. you can see “Bon Jour”
    Implementation using a separate project:     

      So what does  Implementation using a separate project: “ means? Here instead of placing the resource(.resx) file in App_GlobalResources,it will be placed in a separate folder.The folder structure will be

      •  E:\WebSite
      • ·        App_Code
      •   o       MyClass.cs
      • ·        App_Data
      • ·        App_Globalresources
      •   o       LocalString.fr.resx
      •   o       LocalString.resx
      • ·        Bin
      • ·        Default.aspx
      •                o       Default.aspx.cs
      • ·        Global.asax
      • ·        Web.ConfigStrings
      • ·        Properties
      • ·        References
      • ·        LocalizedStrings.fr.resx
      • ·        LocalizedStrings.resx
      • o       LocalizedStrings.Designer.cs
     
    1. We will modify the above website project so that the resource file will be placed in a separate project.
    2. Right click the solution->add->New Project
    3. Name the Project as Strings
    4. Add the default and French resource file in the same manner as we did earlier.
    5. In the Myclass.cs we should change the parameter.
    6. In the initialize method, rewrite the old code with
                               RM = new ResourceManager("Strings.LocalizedStrings",               

                  Assembly.Load("Strings"));

    1. The first parameter should be RootFolder_Containing_resourceFile.the_name_of_the_resourcefile_without_.resx_extension. Here the root folder is Strings. Hence it is named as Strings.LocalizedStrings. 

     Folder Structure:

      • Strings <<<<< ROOT folder
      • ·        Properties
      • ·        References
      • ·        LocalizedStrings.fr.resx
      • ·        LocalizedStrings.resx
      •     o       LocalizedStrings.Designer.cs  
    1. The second parameter loads the respective satellite assemblies. Assembly.Load("Strings")); “strings” is the name of the root folder containing the resource file.
    2. Now run the application and you can see that localization has been implemented.  
     

     

     Happy Internationalization!!!! Yes

     

  • Re: Localization - Step by Step Approach

    06-06-2007, 9:17 AM
    Answer
    • Loading...
    • benderm
    • Joined on 01-16-2007, 8:47 AM
    • Denver, CO
    • Posts 285

    thanks for the brief tutorial! I found it informative. Thanks!



    Don't forget to click "Mark as Answer" on the post that helped you.
    This marks your thread as Resolved so we will all know you have been helped.
  • Re: Localization - Step by Step Approach

    06-06-2007, 12:02 PM
    • Loading...
    • anandh_efi
    • Joined on 10-18-2006, 5:51 AM
    • Bangalore
    • Posts 5

    thanks bendermSmile

  • Re: Localization - Step by Step Approach

    01-08-2008, 11:58 PM

    Hi,

              Thank for your such detail help. It really help me in development.

    I have query abt how i use resource file for Indian languages?

    I created resource file for Hindi-India but i can't enter 'String Value' in that in Hindi characters. Do i need special editior for this?

     

    Thanks.

    Sameer

Page 1 of 1 (4 items)