ASP.net is not using other locale resource files
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 resourc开发者_StackOverflow社区e 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.
Try setting UICulture="auto"
and Culture="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:
<system.web>
<globalization uiCulture="auto" culture="auto" />
</system.web>
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, on BeginRequest
.
To read the language setting from the browser you can use something along the lines of:
var languages = Request.UserLanguages
if (languages != null)
{
var lang = languages[0];
Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
}
If you want to also affect the datetime, number formats etc, then also set CurrentCulture
.
Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);
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:
string selectedCulture = browserPreferredCulture;
Thread.CurrentThread.CurrentUICulture = new
CultureInfo(selectedCulture);
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(selectedCulture);
See http://msdn.microsoft.com/en-us/library/bz9tc508.aspx for an example as a starting point.
精彩评论