如何更改默认的文化? [英] How to change the default culture?

查看:155
本文介绍了如何更改默认的文化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造了我的ASP.NET核心首次应用。当我调试它,我看到一个问题,有口音的话:

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?

更新:

我试图实现乔的建议,但我并没有得到预期的结果,你可以在此图像中看到。

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.

在旧的asp.net这个配置于做的.config与元素

On the old asp.net this configurations was done on .config with element

文本HTML

推荐答案

在project.json你需要这种依赖性

in project.json you need this dependency

"Microsoft.Extensions.Localization": "1.0.0-rc2-final",

在Startup.cs在ConfigureServices你需要code是这样的:

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");
            //}));
        });

在配置你需要code是这样的:

in Configure you need code something like this:

var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
        app.UseRequestLocalization(locOptions.Value);

我有一些这里工作演示code 的,如果你需要更多的

这篇关于如何更改默认的文化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆