ASP.NET Core 3.1:共享本地化不适用于 3.1 版

Gur*_*ley 9 .net c# .net-core asp.net-core

我可能没有在startup.cs文件中进行正确的配置。我创建了一个演示应用程序以使其正常工作,但是在尝试了各种操作后它不起作用。演示存储库可在以下链接中获得

https://github.com/gurpreet42/MyAppV3

startup.cs 文件的配置是

public void ConfigureServices(IServiceCollection services)
{
   services.AddSingleton<LocService>();
   services.AddLocalization(options => options.ResourcesPath = "Resources");

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

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

   services.AddMvc()
           .AddViewLocalization()
           .AddDataAnnotationsLocalization(options =>
                {
                   options.DataAnnotationLocalizerProvider = (type, factory) =>
                   {
                       var assemblyName = new AssemblyName(typeof(SharedResource).GetTypeInfo().Assembly.FullName);
                       return factory.Create("SharedResource", assemblyName.Name);
                   };
               }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

public void Configure(IApplicationBuilder app,
                        IHostingEnvironment env,
                        ILoggerFactory loggerFactory)
{
    // Localisation
    var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
    app.UseRequestLocalization(locOptions.Value);

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseAuthentication();
    app.UseSession();

    app.UseSession();
    app.UseCookiePolicy();
}
Run Code Online (Sandbox Code Playgroud)

LocService类中的代码是

public class LocService
{
    private readonly IStringLocalizer _localizer;

    public LocService(IStringLocalizerFactory factory)
    {
        var type = typeof(SharedResource);
        var assemblyName = new AssemblyName(type.GetTypeInfo().Assembly.FullName);
        _localizer = factory.Create("SharedResource", assemblyName.Name);
    }

    public LocalizedString GetLocalizedHtmlString(string key)
    {
        var value= _localizer[key];
        return value;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在在我们的控制器上,我们可以访问本地化的字符串

localizerService.GetLocalizedHtmlString("my_string")
Run Code Online (Sandbox Code Playgroud)

在“资源”文件夹下,我们有以下文件

SharedResource.cs
SharedResource.en-US.resx
SharedResource.nl.resx
Run Code Online (Sandbox Code Playgroud)

请建议配置错误的地方还是我需要添加一些额外的包?

Rya*_*yan 14

事实证明,在 asp.net core 3.1 中,您需要放置SharedResource.csResources文件夹外(请参阅此github 问题

如果 classSharedResource.csSharedResource.*.resx在同一文件夹中,则编译的 dll 中的命名空间将是错误的xxx.lang.dll

所以,直接把原来的删掉SharedResource.cs在项目下新建一个:

namespace MyAppV3
{
    public class SharedResource
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

并将资源文件读取到Resources文件夹中。


And*_*nca 8

无需创建 LocService 即可使用 IStringLocalizer。

“资源”文件夹结构

SharedResource.cs
SharedResource.en-US.resx
SharedResource.nl.resx
Run Code Online (Sandbox Code Playgroud)

在类 SharedResource 中,不要在命名空间中添加“Resources”。像 MyAppV3.Resources。请保持它只是 MyAppV3。

namespace MyAppV3
{
    public class SharedResource
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

在您的 .csproj 中添加以下属性

<PropertyGroup><EmbeddedResourceUseDependentUponConvention>false</EmbeddedResourceUseDependentUponConvention></PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

添加到 startup.cs > ConfigureServices

    services.AddLocalization(options => options.ResourcesPath = "Resources");
    services.AddScoped<IStringLocalizer, StringLocalizer<SharedResource>>();

    services
        .Configure<RequestLocalizationOptions>(options =>
            {
                var cultures = new[]
                                   {
                                       new CultureInfo("en"),
                                       new CultureInfo("nl")
                                   };
                options.DefaultRequestCulture = new RequestCulture("en");
                options.SupportedCultures = cultures;
                options.SupportedUICultures = cultures;
            });
Run Code Online (Sandbox Code Playgroud)

Startup.cs > 配置

   app.UseRequestLocalization(app.ApplicationServices
            .GetRequiredService<IOptions<RequestLocalizationOptions>>().Value);
Run Code Online (Sandbox Code Playgroud)

将 IStringLocalizer 参数添加到控制器。

   public MyTestController(IStringLocalizer localizer)
   {
        this.localizer = localizer;
   }

   public IActionResult Get()
   {          
        var value = this.localizer.GetString("RessourceName");
        return this.Ok(value);
   }
Run Code Online (Sandbox Code Playgroud)