国际化的软件往往需要多种语言资源,如何在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
}

示例工程
/Files/heekui/winGetMsgFromResource.rar

 

相关文章:

  • 2021-11-04
  • 2021-09-17
  • 2021-07-23
  • 2022-02-21
  • 2022-12-23
  • 2021-05-16
  • 2021-10-30
猜你喜欢
  • 2021-11-18
  • 2022-03-03
  • 2022-12-23
  • 2022-12-23
  • 2021-09-29
  • 2022-12-23
相关资源
相似解决方案