问题
I created my first app with ASP.NET Core. When I debug it, I see a problem with words that have accents:
How can I correctly localize the application?
Update:
I tried to implement Joe's suggestion, but I didn't get the expected result as you can see in this image.
The strings displayed from the database are okay, but the strings used in the view template like title or text are displayed incorrectly.
I don't want a multi-language application, just one in português.
On the old asp.net this configurations was done on .config with element
text html
回答1:
in project.json you need this dependency
"Microsoft.Extensions.Localization": "1.0.0-rc2-final",
in Startup.cs in ConfigureServices you need code like this:
services.AddLocalization(options => options.ResourcesPath = "GlobalResources");
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("en"),
new CultureInfo("fr-FR"),
new CultureInfo("fr"),
};
// State what the default culture for your application is. This will be used if no specific culture
// can be determined for a given request.
options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");
// You must explicitly state which cultures your application supports.
// These are the cultures the app supports for formatting numbers, dates, etc.
options.SupportedCultures = supportedCultures;
// These are the cultures the app supports for UI strings, i.e. we have localized resources for.
options.SupportedUICultures = supportedCultures;
// You can change which providers are configured to determine the culture for requests, or even add a custom
// provider with your own logic. The providers will be asked in order to provide a culture for each request,
// and the first to provide a non-null result that is in the configured supported cultures list will be used.
// By default, the following built-in providers are configured:
// - QueryStringRequestCultureProvider, sets culture via "culture" and "ui-culture" query string values, useful for testing
// - CookieRequestCultureProvider, sets culture via "ASPNET_CULTURE" cookie
// - AcceptLanguageHeaderRequestCultureProvider, sets culture via the "Accept-Language" request header
//options.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(async context =>
//{
// // My custom request culture logic
// return new ProviderCultureResult("en");
//}));
});
in Configure you need code something like this:
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
I have some working demo code here, if you need more
来源:https://stackoverflow.com/questions/37709060/how-to-change-the-default-culture