IStringLocalizer<T> 在 Asp.Net Core Blazor Server 中无法正常工作。

huangapple go评论77阅读模式
英文:

IStringLocalizer<T> doesn't work in Asp.Net core Blazor Server

问题

已经按如下方式设置了本地化:

为项目设置中性语言,

&lt;NeutralLanguage&gt;ko-KR&lt;/NeutralLanguage&gt;

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

builder.Services.AddLocalization(options =&gt;
        {
            options.ResourcesPath = &quot;Resources&quot;;
        }) ;

...

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

...

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

UIStrings.resx
UIStrings.Designer.cs

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

Name : &quot;이름&quot;

FYI,“이름”是英文“name”的韩文翻译。

然后,我看到以下结果:

...
@inject IStringLocalizer&lt;UIStrings&gt; Localizer

...

@UIStrings.Name                              // 输出“이름”
@UIStrings.ResourceManager.GetString(&quot;Name&quot;) // 输出“이름”
@Localizer[&quot;Name&quot;]                            // 输出“Name” <= 不好

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

英文:

I have set it up for localization as follows:

Set the neutralLanguage for the project,

// .csproj
&lt;NeutralLanguage&gt;ko-KR&lt;/NeutralLanguage&gt;

Registered localization services into the service container,

// program.cs
builder.Services.AddLocalization(options =&gt;
        {
            options.ResourcesPath = &quot;Resources&quot;;
        }) ;

...

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

...

Created a .resx file in ~\Resource directory with VS 2022, which created .Designer.cs in the directory as per to my configuration

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

Added a string resource into UIStrings.resx, for example:

Name : &quot;이름&quot; 

FYI, "이름" is Korean for English "name".

and, I am seeing the below result:

// .razor
...
@inject IStringLocalizer&lt;UIStrings&gt; Localizer

...

@UIStrings.Name                              // prints &quot;이름&quot;
@UIStrings.ResourceManager.GetString(&quot;Name&quot;) // prints &quot;이름&quot;
@Localizer[&quot;Name&quot;]                            // prints &quot;Name&quot; &lt;= not good

I don't know why IStringLocalizer<T> doesn't work.

答案1

得分: 0

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

英文:

Try:

@inject IStringLocalizer&lt;UIStrings&gt; Localizer;
&lt;p&gt;@Localizer[UIStrings.Name]&lt;/p&gt;

答案2

得分: 0

我尝试了一些代码,并了解了IStringLocalizer&lt;T&gt;的行为以及与ResourceManager的行为有何不同。

我的问题失败是因为IStringLocalizer的不同文件位置行为。

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

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Localization;
using ResourceManagementExam.Controls;
using ResourceManagementExam.Pages;
using ResourceManagementExam.Resources; // &lt;== 用于识别 Resource1 类
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 =&gt; options.ResourcesPath = &quot;Resources&quot;);
        var app = builder.Build();

        // ResourceManager
        // 默认情况下,要求参数的根名称与.resx的根名称相同。
        var rm = new ResourceManager(typeof(Resource1)); 

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

        // IStringLocalizer&lt;T&gt;
        // options.ResourcesPath = &quot;Resources&quot;,这是相对根路径。
        var programLocalizer = app.Services.GetRequiredService&lt;IStringLocalizer&lt;SharedResource&gt;&gt;();
        Prints(programLocalizer);

        var indexPageLocalizer = app.Services.GetRequiredService&lt;IStringLocalizer&lt;IndexPage&gt;&gt;();

        Prints(indexPageLocalizer);

        var searchBarLocalizer = app.Services.GetRequiredService&lt;IStringLocalizer&lt;SearchBar&gt;&gt;();

        Prints(searchBarLocalizer);
    }

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

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

项目文件夹结构如下:

英文:

I tried some code and found how IStringLocalizer&lt;T&gt; behaves and how it differs from the behavior of ResourceManager.

The failure of my question was from different file-location behavior of IStringLocalizer.

All relevant comments are added between code shown here:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Localization;
using ResourceManagementExam.Controls;
using ResourceManagementExam.Pages;
using ResourceManagementExam.Resources; // &lt;== 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 =&gt; options.ResourcesPath = &quot;Resources&quot;);
        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($&quot;UI Culture: {Thread.CurrentThread.CurrentUICulture}&quot;);
        Console.WriteLine(&quot;Hello : &quot; + rm.GetString(&quot;Hello&quot;));
        Console.WriteLine();


        // IStringLocalizer&lt;T&gt;
        // options.ResourcesPath = &quot;Resources&quot;, which is the relative root path.
        var programLocalizer = app.Services.GetRequiredService&lt;IStringLocalizer&lt;SharedResource&gt;&gt;();
        Prints(programLocalizer);

        var indexPageLocalizer = app.Services.GetRequiredService&lt;IStringLocalizer&lt;IndexPage&gt;&gt;();

        Prints(indexPageLocalizer);

        var searchBarLocalizer = app.Services.GetRequiredService&lt;IStringLocalizer&lt;SearchBar&gt;&gt;();

        Prints(searchBarLocalizer);
    }

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

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

The project folder structure is:

IStringLocalizer<T> 在 Asp.Net Core Blazor Server 中无法正常工作。

huangapple
  • 本文由 发表于 2023年6月9日 11:27:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437024.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定