【问题标题】:Blazor LocalizationBlazor 本地化
【发布时间】:2020-03-21 20:21:10
【问题描述】:

我尝试在 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>

页面位置

项目

-- 页面

---- 帐号

------登录.razor


资源文件位置

项目

-- 资源

---- 页面

----- 帐号

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

【问题讨论】:

标签: asp.net-core blazor blazor-server-side


【解决方案1】:

这对我有用:

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
...

然后,根据 UICulture,在调用 localizer["Field1"] 时返回正确的本地化字符串。

在代码中,我以这种方式更改 UICulture:

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

【讨论】:

  • 知道如何将IStringLocalizer&lt;T&gt; 与提供本地化的卫星程序集一起使用吗?
  • 您也可以在剃须刀头上添加@inject Microsoft.Extensions.Localization.IStringLocalizer&lt;Index&gt; Localizer。对我来说重要的是将它存储在正确的资源文件夹中。我忘记了 Pages 子文件夹。
  • CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("cs-CZ");这个是为我做的。谢谢!
猜你喜欢
  • 2022-06-19
  • 1970-01-01
  • 2021-01-29
  • 2020-04-18
  • 1970-01-01
  • 2021-06-16
  • 1970-01-01
  • 1970-01-01
  • 2022-11-16
相关资源
最近更新 更多