IStringLocalizer<T> 在 Asp.Net core Blazor Server 中不起作用

问题描述 投票:0回答:2

我已将其本地化设置如下:

为项目设置中性语言,

// .csproj
<NeutralLanguage>ko-KR</NeutralLanguage>

将本地化服务注册到服务容器中,

// program.cs
builder.Services.AddLocalization(options =>
        {
            options.ResourcesPath = "Resources";
        }) ;

...

app.UseRequestLocalization(new RequestLocalizationOptions()
     .AddSupportedCultures(new[] { "ko-KR", "en-US" })
     .AddSupportedUICultures(new[] { "ko-KR", "en-US"}));

...

使用 VS 2022 在 ~\Resource 目录中创建了一个 .resx 文件,该文件根据我的配置在该目录中创建了 .Designer.cs

//~\Resources
UIStrings.resx
UIStrings.Designer.cs

在UIStrings.resx中添加了字符串资源,例如:

Name : "이름" 

仅供参考,“름”在韩语中是英语“名字”的意思。

并且,我看到以下结果:

// .razor
...
@inject IStringLocalizer<UIStrings> Localizer

...

@UIStrings.Name                              // prints "이름"
@UIStrings.ResourceManager.GetString("Name") // prints "이름"
@Localizer["Name"]                            // prints "Name" <= not good

我不知道为什么 IStringLocalizer 不起作用。

localization blazor resourcemanager
2个回答
0
投票

尝试:

@inject IStringLocalizer<UIStrings> Localizer;
<p>@Localizer[UIStrings.Name]</p>

0
投票

我尝试了一些代码,发现

IStringLocalizer<T>
的行为方式以及它与
ResourceManager
的行为有何不同。

我的问题失败是由于

IStringLocalizer
的不同文件位置行为。

所有相关注释都添加在此处显示的代码之间:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Localization;
using ResourceManagementExam.Controls;
using ResourceManagementExam.Pages;
using ResourceManagementExam.Resources; // <== to identify Resource1 class
using System.Globalization;
using System.Resources;

namespace ResourceManagementExam;
internal class Program
{
    private static void Main(string[] args)
    {
        var builder = Host.CreateApplicationBuilder(args);

        builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
        var app = builder.Build();

        // ResourceManager
        // requires the root name of the argument to be identical to that of .resx by default.         
        var rm = new ResourceManager(typeof(Resource1)); 

        Console.WriteLine($"UI Culture: {Thread.CurrentThread.CurrentUICulture}");
        Console.WriteLine("Hello : " + rm.GetString("Hello"));
        Console.WriteLine();


        // IStringLocalizer<T>
        // options.ResourcesPath = "Resources", which is the relative root path.
        var programLocalizer = app.Services.GetRequiredService<IStringLocalizer<SharedResource>>();
        Prints(programLocalizer);

        var indexPageLocalizer = app.Services.GetRequiredService<IStringLocalizer<IndexPage>>();

        Prints(indexPageLocalizer);

        var searchBarLocalizer = app.Services.GetRequiredService<IStringLocalizer<SearchBar>>();

        Prints(searchBarLocalizer);
    }

    private static void Prints(IStringLocalizer localizer)
    {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("ko-KR");
        Console.WriteLine($"UI Culture: {Thread.CurrentThread.CurrentUICulture}");
        Console.WriteLine("Hello : " + localizer["Hello"]);
        Console.WriteLine();

        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
        Console.WriteLine($"UI Culture: {Thread.CurrentThread.CurrentUICulture}");
        Console.WriteLine("Hello : " + localizer["Hello"]);
        Console.WriteLine();
    }
}

项目文件夹结构为:

© www.soinside.com 2019 - 2024. All rights reserved.