asp.net(C#)如何用globalization实现多语言转换?
发布网友
发布时间:2022-04-25 14:40
我来回答
共2个回答
热心网友
时间:2022-04-27 11:27
#region Language Operation
/// <summary>
/// 设置app.config中的Language的value.
/// </summary>
/// <param name="AppKey">key</param>
/// <param name="AppValue">value</param>
public void SetLanguageValue(string AppKey, string AppValue)
{
XmlDocument xDoc = new XmlDocument();
string configpath = System.Reflection.Assembly.GetExecutingAssembly().CodeBase + ".config";
string path = configpath.Substring(8);
//此处配置文件在程序目录下
xDoc.Load(path);
XmlNode xNode;
XmlElement xElem1;
XmlElement xElem2;
xNode = xDoc.SelectSingleNode("//appSettings");
xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
if (xElem1 != null)
{
xElem1.SetAttribute("value", AppValue);
}
else
{
xElem2 = xDoc.CreateElement("add");
xElem2.SetAttribute("key", AppKey);
xElem2.SetAttribute("value", AppValue);
xNode.AppendChild(xElem2);
}
xDoc.Save(path);
}
public void SetCurrentLanguageIMG()
{
设置调用的资源文件语言包
switch (OctoSTPNet.Method.Language.LanguageType)
{
case "zh-CHS":
{
menuSChinese1.Checked = Janus.Windows.UI.InheritableBoolean.True;
menuEnglish.Checked = Janus.Windows.UI.InheritableBoolean.False;
menuTChinese.Checked = Janus.Windows.UI.InheritableBoolean.False;
// menuSChinese.Image = OctoSTPNet.Properties.Resources.Tip;
//menuEnglish.Image = null;
//menuTChinese.Image = null;
}
break;
case "zh-TW":
{
menuSChinese1.Checked = Janus.Windows.UI.InheritableBoolean.False;
menuEnglish.Checked = Janus.Windows.UI.InheritableBoolean.False;
menuTChinese.Checked = Janus.Windows.UI.InheritableBoolean.True;
}
break;
default:
{
menuSChinese1.Checked = Janus.Windows.UI.InheritableBoolean.False;
menuEnglish.Checked = Janus.Windows.UI.InheritableBoolean.True;
menuTChinese.Checked = Janus.Windows.UI.InheritableBoolean.False;
}
break;
}
}
private void englishToolStripMenuItem_Click_1(object sender, Janus.Windows.UI.CommandBars.CommandEventArgs e)
{
SetLanguageValue("LanguageType", "EN");
menuSChinese1.Checked = Janus.Windows.UI.InheritableBoolean.False;
menuEnglish.Checked = Janus.Windows.UI.InheritableBoolean.True;
menuTChinese.Checked = Janus.Windows.UI.InheritableBoolean.False;
MessageBoxManager.Show("Please Re-Start OctO to view new Language!", "OctO-Change Language");
}
private void chinaCHSToolStripMenuItem_Click_1(object sender, Janus.Windows.UI.CommandBars.CommandEventArgs e)
{
SetLanguageValue("LanguageType", "zh-CHS");
menuSChinese1.Checked = Janus.Windows.UI.InheritableBoolean.True;
menuEnglish.Checked = Janus.Windows.UI.InheritableBoolean.False;
menuTChinese.Checked = Janus.Windows.UI.InheritableBoolean.False;
MessageBoxManager.Show("Please Re-Start OctO to view new Language!", "OctO-Change Language");
}
private void chinaTWToolStripMenuItem_Click_1(object sender, Janus.Windows.UI.CommandBars.CommandEventArgs e)
{
SetLanguageValue("LanguageType", "zh-TW");
menuSChinese1.Checked = Janus.Windows.UI.InheritableBoolean.False;
menuEnglish.Checked = Janus.Windows.UI.InheritableBoolean.False;
menuTChinese.Checked = Janus.Windows.UI.InheritableBoolean.True;
MessageBoxManager.Show("Please Re-Start OctO to view new Language!", "OctO-Change Language");
}
#endregion
热心网友
时间:2022-04-27 12:45
ASP+内部使用UNICODE,NGWS内部基类的String也是用UNICODE。可以支持某种特定的编码,实现转换。
场所属性可以通过CultureInfo类访问,其中,CurrentCulture是同场所有关的函数的缺省值,
而CurrentUICulture是场所上的资源数据格式,例:
<%=CultureInfo.CurrentCulture.NativeName %>
<%=CultureInfo.currentUICulture.NativeName %>
一些与场所有关的类提供格式化输出,如:
<%=DateTime.Now.Format( "f ",null)%>
<%=DateTime.Now.Format( "f ",new System.Globalization.CultureInfo( "de "))%>
可以为某个目录进行配置,如:
<configuration>
<globalization
fileencoding = "utf-8 "
requestencoding = "utf-8 "
responseencoding = "utf-8 "
culture = "en-us "
uiculture = "de "
/>
或者在Page指令中:
<%@ Page Culture = "fr " UICulture = "fr " ResponseEncoding = "utf-8 " %>
在页面内部还可以随时更改,使用 Thread.CurrentCulture修改,也就是说,同一个页面可以使用
很多种编码输出。
1).设置文化和编码
中文的Culture应设为:zh-cn
CultureInfo.CurrentCulture.Name = "zh-cn ";
CultureInfo.CurrentCulture.EnglishName = "Chinese(Peoples ' Republic of China ";
CultureInfo.CurrentCulture.NativeName = "中文(简体)(中华人民共和国) ";
另外,类RegionInfo还提供地域信息:
RegionInfo.CurrentRegion.NativeName = "中华人民共和国 ";
RegionInfo.CurrentRegion.CurrencySymbol = "¥ ";
2).本地化ASP+应用
3).使用资源文件
NGWS基类支持,运行时有个类叫ResourceManager的实例可以使用。可以用ResourceWriter或者实用
工具resgen.exe来生成资源文件,resgen以 key = value 的形式作为输入,如:
;
;注释
;
[Strings]
greetings = 欢迎你!
more = 更多新闻
资源文件的后缀为.resources。
如何在页面中使用资源文件?
用户的Content-Language可以用Request.UserLanguages[0]来取得。
如何实现多语言支持?
a).准备资源文件,生成.resources文件,文件取名规则:中间带Culture名。例:articles.en-us.resources
b).global.asax中取得一个ResourceManager,并放如Application中供整个Application使用
c).global.asax中为Application_BeginRequest事件写代码,根据客户的情况决定当前的Culture.
d).在页面中用ResourceManager.GetString取得内容。
例:
//global.asax中:
void Application_OnStart(){
Application[ "RM "]=new ResourceManager( "articles ",Server.Mappath( "resources ")+Enviroment.DirectorySeparatorChar,null);
}
void Application_BeginRequest(Object sender,EventArgs e){
try {
Thread.CurrentThread.CurrentCulture = new
CultureInfo(Request.UserLanguages[0]);
}catch(ArgumentException){
Thread.CurrentThread.CurrentCulture=new CultureInfo( "en-us ");
}
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
}
//default.asax中:
ResourceManager rm;
void Page_Init(Object sender,EventArgs e){
rm=(ResouceManager)Application[ "RM "];
}
//输出内容时:
<%= rm.GetString( "greetings ") %>