通过RequestCultureProviders处理路由中的文化(URL)

4
1个回答

12

Startup.cs

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // Add the localization services to the services container
        services.AddLocalization(options => options.ResourcesPath = "Resources");

        // Add framework services.
        services.AddMvc()
            // Add support for finding localized views, based on file name suffix, e.g. Index.fr.cshtml
            .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
            // Add support for localizing strings in data annotations (e.g. validation messages) via the
            // IStringLocalizer abstractions.
            .AddDataAnnotationsLocalization();

        // Configure supported cultures and localization options
        services.Configure<RequestLocalizationOptions>(options =>
        {
            var supportedCultures = new[]
            {
                new CultureInfo("en"),
                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: "fr", uiCulture: "fr");

            // 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 RouteCultureProvider(options.DefaultRequestCulture));
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        var localizationOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
        app.UseRequestLocalization(localizationOptions.Value);

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "InternationalizationDefault",
                template: "{culture=en}/{controller=Home}/{action=Index}/{id?}");
        });
        // ...
    }
}

RouteCultureProvider.cs文件中

public class RouteCultureProvider : IRequestCultureProvider
{
    private CultureInfo defaultCulture;
    private CultureInfo defaultUICulture;

    public RouteCultureProvider(RequestCulture requestCulture)
    {
        this.defaultCulture = requestCulture.Culture;
        this.defaultUICulture = requestCulture.UICulture;
    }

    public Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
    {
        //Parsing language from url path, which looks like "/en/home/index"
        PathString url = httpContext.Request.Path;

        // Test any culture in route
        if (url.ToString().Length <= 1)
        {
            // Set default Culture and default UICulture
            return Task.FromResult<ProviderCultureResult>(new ProviderCultureResult(this.defaultCulture.TwoLetterISOLanguageName, this.defaultUICulture.TwoLetterISOLanguageName));
        }

        var parts = httpContext.Request.Path.Value.Split('/');
        var culture = parts[1];

        // Test if the culture is properly formatted
        if (!Regex.IsMatch(culture, @"^[a-z]{2}(-[A-Z]{2})*$"))
        {
            // Set default Culture and default UICulture
            return Task.FromResult<ProviderCultureResult>(new ProviderCultureResult(this.defaultCulture.TwoLetterISOLanguageName, this.defaultUICulture.TwoLetterISOLanguageName));
        }

        // Set Culture and UICulture from route culture parameter
        return Task.FromResult<ProviderCultureResult>(new ProviderCultureResult(culture, culture));
    }
}

添加 Resources/Views/View.en.resx 文件,包含键值对 HelloWorld: Hello World !

添加 Resources/Views/View.fr.resx 文件,包含键值对 HelloWorld: Bonjour tout le monde !

View.cshtml 中:

@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer

<h2>Localizer["HelloWorld"]</h2>

就这样 :)


只有在所需的文化环境下才能正常工作。对于可选的文化环境路径,请查看丹尼尔的优秀答案:https://dev59.com/4lsV5IYBdhLWcg3wwhHw#35970676 - Tseng
我对通过请求和解析字符串从上下文中检索文化的方式不满意。最初,我尝试从GetRouteData或GetRouteValues扩展方法获取,但失败了(返回null)。有什么想法来增强代码吗? - rdhainaut
这是因为IRouteFeature尚未注册。由于我在RC2发布之前就提出了这个问题,所以我将代码保留了下来,没有进行GetRouteData和移植。我只需要查看 Mvc 如何注册路由器,并在本地化中间件之前注册它即可。在 Mvc 之后注册本地化无法正常工作,因为已经太晚了。 - Tseng

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接