WEB頁面多語言支持解決方案
2024-07-21 02:25:17
供稿:網友
首先建立語言檔,在項目中加入.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 '英文
這幾個語言檔吧
注冊會員,創建你的web開發資料庫,