问题
Most of my .resx files are in App_GlobalResources (explicit), and the master pages are in the App_LocalResources in their directories (implicit). I have to top line in all my pages set to culture / uiculture - auto.
Now that my languages are set up, Nuux explained to me how to create a drop-down box and translate that particular page, and this is great -- a lot more than I had.
But what if I want the end-user to see an array of flags on the homepage, so they can manually opt to view the entire site in that language? Is this a LOT more intricate than what I've already done, or is it just copying and pasting? Is it going to involve creating a directory for each language? Any suggestions or guidance would be greatly appreciated!
回答1:
Take a look at this code Developing an ASP.NET page with MasterPage and Localization. Make it fit your need. If you couldn't customize it. I'll try to help you.
Update: Ok, let us try this: Add new class to your project: and overwrite its content with this
Imports Microsoft.VisualBasic
Imports System.Globalization
Imports System.Threading
Public Class BasePage
Inherits System.Web.UI.Page
Protected Overrides Sub InitializeCulture()
'MyBase.InitializeCulture()
If Session("culture") Is Nothing Then
Session("culture") = "en-US" 'Set default language
End If
Dim cult As String = Session("culture")
Culture = cult
UICulture = cult
MyBase.InitializeCulture()
End Sub
End Class
Now go to the codebehind of every page you have and change the Inherits clause :
Partial Class _Default
Inherits BasePage
After this point you need to implement the language interface in a master page
<asp:DropDownList ID="ddlCulture" DataTextField="Name" 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" />
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
The event in the masterpage codebehind file remains:
Protected Sub btnSelect_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSelect.Click
Session("culture") = ddlCulture.SelectedValue
Response.Redirect(Request.Path)
End Sub
Now your website is ready only customize the control I used to your preference.
On thing remaining is that we used Session to store the user selection, however, when the session expires the user is presented with the default language so you may replace the Session with a persistent cookie. First let this idea work, I hope changing to cookie won't be a problem. I just wanted you keep this concept in mind.
来源:https://stackoverflow.com/questions/8965927/once-localized-with-resx-files-how-to-enable-client-to-view-entire-website-in