·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> ASP.NET WEB页面多语言支持解决方案

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

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

首先建立语言档,在项目中加入.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选项---网际网络设定--〉一般选项卡语言--〉新增里边的内容