Resource file not being picked up when using ASP.N

2019-06-09 16:51发布

问题:

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?

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


回答2:

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>