博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

WEB页面多语言支持解决方案

Posted on 2009-04-10 15:01  linFen  阅读(927)  评论(0编辑  收藏  举报
首先建立语言档,在项目中加入.resx文件
例如:
message.zh-cn.resx 简体中文
message.zh-tw.resx 繁体中文
message.en 英文
..............

======================================================================
然后利用name --value 键值对 填入你要在页面上显示的语言
如:
name value
message.zh-cn.resx中:
res_loginbname 登陆名 :
message.zh-tw.resx中:
res_loginbname 登陸名 :
message.zh-cn.resx中:
res_loginbname login name :



======================================================================
然后在golbal.asax中加入多语言设定支持代码(浏览器需要支持cookie)

=============================================================================================
application_beginrequest event

the application_beginrequest method is an asp.net event that executes
on each web request into the portal application.

the thread culture is set for each request using the language
settings

=============================================================================================
sub application_beginrequest(byval sender as object, byval e as eventargs)
try
if not request.cookies("resource") is nothing or request.cookies("resource").value = "" then
thread.currentthread.currentculture = cultureinfo.createspecificculture(request.cookies("resource").value)
else
thread.currentthread.currentculture = new cultureinfo(configurationsettings.appsettings("defaultculture"))
end if
thread.currentthread.currentuiculture = thread.currentthread.currentculture
catch ex as exception
thread.currentthread.currentculture = new cultureinfo(configurationsettings.appsettings("defaultculture"))
end try
end sub application_beginrequest

在web.config中加入如下代码,用于设定编码和默认语种,在global.asax中有调用:

======================================================================
<globalization requestencoding="utf-8" responseencoding="utf-8" />
<appsettings>
<add key="defaultculture" value="zh-cn" />
<!-- zh-cn:簡體中文 zh-tw:繁體中文 en:英文 -->
</appsettings>



======================================================================
页面代码中使用多语言支持:

imports system.resources

public class 类名
inherits system.web.ui.page
protected locrm as resourcemanager = new resourcemanager("项目文件名.message", gettype(类名).assembly)

private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
lbllogin.text = locrm.getstring("res_login")
end sub
end class


======================================================================

到这里多语言支持的工作就作完了,接下来自己去慢慢key
message.zh-cn.resx 简体中文
message.zh-tw.resx 繁体中文
message.en 英文

这几个语言档吧