.NET Core 3.1 API找不到包含区域代码的共享资源文件 上下文注释问题

问题描述

上下文

我目前正在使用.net Core 3.1 API,它将取代旧的.NET Framework和Delphi后端。 API需要支持全球化和本地化,以翻译错误消息和一些数据值。 本地化通过路线传递,例如: Resources。因此,已连接的应用程序可以快速从本地化切换。 该API有一个Resources: - SharedResources.resx default and fallback language file nl-NL) - SharedResources.en-US.resx - SharedResources.de-DE.resx 文件夹,其中包含以下文件

Startup.cs

public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // Extensions method that execute the install function on all classes that implement // IInstaller interface services.InstallServicesInAssembly(Configuration); } } 中,我使用安装程序来分离问题。

  public class LocalizationInstaller : IInstaller
    {
        public void InstallServices(IServiceCollection services,IConfiguration configuration)
        {
            var localizationConfig = new LocalizationConfig();
            configuration.GetSection(nameof(LocalizationConfig)).Bind(localizationConfig);
            services.AddSingleton(localizationConfig);

            services.AddLocalization(options => options.ResourcesPath = LocalizationConfig.ResourcePath);

            services.Configure<RequestLocalizationoptions>(options =>
            {
                options.DefaultRequestCulture = LocalizationConfig.GetDefaultRequestCulture();
                options.SupportedCultures = LocalizationConfig.GetSupportedCultures();
                options.SupportedUICultures = LocalizationConfig.GetSupportedCultures();
                options.RequestCultureProviders = new[] { new RouteDataRequestCultureProvider { Options = options } };
            });

            services.Configure<RouteOptions>(options =>
            {
                options.ConstraintMap.Add("culture",typeof(LanguageRouteConstraint));
            });

        }

我为本地化制作了一个特定的安装程序:

LocalizationConfig

"LocalizationConfig": { "ResourcePath": "Resources","SupportedCultures": [ "nl-NL","en-US","de-DE" ],"DefaultRequestCulture": "nl-NL" } 的数据来自appsettings.json文件

[HttpGet("{version}/{culture}/Index")]
public IActionResult Index()
{
  var culture = $"CurrentCulture{CultureInfo.CurrentCulture.Name},CurrentUICulture{CultureInfo.CurrentUICulture.Name}";
  var value = _localizer["HelloWorld"].Value;
  return Ok(value);
}

问题

http://localhost/1/nl-NL/Controller/Index

当我向区域性为nl-NL或随机值的API发出请求时,它会像预期的那样在SharedResources.resx中返回认值。 例如:Hallo Wereld!返回SharedResources.rex

但是,当我在使用美国或德国的区域性进行请求时,它仍然从SharedResources.en-US.resx文件而不是SharedResources.de-DE.resx文件var culture文件中返回值 而我的变量SharedResoures.en-US.resx中的区域性设置为en-US或de-DE。

注释

当我将SharedResources.en.resx命名为ArrayList时,它似乎可以正常工作,并且应用程序会找到翻译文件。但是,这并不能解决我想要指定区域代码以便可以同时支持en-GB和en-US的问题。

问题

为什么应用程序可以找到没有区域代码而不包含区域代码文件

Microsoft文档:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-3.1

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)