Unable to change .aspx page culture

I cannot change the culture of the .aspx page.

When I specify the culture using the 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 would like to do is change the localization when the user clicks on the link.

Link

 <asp:ImageButton ID="lang_ur-PK" ImageUrl="~/content/image/flag-of-pakistan.png" runat="server" CssClass="language" Height="64px" Width="64px" 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); } 

Calling this code simply reloads the page and does not 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 a control that needs to be localized:

  <asp:Label id="lblInfoWelcomeMsg" runat="server" Text="<%$ Resources:LocalizedText, Summary_Info_WelcomeMsg %>"></asp:Label> 

thanks

+4
source share
4 answers

You should add this method to 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 could make a BasePage class and add it there, and then BasePage can be inherited on every page.

+7
source

You must do this in Page_PreInit , because the localization can only be changed in this event.

Note that wherever you change the locale, the declarative page will override it, but you can change itin Page_PreInit

Set only the flag in your imageButton_Click (), and then in Page_PreInit change the locale based on the flag value.

+1
source

Your Click handler simply changes the flow culture for the current request - it was long forgotten when the page refreshes after your Response.Redirect.

You need to continue saving the new culture, then read it and set the culture at the beginning of each subsequent request (for example, in Page.InitializeCulture). Common places to save it include:

  • Database on the server.

  • Cookies sent to the customer with a response.

  • In the URL you are redirecting to (e.g. Querystring - e.g.? Lang = ur-PK)

  • Session (but it will be forgotten if the session ends)

+1
source

By redirecting a response, you start a new thread. Take the culture you want, save it in the session, and then on the page load set the culture to the value in the session.

0
source

Source: https://habr.com/ru/post/1346889/


All Articles