Resource file not being picked up when using ASP.NET expression

北慕城南 提交于 2019-12-23 05:22:12

问题


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>


来源:https://stackoverflow.com/questions/30909716/resource-file-not-being-picked-up-when-using-asp-net-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!