欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-13 13:21:58
首先建立语言档,在项目中加入.resx文件例如:message.zh-cn.resx '简体中文message.zh-tw.resx '繁体中文message.en '英文...

首先建立语言档,在项目中加入.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 '英文

这几个语言档吧