.NET多语言解决方案
.NET多语言解决方案
随着Internet的发展,全球经济一体化成为不可逆转的潮流。越来越多的外资企业进驻中国,同时,中国企业也纷纷走向世界,设立了遍布全球的分支机构。但是,在企业与企业之间、分支机构与总部之间的文档交换中,存在着多语言的文件兼容性问题。
在.NET框架中虽然可以很简单多实现空间多多语言,但是,在实际运用中多语言项目全靠控件的多语言完全不够,下面是一个多语言类,只需要简单的调用即可:
/*
*
* 版本: 2.0.50727.3053
* 命名空间: Zhouyz.MultLanguage
* 文件名: LanguageString.cs
*
* 创 建 人:zhouyz
* 创建时间:2010-7-8 16:18:42
* 联系方式:http://www.cnblogs.com/zhouyz
*
*
* 说明:强类型资源语言类,用于查找本地化字符串等。
*
* 修 改 人:.
* 联系方式:
* 修改次数:
* 修改时间:
* 修改内容:
*
*
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Configuration;
using System.Globalization;
using System.Windows.Forms;
namespace Zhouyz.MultLanguage
{
/// <summary>
/// 多语言字符串
/// </summary>
public class LanguageString
{
private static System.Resources.ResourceManager resourceMan = null;//语言资源管理器
private static string _currentAssemblyName = string.Empty;//当前程序集名字
private static Assembly _currentAssembly = null;//当前程序集
private static string _currentLanguageName = string.Empty;//当前语言
private static string _currentRSName = null;// " ClientTest.LanguageRes.EngLishResource";//当前资源包
private static global::System.Globalization.CultureInfo resourceCulture;
public static object syncRoot = new object();
private static bool _IsAutoSetLanguage = false;//是否自动设置语言
public LanguageString()
{
}
#region 内部属性
/// <summary>
///语言管理器
/// </summary>
internal static System.Resources.ResourceManager ResourceManager
{
get
{
return resourceMan;
}
}
/// <summary>
/// 当前程序集名字
/// </summary>
internal static string CurrentAssemblyName
{
get
{
return _currentAssemblyName;
}
}
/// <summary>
/// 当前语言名称
/// </summary>
internal static string CurrentLanguageName
{
get
{
return _currentLanguageName;
}
set
{
setLanguageName();
}
}
internal System.Globalization.CultureInfo Culture
{
get
{
return resourceCulture;
}
set
{
resourceCulture = value;
}
}
#endregion
#region 外部属性
/// <summary>
/// 是否自动设置语言
/// </summary>
public static bool IsAutoSetLanguage
{
get
{
return _IsAutoSetLanguage;
}
set
{
_IsAutoSetLanguage = value;
}
}
/// <summary>
/// 设置语言
/// </summary>
public static void SetLanguage(CultureInfo _culture)
{
if (_currentLanguageName != _culture.Name)
{
if (System.Threading.Thread.CurrentThread.CurrentUICulture != _culture)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = _culture;
}
_currentLanguageName = _culture.Name;
setRSManager();
Update();
}
}
#endregion
#region 私有方法
/// <summary>
/// 设置语言名
/// </summary>
private static void setLanguageName()
{
string currentName = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
if (_currentLanguageName != currentName)
{
_currentLanguageName = currentName;
setRSManager();
}
}
//设置程序集
private static void setAssembly(Assembly aAssembly)
{
if (_currentAssemblyName != aAssembly.FullName )
{
_currentAssembly = aAssembly;
_currentAssemblyName = aAssembly.FullName.Substring(0, aAssembly.FullName.IndexOf(','));
setRSManager();
}
}
//设置语言管理器
private static void setRSManager()
{
setRSPath();
if (_currentRSName != string.Empty && _currentAssembly != null)
{
try
{
System.Resources.ResourceManager temp = new System.Resources.ResourceManager(_currentRSName, _currentAssembly);
temp.IgnoreCase = true;
resourceMan = temp;
}
catch
{
}
}
}
//设置资源路径
private static void setRSPath()
{
if (CurrentLanguageName == null || CurrentLanguageName.Trim().Length < 1)
{
CurrentLanguageName = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
}
string rsPath = ConfigurationManager.AppSettings[CurrentLanguageName];
if (rsPath == null)
{
return;
}
if (rsPath.IndexOf(".resx") > -1)
{
rsPath = rsPath.Replace(".resx", "").Replace('\\', '.').Replace('/', '.');
}
if (_currentAssemblyName != null && _currentAssemblyName.Trim().Length > 0)
{
_currentRSName = _currentAssemblyName + "." + rsPath;
}
}
//控件申请资源
private static void changeControlsLanguage(Control aParentControl, System.ComponentModel.ComponentResourceManager aRes)
{
foreach (Control control in aParentControl.Controls)
{
aRes.ApplyResources(control, control.Name);
if (control.Controls.Count > 0)
{
changeControlsLanguage(control, aRes);
}
}
}
#endregion
/// <summary>
/// 根据字符串名字获得,当前语言对应字符串
/// </summary>
/// <param name="aStrName"></param>
/// <returns></returns>
public static string GetString(string aStrName)
{
lock (syncRoot)
{
if (IsAutoSetLanguage)
{
setLanguageName();
}
setAssembly(Assembly.GetCallingAssembly());
try
{
return ResourceManager.GetString(aStrName, resourceCulture);
}
catch
{
return string.Empty;
}
}
}
/// <summary>
/// 更新所有界面
/// </summary>
public static void Update()
{
foreach (System.Windows.Forms.Form form in Application.OpenForms)
{
System.ComponentModel.ComponentResourceManager res = new System.ComponentModel.ComponentResourceManager(form.GetType());
res.ApplyResources(form, "$this");
changeControlsLanguage(form,res );
}
}
}
}
1、使用前在配置文件(App.config)里面添加配置节
<appSettings>
<add key="default" value="LanguageRes\ChineseResource.resx"/>
<add key="zh-CN" value="LanguageRes\ChineseResource.resx"/>
<add key="en-US" value="LanguageRes\EngLishResource.resx"/>
<add key="ja-JP" value="LanguageRes\JapaneseResource.resx"/>
注意:上面配置中value中的LanguageRes指该解决方案中多所有项目多字符串语言的配置的语言文件夹为LanguageRes。
</appSettings>
2、获得字符串语言包中多字符串方法
例如语言资源包中有一个名称为“button”多字符串,获取方法如下
string temp = LanguageString.GetString("button");
3、更换语言方法
第一步:指定当前线程语言: System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("ja-JP");
其中字符串"ja-JP"指的是语言
第二步:软件语言换为当前线程语言:
LanguageString.SetLanguage(System.Threading.Thread.CurrentThread.CurrentUICulture);
或许直接用LanguageString.SetLanguage(new System.Globalization.CultureInfo("ja-JP"));

浙公网安备 33010602011771号