如何实现多语言支持?
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") %>
上一页 [1] [2] [3] [4]
|