Question

I have a ASP.Net website that has a App-GlobalResources structure as follows:

App-GlobalResources structure

I'm using this file to retrieve the required localized strings in my markup as follows:

  <%@ 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>

I'm always getting the default English text even after changing the culture + ui culture. I'm changing the culture at runtime as follows:

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();
}

Thing of interest here is that the page becomes Right-To-Left or Left-To-Right good and proper but with English. So, it'd seem the *.ur-PK.resx are not being entertained.

Any ideas?

Was it helpful?

Solution

To set the culture and UI culture for an ASP.NET Web page programmatically 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();
}

source:-Set the Culture and UI Culture for ASP.NET Web Page

To fix this, you could create a BasePage that all your specific pages inherit:

  1. Create a new Class (not Webform), call it BasePage, or whatever you want.
  2. Make it inherit System.Web.UI.Page.
  3. Make all your other pages inherit the BasePage.

Here's an example:

public class BasePage : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        //Do the logic you want for all pages that inherit the BasePage.
    }
}

And the specific pages should look something like this:

public partial class _Default : BasePage //Instead of it System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Your logic.
    }

    //Your logic.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top