我有一个ASP.Net网站,其App-GlobalResources结构如下:
我正在使用此文件在我的标记中检索所需的本地化字符串,如下所示:
<%@ Page Title='<%$ Resources:ResourceStaticStuff, HelpIntroductionTitle %>' Language="C#" MasterPageFile="~/App_Resources/PlainDefault.master"
AutoEventWireup="true" CodeBehind="Introduction.aspx.cs" Inherits="Software.PasswordReset.Registration.Web.Help.Introduction" %>
<asp:Content ID="ContentWelcome" ContentPlaceHolderID="BodyContentPlaceholder" runat="server">
<h1 class="title-regular clearfix">
<asp:Literal ID="LiteralHeader" runat="server" Text='<%$ Resources:ResourceStaticStuff, HelpIntroductionTitle %>' />
</h1>
<asp:Literal ID="LiteralHelp" runat="server" Text='<%$ Resources:ResourceStaticStuff, HelpIntroductionLiteralHelpText %>' />
<br />
<br />
</asp:Content>
即使改变了文化+ ui文化,我也总是得到默认的英文文本。我在运行时改变文化如下:
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!Page.IsPostBack)
{
SiteLogger.NLogger.Info("Loading Languages and Directories");
if (!LoadLanguages() || !LoadDirectories())
{
SiteLogger.NLogger.Info("Loading Languages or Directories failed!");
return;
}
SiteLogger.NLogger.Info("Completed : PublicLogOn.PageLoad");
}
}
catch (Exception ex)
{
SiteLogger.NLogger.Error("Error in PublicLogOn.Page_Load", ex.Message);
}
}
private Boolean LoadLanguages()
{
Boolean methodResult;
try
{
SiteLogger.NLogger.Info("In Load Languages");
DDLLanguages.Items.Clear();
var fetchedLanguages = UserManagePage.GetOrganizationLanguages();
foreach (var oneFetchedLanguage in fetchedLanguages)
{
DDLLanguages.Items.Add(new ListItem(oneFetchedLanguage.LanguageSymbol, oneFetchedLanguage.LanguageSymbol));
}
if (fetchedLanguages.Count() == 1)
{
DDLLanguages.Enabled = false;
}
// The first place where the language is pushed in. Everything that follows afterwards will be translated
Session["UserLanguage"] = DDLLanguages.SelectedValue;
UpdateLanguage();
methodResult = true;
}
catch (Exception exp)
{
SiteLogger.NLogger.Error("Error in load languages : ", exp.ToString());
// Nlogger.LogError(exp);
labelMessage.Text = MessageFormatter.GetFormattedErrorMessage("Error retrieving organization languages.");
methodResult = false;
}
return methodResult;
}
private void UpdateLanguage(String languageCode="")
{
if (String.IsNullOrEmpty(languageCode))
{
languageCode = Session["UserLanguage"].ToString();
}
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(languageCode);
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(languageCode);
var isRtl = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.IsRightToLeft;
if (isRtl)
{
Master.BodyTag.Attributes["dir"] = "rtl";
}
else
{
Master.BodyTag.Attributes["dir"] = "ltr";
}
base.InitializeCulture();
}
protected void DDLLanguages_SelectedIndexChanged(object sender, EventArgs e)
{
Session["UserLanguage"] = DDLLanguages.SelectedValue;
UpdateLanguage();
}
这里感兴趣的是页面从右到左或从左到右都是正确的,但是有英文。所以,看起来* .ur-PK.resx似乎没有受理。
有什么想法吗?
答案 0 :(得分:1)
以编程方式override the InitializeCulture method for the page
。
protected override void InitializeCulture()
{
if (Session["UserLanguage"] != null)
{
String selectedLanguage = Session["UserLanguage"].ToString();
UICulture = selectedLanguage ;
Culture = selectedLanguage ;
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(selectedLanguage);
Thread.CurrentThread.CurrentUICulture = new
CultureInfo(selectedLanguage);
}
base.InitializeCulture();
}
源: - Set the Culture and UI Culture for ASP.NET Web Page
要解决此问题,您可以创建一个所有特定页面都继承的BasePage:
以下是一个例子:
public class BasePage : System.Web.UI.Page
{
protected override void InitializeCulture()
{
//Do the logic you want for all pages that inherit the BasePage.
}
}
具体页面看起来应该是这样的:
public partial class _Default : BasePage //Instead of it System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Your logic.
}
//Your logic.
}