I'm unable to change a culture of .aspx page.
When I specify the culture by using page directive at the top:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="VideoPlayerPrototype.Index" Culture="ur-PK" UICulture="ur-PK" %>
Everything works as expected.
What I'd like to do, is to be able to change localisation when user clicks on a link.
Link:
<asp:ImageButton ID="lang_ur-PK"
ImageUrl="~/content/image/flag-of-pakistan.png"
runat="server"
CssClass="language"
Height="64px"
Width="64px"
开发者_如何学JAVA OnClick="setLanguage" />
setLanguage method:
protected void setLanguage(Object sender, EventArgs e)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("ur-PK");
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("ur-PK");
Response.Redirect(Request.Path);
}
Invoking this code just reloads the page and doesn't load the correct language.
I have .resx files stored in App_LocalResources and App_GlobalResources:
Index.aspx.resx, Index.aspx.en.resx, Index.aspx.ur-PK.resx, Index.aspx.ur.resx etc.
Here is an example of control that must be localised:
<asp:Label id="lblInfoWelcomeMsg" runat="server"
Text="<%$ Resources:LocalizedText, Summary_Info_WelcomeMsg %>"></asp:Label>
Thank you
You have to add this method in your code behind:
protected override void InitializeCulture()
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("ur-PK");
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("ur-PK");
base.InitializeCulture();
}
It would be better if you can make BasePage
a class and add it there and then BasePage
can be inherited on each page.
You have to do this in Page_PreInit
because localization can be change only in that event.
Note that wherever else you change the locale, then page declarative will override it, but you can change itin Page_PreInit
Only set a flag in your imageButton_Click() and then in Page_PreInit
change the locale based on the flag value.
Your Click handler simply changes the thread's culture for the current request - this has been long forgotten when the page refreshes following your Response.Redirect.
You need to persist the new culture somewhere, then read it and set the culture at the beginning of each subsequent request (e.g. in Page.InitializeCulture). Common places to persist it include:
A database on the server.
A cookie sent to the client with the response.
In the URL to which you redirect (e.g. in the Querystring - e.g. ?lang=ur-PK)
Session (but it will be forgotten if the Session expires)
By doing a response redirect, you start a new thread. Take the culture you want, save it in the session and then on page load set the culture to the value in the session.
精彩评论