天天看点

WINFORM C/S 国际化

国际化的软件往往需要多种语言资源,如何在C#的WinForm中做到呢?且看以下分解:

1 工程添加资源文件

           资源文件命名方式 [资源文件主题名].[语言区域.].resx  

          例如资源文件主题名为: "Resource1" 。我们准备了 中 英 日 三个语言版本的资源文件,则对应的语言区域分别是 "zh-CN"、"en"、"ja"。

             所以我们添加了三个资源文件: Resource1.zh-CN.resx 、Resource1.en.resx、 Resource1.ja.resx

2 添加命名空间(反射、资源、进程、国际化)

          using System.Reflection;

          using System.Resources;

           using System.Threading;

          using System.Globalization;

3 获取资源文件管理器

            ResourceManager rm = new ResourceManager("winGetMsgFromResource.Resource1", Assembly.GetExecutingAssembly());

             资源文件名的构成为 [项目命名空间].[资源文件主题名]

4 获取当前进程的语言区域

            CultureInfo ci = Thread.CurrentThread.CurrentCulture;

5 从资源文件中按项目名获取值

            假定MsgId是资源文件中的项目名

             rm.GetString(MsgId, ci);

6 前台国际化环境的选择(改变当前程序进程中的区域信息的方式达到改变)

            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("zh-CN");

            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-us");

            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("ja-JP");

实例:一个简单的资源文件访问类

using System;

using System.Collections.Generic;

using System.Text;

using System.Reflection;

using System.Resources;

using System.Threading;

using System.Globalization;

namespace winGetMsgFromResource

{

    class clsMsg

    {

        public static string getMsg(string MsgId)

        {

             ResourceManager rm = new ResourceManager("winGetMsgFromResource.Resource1", Assembly.GetExecutingAssembly());

             CultureInfo ci = Thread.CurrentThread.CurrentCulture;

            return rm.GetString(MsgId, ci);

         }

        public static string getMsg1()

        {

            string strOut = string.Empty;

             CultureInfo ci = Thread.CurrentThread.CurrentCulture;

            switch (ci.ToString())

            {

                case "zh-CN":

                     strOut = "当前 文化区域 为 中文";

                    break;

                case "en-US":

                     strOut = "Current Culture is ENGLISH";

                    break;

                case "ja-JP":

                     strOut = "現在の言葉は 日本語です";

                    break;

                default:

                     strOut = "others";

                    break;

             }

            return strOut;

         }

     }

}