.resx 本地化不适用于 ASP.Net - C#
我创建了两个 .resx 文件 - WebResources.resx 和 WebResources.fr-FR.resx。
两者都包含 ExceptionMessages_SignupWithCard。
为了测试我所做的:
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
Response.Write("Current Culture is " + CultureInfo.CurrentCulture.EnglishName);
Response.Write("<br>Message is: " + Resources.WebResources.ExceptionMessages_SignupWithCard);
问题是我仍然从 WebResources.resx 收到 ExceptionMessages_SignupWithCard 消息,而不是从 WebResources.fr-FR.resx 收到消息。
有人可以帮忙吗?
谢谢。
I've created two .resx files - WebResources.resx and WebResources.fr-FR.resx.
Both contain ExceptionMessages_SignupWithCard.
To test I did:
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
Response.Write("Current Culture is " + CultureInfo.CurrentCulture.EnglishName);
Response.Write("<br>Message is: " + Resources.WebResources.ExceptionMessages_SignupWithCard);
The problem is that I still get the ExceptionMessages_SignupWithCard message from WebResources.resx and not the one from WebResources.fr-FR.resx .
Can someone please assist?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ASP.Net(特别是
ResourceManager
)使用CurrentUICulture
来检索资源。也尝试设置线程的 CurrentUICulture。
ASP.Net (specifically, the
ResourceManager
) usesCurrentUICulture
to retrieve resources.Try setting the Thread's CurrentUICulture as well.
尝试设置 Page.Culture 和 Page.UICulture 而不是直接设置线程区域性。
由于它是一个字符串属性,因此看起来不太好。但是,有很多代码不仅会改变线程文化,还会改变资源管理器。
这似乎是标准方法,而不是分配线程的区域性。
Try setting the Page.Culture and Page.UICulture instead of the Thread Culture directly.
Since it is a string property, it appears that it wouldn't be very good. BUT, there is a lot of code going on to change not only the Threads Cultures, but the ResourceManagers too.
This seems to be the standard approach and not assigning the Thread's culture.
有两种方法可以设置 ASP.NET 网页的 UICulture。
选项 #1 是 Web 浏览器可以通过设置 Web 浏览器中的当前语言来调整 UI 文化(自动)。在 FireFox 中,转到“选项”中的“内容”选项卡,然后单击“语言”,然后添加法语并将其移动到列表顶部。 IE 类似,它在“选项”页面中有一个“语言”按钮,您可以添加法语并将其移至订单顶部。为了让您的网页支持这种自动调整方式,您必须在网页编译指示中设置“UICulture=auto”,如下所示:
<%@ 页面语言="C#" AutoEventWireup="true" UICulture="auto" ... ... ... %>
如果您使用此方法,它将显示它不理解的任何 Web 浏览器语言的默认语言。您只需定义一个文化中立的资源来覆盖一种语言的所有版本,或者您可以定义特定的语言(fr-CA、es-mx 等),它将回退到中性语言文件(fr-fr)如果未定义特定语言。 MSDN 提供了有关汽车文化以及如何向 IE 添加更多网络浏览器语言的更多信息:
http://msdn.microsoft.com/en-us/library/fw69ke6f.aspx
选项 #2 是在网页编译指示中将其设置为特定语言,例如 UICulture="fr-FR",或者在整个站点的 web.config 中将其设置。
MSDN 在下面的链接中解释了这些设置,以及如何通过 web.config 为站点上的所有页面进行设置。
http://msdn.microsoft.com/en-us/library/bz9tc508.aspx
您正在使用的编程方法是有效的,但正如您所注意到的,它只是暂时有效。我使用编程方法来读取通过查询字符串指定的语言并以这种方式更改文化。您可以实现一个 HttpModule 来检查查询字符串并以这种方式设置当前页面请求的区域性。
There are two methods to set the UICulture of an asp.net web page.
Option #1 is that the web browser can adjust the UI Culture (auto), by setting the current Language in the web browser. In FireFox you go the Content tab in Options, and click Languages, then add the French language and move it to the top of the list. IE is similar, it has a Languages button in the Options pages, and you add French and move it to the top of the order. For your web page to support this style of automatic adjustment, you have to set 'UICulture=auto' in web page pragma like so:
<%@ Page Language="C#" AutoEventWireup="true" UICulture="auto" ... ... ... %>
If you use this method, it will display your Default language for any web browser languages that it does not understand. You only have to define a culture-neutral resource to cover all versions of a language, or you can define the specific languages (fr-CA, es-mx, etc) and it will fallback to the neutral language file (fr-fr) if the specific language is not defined. MSDN has more info on the auto culture and how to add more web browser languages to IE:
http://msdn.microsoft.com/en-us/library/fw69ke6f.aspx
Option #2 is to set it in the web page pragma to a specific language, like UICulture="fr-FR", or in the web.config for the whole site.
MSDN explains these settings in the link below, and how you can set it via web.config for all pages on your site.
http://msdn.microsoft.com/en-us/library/bz9tc508.aspx
The programmatic method that you are using works, but as you noticed it only works temporarily. I've used the programmatic method for reading the language specified via the querystring and changing cultures that way. You can implement an HttpModule to check the querystring and set the culture for the current page request this way.