从 .NET Core 2 迁移到 .NET Core 7:启动 .cs 中的本地化更改

Migrating from .NET Core 2 to .NET Core 7: Changes for Localization in Startup.cs

提问人:Jeswin Pathrose 提问时间:11/10/2023 最后编辑:Jeswin Pathrose 更新时间:11/10/2023 访问量:35

问:

我正在将 .NET Core 2 应用程序升级到 .NET Core 7。作为此升级的一部分,我将专门研究 Startup.cs 文件中的本地化设置。

我在 .NET Core 2 中使用了以下配置进行本地化:

 services.Configure<RequestLocalizationOptions>(opts =>
            {
                var supportedCultures = new List<CultureInfo>
                {
                    new CultureInfo("en"),
                    new CultureInfo("ar"),
                    new CultureInfo("ar-AE"),
                    new CultureInfo("en-GB"),
                    new CultureInfo("fr-FR"),
                    new CultureInfo("fr"),
                    new CultureInfo("en-US"),
                };

                opts.DefaultRequestCulture = new RequestCulture("ar-AE");
                // Formatting numbers, dates, etc.
                opts.SupportedCultures = supportedCultures;
                // UI strings that we have localized.
                opts.SupportedUICultures = supportedCultures;
                var defaultCookieRequestProvider =
                   opts.RequestCultureProviders.FirstOrDefault(rcp =>
                       rcp.GetType() == typeof(CookieRequestCultureProvider));
                if (defaultCookieRequestProvider != null)
                    opts.RequestCultureProviders.Remove(defaultCookieRequestProvider);
                opts.RequestCultureProviders.Insert(0,
                   new CookieRequestCultureProvider()
                   {
                       CookieName = ".AspNetCore.Culture",
                       Options = opts
                   });
            });
            services.AddLocalization(options => options.ResourcesPath = "Resources");
            services.AddMvc()
                .AddViewLocalization((LanguageViewLocationExpanderFormat.Suffix))
                .AddDataAnnotationsLocalization(options =>
                {
                    options.DataAnnotationLocalizerProvider = (type, factory) =>
                    {
                        var assemblyName = new AssemblyName(typeof(SharedResource).GetTypeInfo().Assembly.FullName);
                        return factory.Create("SharedResources", assemblyName.Name);
                    };
                });
          
      var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
            app.UseRequestLocalization(locOptions.Value);

视图

@SharedHtmlLocalizer["index"]

但是,我不确定我需要进行哪些必要的更改来适应向 .NET Core 7 的迁移。具体来说,关于在更新的 Startup.cs 中设置本地化所需的更改。

我已经查看了迁移指南和文档,但希望能获得更具体的见解或指导,以便在新版本中调整本地化设置。在此迁移过程中,我应该考虑在 .NET Core 7 中处理本地化的方式是否有任何重大变化?

C# asp.net 本地化 ASP.net-core-localization

评论

0赞 freedomn-m 11/10/2023
这个答案可能对你有所帮助: stackoverflow.com/a/56749905/2181514
0赞 Ruikai Feng 11/16/2023
在 IServiceCollection 生成之前配置 RequestLocalizationOptions 后,只需调用 app。UseRequestLocalization() 而不从容器中读取选项

答: 暂无答案