【问题标题】:Localisation in Asp.Net Core 3.1 not working after updatig from versin 2.2 [duplicate]从版本 2.2 更新后,Asp.Net Core 3.1 中的本地化不起作用 [重复]
【发布时间】:2020-02-03 14:36:02
【问题描述】:

我们在 Asp.net 核心版本 2.2 中有一个网站。 我们已将网站从 2.2 版更新到 3.1 版。

将应用程序更新到 3.1 版后,我们在 2.2 版中用于本地化的相同代码无法正常工作。

我已将代码放在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();
}

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

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

localizerService.GetLocalizedHtmlString("my_string")

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

  1. SharedResource.cs
  2. SharedResource.en-US.resx
  3. SharedResource.nl.resx

将网站从 .net 核心版本 2.2 更新到版本 3.1 后,值没有转换为所选语言。

【问题讨论】:

  • 你能显示 v3.1 的 startup.cs 吗?
  • @LazZiya 我已经编辑了帖子显示提到了startup.cs的内容
  • 它仍然无法正常工作。你能检查我创建的演示应用程序并建议我在那里做错了吗github.com/gurpreet42/MyAppV3我怎样才能使它工作。

标签: c# asp.net-core .net-core


【解决方案1】:

startup.cs 文件仍然指向 v2.2 并使用我注意到的一些不推荐使用的功能。

我刚刚更新了startup 以使用 v3.1,它运行良好:

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.AddRazorPages()
        .AddViewLocalization()
        .AddDataAnnotationsLocalization(options =>
        {
            options.DataAnnotationLocalizerProvider = (type, factory) =>
            {
                var assemblyName = new AssemblyName(typeof(SharedResource).GetTypeInfo().Assembly.FullName);
                return factory.Create("SharedResource", assemblyName.Name);
            };
        });
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();

    app.UseRouting();

    app.UseAuthorization();

    var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
    app.UseRequestLocalization(locOptions.Value);

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapRazorPages();
    });
}

【讨论】:

  • @GurpreetKailey,很高兴知道它正在工作。做更多的改变取决于你想要达到的目标。一般来说,要完全本地化 ASP.Net Core Web 应用程序(视图、DataAnnotations、ModelBinding、IdentityErrors、客户端验证等)还有很多工作要做。有关更多详细信息,您可以查看official docs。此外,this article 可以帮助您有一个整体的想法,并更快、更轻松地进行本地化。
  • 在上一条评论中这是一个错字,实际上它不起作用。你能检查一下我创建的演示应用程序并让它工作吗? github.com/gurpreet42/MyAppV3
  • 感谢@LazZiya 的帮助,我们能够找到根本原因。
猜你喜欢
  • 1970-01-01
  • 2020-03-16
  • 1970-01-01
  • 2021-01-20
  • 1970-01-01
  • 2019-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多