首页 文章

多语言网站,如何将asp CommandArgument指令更改为html以切换语言?

提问于
浏览
0

我有一个多语言的公司网站(Visual Studio / VB / ASP.NET 4.0),当你在主页上点击一个标志时,文本会改变为该语言,并在我上课后与你保持整个会话, BasePage.vb . 一切都很好,但 URL never changes .... clients.aspx仍然是clients.aspx虽然文本DOES切换到荷兰语 .

@Aritstos建议它使它看起来像 clients.aspx?lang=nl 而不是另一种语言的 clients.aspx .

Currently, my asp homepage hyperlinks that work look like this:

<asp:LinkButton ID="LinkButton7" runat="server"
      CommandArgument="nl" OnClick="RequestLanguageChange_Click"
      class="flagbutton">      
      <asp:Image ID="Image1" runat="server" ImageUrl="~/images/flagnl.png"
      tooltip="Bekijk deze website in het Nederlands" title="Bekijk deze website 
      in het Nederlands"/>
      <img class="map" src="images/flaghovernl.png" alt=""/>
      </asp:LinkButton>

I tried to change the asp links to html links, like this:

<a href="default.aspx?lang-nl" class="flagbutton">
     <img src="images/flagnl.png" alt="Bekijk deze website in het Nederlands"
     title="Bekijk deze website in het Nederlands"/>
     <img class="map" src="images/flaghovernl.png" alt=""/>

但那并没有将语言转换为荷兰语 . 当我点击任何链接,比如about.aspx时,?lang-nl就会消失 . 所以荷兰人什么都没有 . 我被告知asplinks做了javascript_dopostback吗?这很糟糕 . 有人可以告诉我如何更改我的链接,以便他们最终 create a string at the end of the URL saying "?lang-nl" ,并通过他们的 entire session 保持这样的状态?任何帮助将是真诚的感谢!

PS - 这是我的BasePage.vb的代码:

Imports Microsoft.VisualBasic
    Imports System
    Imports System.Data
    Imports System.Configuration
    Imports System.Globalization
    Imports System.Threading
    Imports System.Web
    Imports System.Web.Security
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Web.UI.WebControls.WebParts
    Imports System.Web.UI.HtmlControls

     Namespace Udev.MasterPageWithLocalization.Classes
      ''' <summary>
      ''' Custom base page used for all web forms.
      ''' </summary>
      Public Class BasePage
          Inherits Page
          Protected Overrides Sub InitializeCulture()
              'retrieve culture information from session
              Dim culture__1 As String =  
  Convert.ToString(Session([Global].SESSION_KEY_CULTURE))

              'check whether a culture is stored in the session
              If culture__1.Length > 0 Then
                  Culture = culture__1
              End If

              'set culture to current thread
              Thread.CurrentThread.CurrentCulture =  
  CultureInfo.CreateSpecificCulture(culture__1)
              Thread.CurrentThread.CurrentUICulture = New CultureInfo(culture__1)

              'call base class
              MyBase.InitializeCulture()
          End Sub
      End Class
  End Namespace

在我的Global.vb中,我有这个:

Namespace Udev.MasterPageWithLocalization.Classes
      ''' <summary>
      ''' Summary description for Global
      ''' </summary>
      Public Structure [Global]
          Public Const SESSION_KEY_CULTURE As String = "culture"
      End Structure
  End Namespace

在我的Culture.vb中,我有这个:

Namespace Udev.MasterPageWithLocalization.Classes
      ''' <summary>
      ''' This class provides ISO definitions for all cultures that are supported by 
    this application.
      ''' </summary>
      Public Structure Culture
          'German - Switzerland definition
          Public Const DE As String = "de"
          'English - Great Britain definition
          Public Const EN As String = "en"
      End Structure
  End Namespace

2 回答

  • 2

    从你的案例中的url读取参数是clients.aspx?lang = nl中的(lang)你可以使用:

    Request.QueryString("lang")
    

    这样,您可以使用简单的if或select case语句来确定用户请求的语言 . 像这样的东西:

    Select Case Request.QueryString("lang")
        Case "en"
            ' switch the english
        Case "ar"
            ' switch to arabic
        Case "jp"
            ' switch to japan
        Case Else
            ' your default
    End Select
    

    现在,为了在导航网站时为用户保留相同的语言,您可以使用以下代码将其保存在会话中:

    Session("lang") = "en"
    

    并读取会话值使用代码:

    Dim lang as String = CType(Session.Item("lang"), String)
    

    现在当我们结合两种方法并确保不存在冲突时:

    ' check if there is a lang value in the url
        If Request.QueryString("lang").Length > 0 Then
            Select Case Request.QueryString("lang")
                Case "en"
                    Session("lang") = "en"
                    ' switch the english languge
                Case "ar"
                    Session("lang") = "ar"
                    ' switch to arabic
                Case "jp"
                    Session("lang") = "jp"
                    ' sitch to japaneese
                Case Else
                    Session("lang") = "en"
                    ' your default language
            End Select
    
        Else
            ' check if there is a value in the session
            Select Case CType(Session.Item("lang"), String)
                Case "en"
                    ' switch the english languge
                Case "ar"
                    ' switch to arabic
                Case "jp"
                    ' sitch to japaneese
                Case Else
                    ' your default language
            End Select
        End If
    
  • 2

    URL参数不会自动从一个请求传递到下一个请求 .

    您需要确定语言是始终作为URL参数传递,还是将所选语言存储在Session变量中 .

    如果选择Session变量存储,还有几种方法可以实现语言选择 .

    在BasePage中,检测URL参数(例如lang = nl),并实现这样的语言选择(抱歉C#代码):

    if (Request["lang"]=="nl")
        Session["lang"] = "nl";
    

    您还可以使用LinkButton并将RequestLanguageChange_Click实现为

    Session["lang"] = "nl";
    Response.Redirect(Request.RawUrl);
    

    您没有提到实际实现多语言功能的方式 . 在任何情况下,您的代码都需要分析Session [“lang”]的值以查找所有UI元素的文本 .

相关问题