views:

649

answers:

1

Hi,

I'm using the Resource expression directives in an ASP.NET page that has four global resource files, neutral, UK, US and Italian. However, using the expression syntax always returns US.

Some code for the ASPX page:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GlobalisationResources._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <h2>Current culture: <%= System.Threading.Thread.CurrentThread.CurrentUICulture.Name + " - " + System.Threading.Thread.CurrentThread.CurrentCulture.Name %></h2>
     <h2>At page load: <asp:Literal ID="PageLoadLiteral" runat="server" /></h2>
     <h2>At initialise culture: <asp:Literal ID="InitCultureLiteral" runat="server" /></h2>
     <asp:DropDownList AutoPostBack="true" runat="server" ID="LangDropDown" />
     <br />  
     Using resources processor command: <span><asp:Literal runat="server" Text="<%$ Resources: SomeResources, Banana %>" /></span>
     <br />
     GetGlobalResourceObject: <span><%= GetGlobalResourceObject ("SomeResources", "Banana") %></span>
     <br />
     Typed: <span><%= Resources.SomeResources.Banana %></span>  
     <br />  
     <br />  
     ORANGES! Using resources processor command: <span><asp:Literal runat="server" Text="<%$ Resources: SomeResources, Orange %>" /></span>
     <br />
     ORANGES! GetGlobalResourceObject: <span><%= GetGlobalResourceObject ("SomeResources", "Orange")%></span>
     <br />
     ORANGES! Typed: <span><%= Resources.SomeResources.Orange %></span>  
    </div>
    </form>
</body>
</html>

The code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Globalization;

namespace GlobalisationResources
{
    public partial class _Default : Page
    {
     string mCulture;

     protected void Page_Load (object sender, EventArgs e)
     {
      LangDropDown.SelectedIndexChanged += LangDropDown_SelectedIndexChanged;

      if (!IsPostBack)
      {
       LangDropDown.Items.Add ("en-US");
       LangDropDown.Items.Add ("en-GB");
       LangDropDown.Items.Add ("it-IT");
      }

      PageLoadLiteral.Text = Thread.CurrentThread.CurrentUICulture.Name + " - " + Thread.CurrentThread.CurrentCulture.Name;
      InitCultureLiteral.Text = mCulture;
     }

     override protected void InitializeCulture ()
     {
      mCulture = Thread.CurrentThread.CurrentUICulture.Name + " - " + Thread.CurrentThread.CurrentCulture.Name;

      base.InitializeCulture ();
     }

     void LangDropDown_SelectedIndexChanged (object sender, EventArgs e)
     {
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture (LangDropDown.Text);
      Thread.CurrentThread.CurrentUICulture = new CultureInfo (LangDropDown.Text);
     }
    }
}

As you can see, the user can change the language at any time, and at the top of the page I print out the culture at various stages, the results are:

At first request:

Current culture: en-US - en-GB
At page load: en-US - en-GB
At initialise culture: en-US - en-GB

When selecting GB:

Current culture: en-GB - en-GB
At page load: en-US - en-GB
At initialise culture: en-US - en-GB

And for IT:

Current culture: it-IT - it-IT
At page load: en-US - en-GB
At initialise culture: en-US - en-GB

I don't know if those results have any impact on the use the Resources syntax or not, but does anyone know why it would always pick up the wrong values when everything else (GetGlobalResourceObject and typed) always work?

P.S. I'm in the UK, and my region is set to UK too.

Thanks.

+1  A: 

I think your culture not set properly set using like.. and plz let me know

protected override void InitializeCulture()
{
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    base.InitializeCulture();
}
Muhammad Akhtar
That's exactly what I've done, but it doesn't seem to work. It'll only pull out the default, not the culture specific values.
Kieron
Got it that time! Although because of the order in which the InitialiseCulture method is called, the control hasn't been created yet. To fix simply use Request.Form["ControlName"] and all is good.Thanks!
Kieron