2008年6月1日 星期日

.Net Internationalization

The internationalization is a big issue for application that will be deployed for different language environment.
Visual Studio 2005 don't integrate the ResGen is very strange thing. We need to do some extra job to integrate the resource file into our program.
So, this article is a document to describe the steps to let a C# application can support multi-language.
  • Use notepad create a text file. I named it as i18n.txt with following content
i18nTest.Name = Multi-language testing
i18nTest.Caption = Multi-language caption
i18nTest.bntClickMe = Please push me

  • Create a new text file for Tradition Chinese call i18n.zh-tw.txt
i18nTest.Name = 多國語系測試
i18nTest.Caption = 多國語系標題
i18nTest.bntClickMe = 請按我吧
(Be aware save the file with unicode.)

  • Using ResGen.exe (should be in C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin) compile the text file into resources file.
resgen i18n.zh-tw.txt ==> there should be a file called i18n.zh-tw.resources created.
  • Add the files generated by ResGen to project solution.
  • Add some code to get the resource string.
    • import
    • using System.Globalization;
      using System.Threading;
      using System.Resources;
    • declare & initialize a resource manager
public ResourceManager rm;
rm = System.Resources.ResourceManager.CreateFileBasedResourceManager("i18n",".",null);
"i18n" is your default language resource file name.

    • switch culture info
    • try
      {
      Thread.CurrentThread.CurrentCulture =
      new CultureInfo("zh-TW");
      }
      catch (Exception ex)
      {
      Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
      }
      Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
    • Get resource string
    • rm.GetString("i18nTest.Caption");
By using the code above, our program can change it language at runtime and by demand.

Reference
  1. http://msdn.microsoft.com/en-us/library/ccec7sz1.aspx
  2. http://www.codeproject.com/KB/cs/multilingual_pplication.aspx

沒有留言: