ASP.net 不使用其他区域设置资源文件
我有一个 Default.aspx
文件,我在其中获取本地化值:
Default.aspx:
<asp:Localize meta:resourcekey="lblTitle" runat="server">Welcome</asp:Localize>
然后我创建一个匹配的后备资源文件:
Default.aspx.resx:
lblTitle.Text Welcome to Stackoverflow Localized
并且有效:
现在我想创建,例如,一个法国人本地化:
Default.aspx.fr.resx:
lblTitle.Text Bienvenue Stackoverflow
我更改浏览器以发送法语区域设置:
(确实如此):
GET http://stackoverflow.com/ HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */*
Accept-Language: fr-CH,qps-ploc;q=0.5
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E)
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Host: stackoverflow.com
除了它不起作用:
我正在关注什么微软表示:
例如,如果 App_LocalResources 文件夹中有一个名为 Default.aspx 的页面,则可以创建以下文件:
默认.aspx.resx
。如果未找到语言匹配,则这是默认的本地资源文件(后备资源文件)。
默认.aspx.es.resx
。这是西班牙语的资源文件,没有文化信息。
默认.aspx.es-mx.resx
。这是专门针对西班牙语(墨西哥)的资源文件。
默认.aspx.fr.resx
。这是法语资源文件,没有文化信息。
为什么 .NET 没有做 .NET 应该做的事情?
更新:
来自 MSDN:
选择不同语言的资源文件
ASP.NET 可以将页面的 UICulture 和 Culture 属性设置为浏览器传递的语言和文化值。 ...有关详细信息,请参阅如何:设置 ASP 的区域性和 UI 区域性.NET 网页全球化。
如何让 ASP.NET 将页面的 UICulture
和 Culture
属性设置为传递的语言和文化值通过浏览器?
来自如何:设置 ASP.NET 网页的区域性和 UI 区域性全球化:
用户可以设置用户界面文化和浏览器文化。为了 例如,在 Microsoft Internet Explorer 中,用户可以在“工具”菜单上 单击“Internet 选项”,在“常规”选项卡上,单击“语言”,然后单击 设置他们的语言偏好。如果启用ClientBasedCulture Web.config 文件中全球化元素的属性已设置 为true时,ASP.NET可以设置UI文化和网页文化 根据浏览器发送的值自动进行。
以声明方式设置 ASP.NET 网页的区域性和 UI 区域性
- 要让 ASP.NET 将 UI 区域性和区域性设置为当前浏览器设置中指定的第一种语言,请将 UICulture 和 Culture 设置为 auto。或者,您可以将此值设置为 auto:culture_info_name,其中 culture_info_name 是区域性名称。有关区域性名称的列表,请参阅 CultureInfo。您可以在 @ Page 指令或 Web.config 中进行此设置文件。
i have a Default.aspx
file where i fetch localized values:
Default.aspx:
<asp:Localize meta:resourcekey="lblTitle" runat="server">Welcome</asp:Localize>
i then create a matching fallback resource file:
Default.aspx.resx:
lblTitle.Text Welcome to Stackoverflow Localized
And that works:
Now i want to create, for example, a French localization:
Default.aspx.fr.resx:
lblTitle.Text Bienvenue Stackoverflow
And i change my browser to send a french language locale:
(which it does):
GET http://stackoverflow.com/ HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */*
Accept-Language: fr-CH,qps-ploc;q=0.5
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E)
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Host: stackoverflow.com
Except that it just doesn't work:
i'm following what Microsoft says:
For example, if you have a page named Default.aspx in the App_LocalResources folder, you might create the following files:
Default.aspx.resx
. This is the default local resource file (the fallback resource file) if no language match is found.
Default.aspx.es.resx
. This is the resource file for Spanish, without culture information.
Default.aspx.es-mx.resx
. This is the resource file for Spanish (Mexico) specifically.
Default.aspx.fr.resx
. This is the resource file for French, without culture information.
Why is .NET doing not what .NET should be doing?
Update:
From MSDN:
Selecting Resource Files for Different Languages
ASP.NET can set the UICulture and Culture properties for the page to the language and culture values that are passed by the browser. ... For detailed information, see How to: Set the Culture and UI Culture for ASP.NET Web Page Globalization.
How can i get ASP.NET to set the UICulture
and Culture
properties for the page to the language and culture values that are passed by the browser?
From How to: Set the Culture and UI Culture for ASP.NET Web Page Globalization:
Users can set the UI culture and culture in their browsers. For
example, in Microsoft Internet Explorer, on the Tools menu, users can
click Internet Options, on the General tab, click Language, and then
set their language preference. If the enableClientBasedCulture
attribute of the globalization element in the Web.config file is set
to true, ASP.NET can set the UI culture and culture for a Web page
automatically, based on the values that are sent by a browser.To set the culture and UI culture for an ASP.NET Web page declaratively
- To have ASP.NET set the UI culture and culture to the first language that is specified in the current browser settings, set UICulture and Culture to auto. Alternatively, you can set this value to auto:culture_info_name, where culture_info_name is a culture name. For a list of culture names, see CultureInfo. You can make this setting either in the @ Page directive or Web.config file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试在 .aspx 文件的
@ Page
指令中设置UICulture="auto"
和Culture="auto"
。如何:设置 ASP.NET 网页全球化的区域性和 UI 区域性
或者,您可以在 web.config 中完成相同的操作,只不过它适用于每个页面:
Try setting
UICulture="auto"
andCulture="auto"
in the@ Page
directive in your .aspx file.How to: Set the Culture and UI Culture for ASP.NET Web Page Globalization
Or, you can accomplish the same thing in the web.config, except it would apply to every page:
默认情况下,浏览器语言不会影响应用程序区域设置。您需要添加一些代码来实现此目的。一种方法是在
BeginRequest
上的Global.asax
或 HttpModule 中添加一些代码。要从浏览器读取语言设置,您可以使用以下内容:
如果您还想影响日期时间、数字格式等,则还要设置
CurrentCulture
。By default the browser language doesn't affect the application locale. You need to add some code to achieve this. One way is to add some code in
Global.asax
or a HttpModule, onBeginRequest
.To read the language setting from the browser you can use something along the lines of:
If you want to also affect the datetime, number formats etc, then also set
CurrentCulture
.我不认为 ASP.NET 运行时默认设置处理线程的 UI 区域性。您必须明确分配它。您可以使用自己的自定义 HttpModule 甚至在 Global.asax.cs 中执行此操作。
类似的内容:
请参阅 http://msdn.microsoft.com/en- us/library/bz9tc508.aspx 作为起点的示例。
I don't believe that the ASP.NET runtime by default sets the processing thread's UI culture. You have to explicitly assign it. You can do this with your own custom HttpModule, or even in your Global.asax.cs.
Something along the lines of:
See http://msdn.microsoft.com/en-us/library/bz9tc508.aspx for an example as a starting point.