Blazor本地化

4

我希望您能为Blazor应用程序提供本地化翻译,并且在查找视图中的键资源值时遇到了一些问题。

请问该如何解决此问题?

以下是我的代码:

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddLocalization(options => { options.ResourcesPath = "Resources"; });

    services.Configure<RequestLocalizationOptions>(
        options =>
        {
            List<CultureInfo> supportedCultures =
                new List<CultureInfo>
                {
                    new CultureInfo("bg-BG"),
                    new CultureInfo("en-US")
                };

            options.DefaultRequestCulture = new RequestCulture("bg-BG");

            // Formatting numbers, dates, etc.
            options.SupportedCultures = supportedCultures;

            // UI string 
            options.SupportedUICultures = supportedCultures;

        });

    services.AddRazorPages();

    services.AddServerSideBlazor().AddCircuitOptions(options => { options.DetailedErrors = true; });

    services.AddApplicationRepositoryServices();

    services.AddSingleton<WeatherForecastService>();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
     app.UseRequestLocalization( app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value);

    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.UseAuthentication();

    app.UseRouting();

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

       endpoints.MapBlazorHub();

        endpoints.MapFallbackToPage("/_Host");
    });
}

Login.razor

@page "/login"

@using Microsoft.Extensions.Localization

@inject IStringLocalizer<Login> Localizer

<button type="submit" class="btn btn-primary w-100">@Localizer["LoginButtonText"]</button>

页面位置

项目

-- 页面

---- 帐户

------ Login.razor


资源文件位置

项目

-- 资源

---- 页面

------ 帐户

-------- Login.bg-BG.resx


1
看看这个库 - https://github.com/Blazored/Localisation。免责声明 - 不知道它是否好用,但在这里提到:https://github.com/AdrienTorris/awesome-blazor。 - jazb
本文档描述了如何本地化Blazor应用程序。https://www.soluling.com/Help/Blazor/Index.htm - Jaska
1个回答

13

这对我有用:

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
  ...
  services.AddLocalization(options => { options.ResourcesPath = "Resources"; });
  ...
}

Index.razor

@page "/"
@using System.Globalization

<h1>Localization test</h1>

Localized field: @(localizer["Field1"])

@code {
    [Inject] public Microsoft.Extensions.Localization.IStringLocalizer<Index> localizer { get; set; }
}

我的 resx 文件存储方式如下:

Resources\Pages\Index.resx
Resources\Pages\Index.cs.resx
...

enter image description here

然后,根据UICulture的不同,调用localizer["Field1"]时返回相应的本地化字符串。

在代码中,我通过以下方式更改了UICulture:

CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("cs-CZ");

1
有没有想法如何使用带有提供本地化的卫星程序集的 IStringLocalizer<T> - Stefan Ossendorf
4
您还可以在Razor标头中添加@inject Microsoft.Extensions.Localization.IStringLocalizer<Index> Localizer。对我来说重要的是将其存储在正确的资源文件夹中,我忘记了Pages子文件夹。 - Hunv
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("cs-CZ"); 这个对我有用。谢谢! - Marin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接