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

查看:23
本文介绍了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)

如果class SharedResource.csSharedResource.*.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
    {
    }
}

并读取资源文件到Resources文件夹.

And read resource files to the Resources folder.

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

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