简体   繁体   English

无法更改.aspx页面的文化

[英]Unable to change culture of .aspx page

I'm unable to change a culture of .aspx page. 我无法改变.aspx页面的文化。

When I specify the culture by using page directive at the top: 当我在顶部使用page指令指定文化时:

<%@ 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"
                            OnClick="setLanguage" />

setLanguage method: setLanguage方法:

        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: 我有存储在App_LocalResources和App_GlobalResources中的.resx文件:

Index.aspx.resx, Index.aspx.en.resx, Index.aspx.ur-PK.resx, Index.aspx.ur.resx etc. Index.aspx.resx,Index.aspx.en.resx,Index.aspx.ur-PK.resx,Index.aspx.ur.resx等。

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. 如果你可以使BasePage成为一个类并将其添加到那里然后可以在每个页面上继承BasePage那会更好。

You have to do this in Page_PreInit because localization can be change only in that event. 您必须在Page_PreInit执行此操作,因为本地化只能在该事件中进行更改。

Note that wherever else you change the locale, then page declarative will override it, but you can change itin Page_PreInit 请注意, Page_PreInit您更改区域设置,否则页面声明将覆盖它,但您可以在Page_PreInit更改Page_PreInit

Only set a flag in your imageButton_Click() and then in Page_PreInit change the locale based on the flag value. 只在imageButton_Click()中设置一个标志,然后在Page_PreInit根据标志值更改语言环境。

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. 您的Click处理程序只是更改了当前请求的线程文化 - 在您的Response.Redirect页面刷新后很久就忘记了这一点。

You need to persist the new culture somewhere, then read it and set the culture at the beginning of each subsequent request (eg in Page.InitializeCulture). 您需要在某处保留新文化,然后阅读它并在每个后续请求的开头设置文化(例如,在Page.InitializeCulture中)。 Common places to persist it include: 常见的地方包括:

  • A database on the server. 服务器上的数据库。

  • A cookie sent to the client with the response. 通过响应发送给客户端的cookie。

  • In the URL to which you redirect (eg in the Querystring - eg ?lang=ur-PK) 在您重定向的URL中(例如在Querystring中 - 例如?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. 获取所需的文化,将其保存在会话中,然后在页面加载时将文化设置为会话中的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM