ASP.NET WEB頁面多語言支援解決方案)

來源:互聯網
上載者:User

首先建立語言檔,在項目中加入.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         '英文

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

 

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

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.