天天看點

資源檔案在國際化中的運用(WinForm)

國際化的軟體往往需要多種語言資源,如何在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");

執行個體:一個簡單的資源檔案通路類

資源檔案在國際化中的運用(WinForm)

using System;

資源檔案在國際化中的運用(WinForm)

using System.Collections.Generic;

資源檔案在國際化中的運用(WinForm)

using System.Text;

資源檔案在國際化中的運用(WinForm)

using System.Reflection;

資源檔案在國際化中的運用(WinForm)

using System.Resources;

資源檔案在國際化中的運用(WinForm)

using System.Threading;

資源檔案在國際化中的運用(WinForm)

using System.Globalization;

資源檔案在國際化中的運用(WinForm)
資源檔案在國際化中的運用(WinForm)

namespace winGetMsgFromResource

資源檔案在國際化中的運用(WinForm)
資源檔案在國際化中的運用(WinForm)
資源檔案在國際化中的運用(WinForm)

{

資源檔案在國際化中的運用(WinForm)

    class clsMsg

資源檔案在國際化中的運用(WinForm)
資源檔案在國際化中的運用(WinForm)
資源檔案在國際化中的運用(WinForm)
資源檔案在國際化中的運用(WinForm)

        public static string getMsg(string MsgId)

資源檔案在國際化中的運用(WinForm)
資源檔案在國際化中的運用(WinForm)
資源檔案在國際化中的運用(WinForm)
資源檔案在國際化中的運用(WinForm)

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

資源檔案在國際化中的運用(WinForm)

            CultureInfo ci = Thread.CurrentThread.CurrentCulture;

資源檔案在國際化中的運用(WinForm)

            return rm.GetString(MsgId, ci);

資源檔案在國際化中的運用(WinForm)

        }

資源檔案在國際化中的運用(WinForm)
資源檔案在國際化中的運用(WinForm)

        public static string getMsg1()

資源檔案在國際化中的運用(WinForm)
資源檔案在國際化中的運用(WinForm)
資源檔案在國際化中的運用(WinForm)
資源檔案在國際化中的運用(WinForm)

            string strOut = string.Empty;

資源檔案在國際化中的運用(WinForm)
資源檔案在國際化中的運用(WinForm)

            switch (ci.ToString())

資源檔案在國際化中的運用(WinForm)
資源檔案在國際化中的運用(WinForm)
資源檔案在國際化中的運用(WinForm)

資源檔案在國際化中的運用(WinForm)

                case "zh-CN":

資源檔案在國際化中的運用(WinForm)

                    strOut = "目前 文化區域 為 中文";

資源檔案在國際化中的運用(WinForm)

                    break;

資源檔案在國際化中的運用(WinForm)

                case "en-US":

資源檔案在國際化中的運用(WinForm)

                    strOut = "Current Culture is ENGLISH";

資源檔案在國際化中的運用(WinForm)
資源檔案在國際化中的運用(WinForm)

                case "ja-JP":

資源檔案在國際化中的運用(WinForm)

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

資源檔案在國際化中的運用(WinForm)
資源檔案在國際化中的運用(WinForm)

                default:

資源檔案在國際化中的運用(WinForm)

                    strOut = "others";

資源檔案在國際化中的運用(WinForm)
資源檔案在國際化中的運用(WinForm)

            }

資源檔案在國際化中的運用(WinForm)

            return strOut;

資源檔案在國際化中的運用(WinForm)
資源檔案在國際化中的運用(WinForm)

    }

資源檔案在國際化中的運用(WinForm)

}

示例工程

<a href="http://files.cnblogs.com/heekui/winGetMsgFromResource.rar">/Files/heekui/winGetMsgFromResource.rar</a>