0

I'm trying to use resource files in an ASP.NET Web Forms application (.NET 4.0). I'm using VS2012. I have the following files inside the App_GlobalResources folder:

  • Address.resx (default language, English)
  • Address.ja-JP.resx (Japanese)

The problem is when I'm trying to display the text in Japanese in an ASP.NET page (*.aspx file). If I use the following syntax everything works fine:

<%= Resources.Address.Street1 %>

But when I try to bind it to a property of an asp:Label control the default text (English) is displayed instead of Japanese:

<asp:Label ID="lblStreet1" runat="server" Text='<%$ Resources:Address,Street1 %>'></asp:Label>

BTW culture is being set in session variables and then in the master page I have something like this:

Thread.CurrentThread.CurrentCulture = (CultureInfo) Session["ci"];
Thread.CurrentThread.CurrentUICulture = (CultureInfo) Session["uci"];

Also, I don't know if this is relevant or not but I generated the Address.ja-JP.resx outside Visual Studio (using Notepad++) and then moved the file to the App_GlobalResources folder and included the file in the solution.

Am I missing something here?

2 Answers 2

1

I was able to find a solution to my problem. In the code behind I had to override the InitializeCulture method, I did something like this:

protected override void InitializeCulture()
{
    Thread.CurrentThread.CurrentCulture = (CultureInfo) Session["ci"];
    Thread.CurrentThread.CurrentUICulture = (CultureInfo) Session["uci"];

    base.InitializeCulture();
}
Sign up to request clarification or add additional context in comments.

Comments

0

I would recommend you look in to using meta:resourcekey on the label control. In your case you could then use:

<asp:Label ID="lblStreet1" runat="server" meta:resourcekey="myStreet1Label"></asp:Label>

The resource key in your resx files would then be like this:

<data name="myStreet1Label.Text">
<value xml:space="preserve">The street data.</value></data>

4 Comments

Thanks d-unit, that might work but I don't want to go that way. Too many pages are already using the expression syntax: <%$ ... %> :(
In that case, maybe try using HttpContext.GetGlobalResourceObject. <asp:Label ID="lblStreet1" runat="server" Text='<%= GetGlobalResourceObject("Address", "Street1") %>' />
Thanks, I tried with Text='<%# GetGlobalResourceObject("Address", "Street1") %>'> but it doesn't work...
Found a solution to my problem (see my answer). Anyway thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.