英文:
IStringLocalizer<T> doesn't work in Asp.Net core Blazor Server
问题
已经按如下方式设置了本地化:
为项目设置中性语言,
<NeutralLanguage>ko-KR</NeutralLanguage>
将本地化服务注册到服务容器中,
builder.Services.AddLocalization(options =>
{
options.ResourcesPath = "Resources";
}) ;
...
app.UseRequestLocalization(new RequestLocalizationOptions()
.AddSupportedCultures(new[] { "ko-KR", "en-US" })
.AddSupportedUICultures(new[] { "ko-KR", "en-US"}));
...
在~\Resource目录下使用VS 2022创建了一个 .resx 文件,根据我的配置在该目录下创建了 .Designer.cs 文件,
UIStrings.resx
UIStrings.Designer.cs
在 UIStrings.resx 中添加了一个字符串资源,例如:
Name : "이름"
FYI,“이름”是英文“name”的韩文翻译。
然后,我看到以下结果:
...
@inject IStringLocalizer<UIStrings> Localizer
...
@UIStrings.Name // 输出“이름”
@UIStrings.ResourceManager.GetString("Name") // 输出“이름”
@Localizer["Name"] // 输出“Name” <= 不好
我不知道为什么 IStringLocalizer<T> 不起作用。
英文:
I have set it up for localization as follows:
Set the neutralLanguage for the project,
// .csproj
<NeutralLanguage>ko-KR</NeutralLanguage>
Registered localization services into the service container,
// 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"}));
...
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 : "이름"
FYI, "이름" is Korean for English "name".
and, I am seeing the below result:
// .razor
...
@inject IStringLocalizer<UIStrings> Localizer
...
@UIStrings.Name // prints "이름"
@UIStrings.ResourceManager.GetString("Name") // prints "이름"
@Localizer["Name"] // prints "Name" <= 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<UIStrings> Localizer;
<p>@Localizer[UIStrings.Name]</p>
答案2
得分: 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; // <== 用于识别 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 => options.ResourcesPath = "Resources");
var app = builder.Build();
// ResourceManager
// 默认情况下,要求参数的根名称与.resx的根名称相同。
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",这是相对根路径。
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();
}
}
项目文件夹结构如下:
英文:
I tried some code and found how IStringLocalizer<T>
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; // <== 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();
}
}
The project folder structure is:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论