资源文件在国际化中的运用(WinForm)
2006-12-03 22:04
309 查看
国际化的软件往往需要多种语言资源,如何在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;
}
}
}
示例工程
/Files/heekui/winGetMsgFromResource.rar
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;
}
}
}
示例工程
/Files/heekui/winGetMsgFromResource.rar
相关文章推荐
- 资源文件在国际化中的运用(WinForm)
- 资源文件在国际化中的运用(WinForm)
- Struts2(十三):超链接实现动态加载国际化资源文件
- .NET 中从一个类库中访问资源文件,以实现 ASP.NET 或 ASP.NET MVC 的国际化
- java后台获取国际化资源文件
- 复习struts2之配置action范围国际化资源文件
- 使用native2ascii命令处理Struts2国际化资源文件
- 使用资源文件实现C#国际化(Silverlight中英文的实现)
- Tips:简便的运用参数替换来减小资源文件
- Struts国际化资源文件中文转码
- 第二十七章 配置Action范围国际化资源文件
- 使用资源文件实现java的国际化
- 【Java EE 学习 35 上】【strus2】【类型转换器】【struts2和Servlet API解耦】【国际化问题】【资源文件乱码问题已经解决】
- 加载国际化资源文件
- app增加国际化的资源文件支持
- struts2 资源文件:国际化
- structs 国际化资源文件配置
- Android国际化资源文件自动化生成工具
- 2.JSTL国际化:资源文件
- Struts2学习笔记(十六):配置国际化全局资源文件、输出国际化信息