asp.net - .net core 3.1 通过 URL 进行本地化/文化 - localhost/en/area?/controller/action

标签 asp.net .net asp.net-mvc asp.net-core asp.net-core-3.1

我一直在努力本地化 Core 3.1,但目前我终于用他们的新行为做到了(令人惊讶的是他们的文档没有针对新更改进行更新)。

话虽如此,我似乎找不到一种基于 URL 进行文化的好方法,例如:https://localhost/en/area?/controller/action

注意:几天前我找到了一个解决方案,但它不适用于身份(身份对我的工作至关重要)

拜托,我需要你的帮助。

当前设置:-

配置服务

            services.AddLocalization();

            services.AddMvc()
                .AddMvcLocalization()
                .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
                .AddDataAnnotationsLocalization(options =>
                {
                    options.DataAnnotationLocalizerProvider = (type, factory) =>
                        factory.Create(typeof(SharedResource));
                });


            services.Configure<RequestLocalizationOptions>(options =>
            {
                var supportedCultures = new List<CultureInfo>
                    {
                        new CultureInfo("en"),
                        new CultureInfo("ar")
                    };

                options.DefaultRequestCulture = new RequestCulture("en");
                options.SupportedCultures = supportedCultures;
                options.SupportedUICultures = supportedCultures;
            });

配置

            var supportedCultures = new[]
            {
                new CultureInfo("en"),
                new CultureInfo("ar"),
            };

            app.UseRequestLocalization(new RequestLocalizationOptions
            {
                DefaultRequestCulture = new RequestCulture("en"),
                // Formatting numbers, dates, etc.
                SupportedCultures = supportedCultures,
                // UI strings that we have localized.
                SupportedUICultures = supportedCultures
            });

HomeController 将用户的选择保存在 cookie 中

        [HttpPost]
        public IActionResult SetLanguage(string culture, string returnUrl)
        {
            Response.Cookies.Append(
                CookieRequestCultureProvider.DefaultCookieName,
                CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
                new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
            );

            return LocalRedirect(returnUrl);
        }

最佳答案

配置RouteDataRequestCultureProviderRequestLocalizationOptions中:

services.Configure<RequestLocalizationOptions>(options =>
{
    // ...

    options.RequestCultureProviders.Insert(0, 
        new RouteDataRequestCultureProvider { Options = options } 
    );
});

如果您使用 Controller ,请配置端点以使用 {culture} 路由值:

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{culture=en}/{controller=Home}/{action=Index}/{id?}");
    endpoints.MapRazorPages();
});

关于asp.net - .net core 3.1 通过 URL 进行本地化/文化 - localhost/en/area?/controller/action,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59941181/

相关文章:

c# - 我想知道如何将值传递给另一个 View 并在单击 asp.net mvc 中的更新按钮时更新我的​​详细信息

c# - 使用 Nhibernate Criteria Api 查询集合?

javascript - 为什么不允许在 jquery 数据表服务器端处理 ajax 中使用成功?

javascript - 更改对加载的脚本没有影响

ASP.NET MVC Core 找不到匹配命令 "dotnet-bundle"的可执行文件

c# - 如何修复试图匹配 1-100 个字符的字母、数字、空格、下划线和破折号的 C# 正则表达式

c# - 如何访问一个窗口?

c# - Razor 页面中 VB.NET 的 @@<text> 标签问题

c# - 尝试在 MVC4 中使用 ViewModel 进行分页

javascript - 在网络中存储一些选项