• 多语言应用程序开发



                                      郑重声明:本文摘自www.codeproject.com. 版权归作者所有,如有违权请告之,本人将在一周内清除.若转载本站文章请标明出处




    源代码下载



    Globalization of Windows Application in 20 Minutes 

    Prelude

    There was once a time when multiple language support to a windows application used to be a three to six months call, but with the advent of .Net not anymore. Here is a 20 minutes crash course for globalization / Localization of a windows application. I think you'll find it as useful as I do. 

    Globalization in simple terms means enabling an application so that it works in different national, similar to how a global company operates in different countries. For a windows application globalization means, it is intended for worldwide distribution. There are two aspects of the Globalization:

    • Internationalization : Enabling the application to be used without language or culture barriers, i.e. language and cuture information comes from a resource rather than hard coded in the application  
    • Localization : Translating and enabling the product for a specific locale. Based on the resource file translating the application into that language and culture

    Target

    • Modular design: Code Once Use Everywhere (COUE), this is the prime feature which is needed when you globalize an application. All anyone should do is to convert any user interface output/message box /labels text etc, to something like  frmMain.RM.GetString("10001"), No culture information or resource manager initialization again in all the forms or anywhere else
    • Default language: Saving and retrieving the default language selected by the user in registry 
    • Features: How to take care of  date/time , multiple forms , images etc
    • Reusability: Minimum number of effor when you a dd a new form to the application
    • Extensibility: Support of multiple language, French, Spanish and English using resource files for each

    To hold your interest here is how it looks

     

    Time starts now

    The first thing we need will be three resource files for the three languages English, French and Spanish. I have used Google translate here^ to accompalish this

    1. English -file name resource.en-US.txt


    2. Spanish -file name resource.es-ES.txt


    3. French -file name resource.fr-FR.txt

    Using the Resource generator (Resgen) in the Visual Studio.Net 2003 Command Prompt we will create three resource files which an be understandable by the application, here is how

     

    We are done the resource files which will be used by the application will look something like this (below)  with extension .resources for each text files

     

    Put these three .resource files in a Resource folder inthe executable path

    Functionality

    First Run  

    When the application runs for the first time,we check for Registry Entry language of the application in [HKEY_CURRENT_USER\SOFTWARE\CODE PROJECT\GLOBALIZATION SAMPLE] and returns "en-US" if there is no entry yet. This value is set for the string strCulture of the application

    GetStringRegistryValue in the RegistryAccess class helps us get this

    Collapse
    static public string GetStringRegistryValue(string key, string defaultValue)
    {
    RegistryKey rkCompany;
    RegistryKey rkApplication;
    rkCompany = Registry.CurrentUser.OpenSubKey(SOFTWARE_KEY, false).OpenSubKey(COMPANY_NAME, false);
    if( rkCompany != null )
    {
    rkApplication = rkCompany.OpenSubKey(APPLICATION_NAME, true);
    if( rkApplication != null )
    {
    foreach(string sKey in rkApplication.GetValueNames())
    {
    if( sKey == key )
    {
    return (string)rkApplication.GetValue(sKey);
    }
    }
    }
    }
    return defaultValue;
    }
    

    Globalize Application

    Once we have the strCulture we call the GlobalizeApp function

     private string strResourcesPath= Application.StartupPath + "/Resources";// Resource
    path private string strCulture= "en-US"; // string to store current culture which is comon in all the
    forms private static ResourceManager rm; //
    resourcemanager which retrivesthe strings
    //from the resource files
    
    private void GlobalizeApp()
    {
    SetCulture();
    SetResource();
    SetUIChanges();
    }
    private void SetCulture()
    {
    CultureInfo objCI = new CultureInfo(strCulture);
    Thread.CurrentThread.CurrentCulture = objCI;
    Thread.CurrentThread.CurrentUICulture = objCI;
    }
    private void SetResource()
    {
    rm = ResourceManager.CreateFileBasedResourceManager
    ("resource", strResourcesPath, null);
    }
    private void SetUIChanges()
    {
    ...
    }
    

    GlobalizeApp function Sets the culture information to the current thread, Set the Resource manager to the respective resource file and SetUIChnages does all the user interface translations

    Modular design: Code Once Use Everywhere

    This as i said already, is an important feature because when the application expands or grows with time, you should be ready to change the new string with just one statement replacement. For this i have created a Public Resource Manager in frmMain

            public static ResourceManager RM
    {
    get
    {
    return rm ;
    }
    }
    

    So when the Main Form loads you set the culture and the resource file infiormation to the public resource manager , And in the new added form or anywhere you add a messagebox or label, You can call the resource manager like this

    this.Text = frmMain.RM.GetString("0006");
    label1.Text = frmMain.RM.GetString("0008");
    

    Translations

       SetUIChanges describes how the translations are done ,

    • Texts are directly translated from the resource file
    • Images has to be taken care of using multiple images
    • DateTime etc which are windows specific does not need to be translated at all (isn't that cool)

    The Code behind

    Collapse
    private void SetUIChanges()
    {
    if (String.Compare(strCulture,"en-US")==0)
    {
    picTop.Image = picE.Image;
    }
    if (String.Compare(strCulture,"es-ES")==0)
    {
    picTop.Image = picS.Image;
    }
    if (String.Compare(strCulture,"fr-FR")==0)
    {
    picTop.Image = picF.Image;
    }
    label1.Text=rm.GetString("0001");
    label2.Text=rm.GetString("0002");
    label3.Text=rm.GetString("0003");
    btnSubmit.Text=rm.GetString("0004");
    btnCancel.Text=rm.GetString("0005");
    this.Text = rm.GetString("0000");
    lblselect.Text = rm.GetString("0009");
    lbltime.Text = DateTime.Now.ToLongDateString().ToString();
    }
    

    For images , i have used here three hidden picturebox controls as show below

      

     

    Saving the default culture in registry

    The Code behind

    static public void SetStringRegistryValue(string key, string stringValue)
    {
    RegistryKey rkSoftware;
    RegistryKey rkCompany;
    RegistryKey rkApplication;
    rkSoftware = Registry.CurrentUser.OpenSubKey(SOFTWARE_KEY, true);
    rkCompany = rkSoftware.CreateSubKey(COMPANY_NAME);
    if( rkCompany != null )
    {
    rkApplication = rkCompany.CreateSubKey(APPLICATION_NAME);
    if( rkApplication != null )
    {
    rkApplication.SetValue(key, stringValue);
    }
    }
    }
    

    Acknowledgement

     My humble acknowledgement to my boss who gave me a 6 days deadline, for globalization of an application we have been working on for an year.

    What's wrong with the .Resx approach

    i got a number of emails asking why not use the .resx approach for each forms well here is few of the reasons, i prefer a single resource file compared to multiple .resx files for each form

    Three simple reasons

    1. Maintainability

    Assuming you are taking .resx files approach

    Take a simple scenario, By mistake you have a wrong translation for a "Submit" button for a say german language. The original translation is "Einreichen" but you initially missed the last n and now you have "Einreiche" instead of "Einreichen" for submit button through out your application

    What you can do to resolve this,

    a. You have to go to each forms and change the resource file of the form
    b. Compile the exe again creating the german dll and redistribute the whole exe with setup including the new dll

    On the other hand if you use a single resource file as in the article ,
    Each "Submit" in all the forms translates into something like
    ResourceManager.GetString("101")

    If the translation is wrong just -

    1. Update the initial german text file
    2. Resgen it and create a resource file
    3. Over write your existing resource file by the updated resource file

    you are done. Redistribution needs just the light weight resource file and you EXE will automatically update the submit button every where

    2. Extensibility

    You have to add another language say latino.resx file approach go to each form create resx file for Latino compile and create latino dll

    With Single Resource file approach create another text file with latino translation as shown in example above Resgen it and Add a menu option for Latino you are done. You can have a latino menu option even earlier and add teh resource file later you won't even need to re compile

    3. Dynamic UI changes


    With resource file you can have a drop down menu instead of the radio button in the example and change the complete UI on the fly to whichever language you fancy
    With .resx and dll approach you have to start the application with that localized dll
    i think you might be able to dynamically change the UI but it will be much more complicated process.

    Another not that important reason is, Resource file approach create light weight .Resources files where as .resx creates dll for each language.

    If want to go by the standard approach you can definetly get better results but not as fast as this approach will give you

    And Thanks

     For coming so far, i hope this 20 minutes was worth it and give me your comments/ suggestion to improve this.

    In Action (French)

     

     

  • 相关阅读:
    二分图的最大匹配-hdu-3729-I'm Telling the Truth
    hdu3308LCIS(线段树,点更新,段查寻,查寻时一定要注意跨越时如何计算)
    小智慧58
    【开源项目】Android 手写记事 App(半成品)
    持续集成之戏说Check-in Dance
    XSS与字符编码的那些事儿
    十大渗透测试演练系统
    Google DNS劫持背后的技术分析
    黑客是怎样绕过WAF之三重防护绕过讲解
    Seay工具分享
  • 原文地址:https://www.cnblogs.com/msnadair/p/744106.html
Copyright © 2020-2023  润新知