Once I've added Global Resource languages, how

2019-08-11 08:26发布

I have added 3 additional languages (fr, es-mx, de) to English in my company's website, and they're all working fine. I followed the MSDN walkthrough on creating localization.

I'm using Visual Studio 2010 / VB / dot-net 4.0 and I already have in my top line on all of my pages, this:

  <%@ Page Title="USS Products & Services" Language="VB" MasterPageFile="~/products/products.Master" AutoEventWireup="false"
CodeFile="default.aspx.vb" Inherits="default" culture="auto" meta:resourcekey="PageResource1" uiculture="auto" %>

I have 4 global resource (.resx) files in my global_apps directory. But what if I don't want just the browser alone to detect their language? I want to give them the option of choosing their own language.

How do I give the client the option between 4 flags -- 1 for each language -- and let them choose? Or maybe a rollover sitemap type of effect, where they can mouse over a language and choose it? Any help would be appreciated! Thanks!

1条回答
霸刀☆藐视天下
2楼-- · 2019-08-11 08:30

Try this simple method: I've defined the languages in a dropdownlist and have a button select

<asp:DropDownList ID="ddlCulture" DataTextField="DisplayName" DataValueField="Name"
         runat="server" >
        <asp:ListItem Value="es-MX">Spanish</asp:ListItem>
        <asp:ListItem Value="en-US">English</asp:ListItem>
    </asp:DropDownList>
<asp:Button ID="btnSelect" Text="Select" runat="server" OnClick="btnSelect_Click" />

Now Code:

protected void btnSelect_Click(object sender, EventArgs e)
   {
        Session["uiculture"] = ddlCulture.SelectedValue;
        Session["culture"] = ddlCulture.SelectedValue;
        Response.Redirect(Request.Path);
   }

    protected override void InitializeCulture()
    {
      if(Session["culture"]!=null)
        UICulture=Session["culture"].ToString();
    }

Update: Sorry I forgot the override keyword. Now included it should work.

By the way, you're using VB, sorry I'vent seen this. The equivalent code is:

 Protected Sub btnSelect_Click(ByVal sender As Object, ByVal e As EventArgs)
    Session("uiculture") = ddlCulture.SelectedValue
    Session("culture") = ddlCulture.SelectedValue
    Response.Redirect(Request.Path)
End Sub
Protected Overrides Sub InitializeCulture()
    If Not Session("culture") Is Nothing Then
        UICulture = Session("culture").ToString()
    End If
End Sub
查看更多
登录 后发表回答