ASP.NET Core 3.1:共享本地化不适用于版本3.1 [英] ASP.NET Core 3.1 : Shared Localization not working for version 3.1

查看:230
本文介绍了ASP.NET Core 3.1:共享本地化不适用于版本3.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能没有在 startup.cs 文件中进行正确的配置。我已经创建了一个演示应用程序来使其正常运行,但是在尝试了各种尝试之后,它却无法正常工作。演示存储库位于以下链接

I may be not doing the correct configurations in the startup.cs file. I have created a demo application to make it working, but after trying various things it is not working. The demo repository is available at following link

https:/ /github.com/gurpreet42/MyAppV3

startup.cs文件的配置为

Configurations of startup.cs files are

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();
}

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;
    }
}

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

Now on our controller, we can access the localized string as

localizerService.GetLocalizedHtmlString("my_string")

在资源文件夹下,存在以下文件

Under the "Resources" folder we have following files present

SharedResource.cs
SharedResource.en-US.resx
SharedResource.nl.resx

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

Please suggest where the configurations are wrong or do I need to add some extra package?

推荐答案

事实证明,在ASP.NET Core中3.1,您需要将 SharedResource.cs 放在 Resources 文件夹中(请参阅此 github问题

It turns out that in asp.net core 3.1, you need to place SharedResource.cs out of Resources folder(see this github issue)

如果类 SharedResource .cs SharedResource。*。resx 在同一文件夹中,命名空间在编译的dll xxx.lang中将是错误的。 dll

If class SharedResource.cs and SharedResource.*.resx in same folder, the namespace will be error in compiled dll xxx.lang.dll.

所以删除原始 SharedResource.cs 直接在项目下创建一个新文件:

So, just delete original SharedResource.cs create a new one under the project directly:

namespace MyAppV3
{
    public class SharedResource
    {
    }
}

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

And readd resource files to the Resources folder.

这篇关于ASP.NET Core 3.1:共享本地化不适用于版本3.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆