[英]Unable to change culture of .aspx page
我無法改變.aspx頁面的文化。
當我在頂部使用page指令指定文化時:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="VideoPlayerPrototype.Index" Culture="ur-PK" UICulture="ur-PK" %>
一切都按預期工作。
我想做的是,當用戶點擊鏈接時能夠更改本地化。
鏈接:
<asp:ImageButton ID="lang_ur-PK"
ImageUrl="~/content/image/flag-of-pakistan.png"
runat="server"
CssClass="language"
Height="64px"
Width="64px"
OnClick="setLanguage" />
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);
}
調用此代碼只會重新加載頁面,並且不會加載正確的語言。
我有存儲在App_LocalResources和App_GlobalResources中的.resx文件:
Index.aspx.resx,Index.aspx.en.resx,Index.aspx.ur-PK.resx,Index.aspx.ur.resx等。
以下是必須本地化的控件示例:
<asp:Label id="lblInfoWelcomeMsg" runat="server"
Text="<%$ Resources:LocalizedText, Summary_Info_WelcomeMsg %>"></asp:Label>
謝謝
您必須在后面的代碼中添加此方法:
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();
}
如果你可以使BasePage
成為一個類並將其添加到那里然后可以在每個頁面上繼承BasePage
那會更好。
您必須在Page_PreInit
執行此操作,因為本地化只能在該事件中進行更改。
請注意, Page_PreInit
您更改區域設置,否則頁面聲明將覆蓋它,但您可以在Page_PreInit
更改Page_PreInit
只在imageButton_Click()中設置一個標志,然后在Page_PreInit
根據標志值更改語言環境。
您的Click處理程序只是更改了當前請求的線程文化 - 在您的Response.Redirect頁面刷新后很久就忘記了這一點。
您需要在某處保留新文化,然后閱讀它並在每個后續請求的開頭設置文化(例如,在Page.InitializeCulture中)。 常見的地方包括:
服務器上的數據庫。
通過響應發送給客戶端的cookie。
在您重定向的URL中(例如在Querystring中 - 例如?lang = ur-PK)
會話(但會議到期時會被遺忘)
通過執行響應重定向,您可以啟動一個新線程。 獲取所需的文化,將其保存在會話中,然后在頁面加載時將文化設置為會話中的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.