閱讀579 返回首頁    go 阿裏雲 go 技術社區[雲棲]


ASP.NET WEB頁麵多語言支持解決方案

首先建立語言檔,在項目中加入.resx文件

例如:
message.zh-cn.resx'簡體中文
message.zh-tw.resx'繁體中文
message.en'英文
..............

============================================
然後利用Name--Value鍵值對填入你要在頁麵上顯示的語言
如:
namevalue
message.zh-cn.resx中:res_loginbname登陸名:
message.zh-tw.resx中:res_loginbname登陸名:
message.zh-cn.resx中:res_loginbnameLoginName:

=============================================
然後在Golbal.asax中加入多語言設定支持代碼(瀏覽器需要支持Cookie)

'========================================
'Application_BeginRequestEvent
'
'TheApplication_BeginRequestmethodisanASP.NETeventthatexecutes
'oneachwebrequestintotheportalapplication.
'
'Thethreadcultureissetforeachrequestusingthelanguage
'settings
'
'=========================================
SubApplication_BeginRequest(ByValsenderAsObject,ByValeAsEventArgs)
Try
IfNotRequest.Cookies("resource")IsNothingOrRequest.Cookies("resource").Value=""Then
Thread.CurrentThread.CurrentCulture=CultureInfo.CreateSpecificCulture(Request.Cookies("resource").Value)
Else
Thread.CurrentThread.CurrentCulture=NewCultureInfo(ConfigurationSettings.AppSettings("DefaultCulture"))
EndIf
Thread.CurrentThread.CurrentUICulture=Thread.CurrentThread.CurrentCulture
CatchexAsException
Thread.CurrentThread.CurrentCulture=NewCultureInfo(ConfigurationSettings.AppSettings("DefaultCulture"))
EndTry
EndSub'Application_BeginRequest

在Web.Config中加入如下代碼,用於設定編碼和默認語種,在Global.asax中有調用:

================================================
<globalizationrequestEncoding="utf-8"responseEncoding="utf-8"/>
<appSettings>
<addkey="DefaultCulture"value="zh-cn"/>
<!--zh-cn:簡體中文zh-tw:繁體中文en:英文-->
</appSettings>

=================================================
頁麵代碼中使用多語言支持:

ImportsSystem.Resources

PublicClass你的類名
InheritsSystem.Web.UI.Page
ProtectedLocRMAsResourceManager=NewResourceManager("項目文件名.message",GetType(類名).Assembly)

PrivateSubPage_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Load
lblLogin.Text=LocRM.GetString("res_login")
EndSub
EndClass


==================================================
到這裏多語言支持的工作就作完了,接下來自己去慢慢Key
message.zh-cn.resx'簡體中文
message.zh-tw.resx'繁體中文
message.en'英文

這幾個語言檔吧,這是一個痛苦的回憶!


語言的後綴如:
zh-cn'簡體中文
zh-tw'繁體中文
en'英文

可以參考IE選項---網際網絡設定--〉一般選項卡語言--〉新增裏邊的內容

最後更新:2017-04-02 00:06:35

  上一篇:go ASP.NET實現自適應圖片大小的彈窗 窗口可任意編輯
  下一篇:go asp.net StreamReader 創建文件