Blazor 本地化

3 asp.net-core blazor blazor-server-side

我尝试在 Blazor 应用程序中实现本地化,但在尝试通过视图中的键获取资源值时遇到了一些问题。

我怎么能做到这一点?

到目前为止,这是我的代码:

启动文件

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");
    });
}
Run Code Online (Sandbox Code Playgroud)

登录.razor

@page "/login"

@using Microsoft.Extensions.Localization

@inject IStringLocalizer<Login> Localizer

<button type="submit" class="btn btn-primary w-100">@Localizer["LoginButtonText"]</button>
Run Code Online (Sandbox Code Playgroud)

页面位置

项目

-- 页面

- - 帐户

------ 登录.razor


资源文件位置

项目

- 资源

---- 页面

- - - 帐户

--------登录.bg-BG.resx

rk7*_*k72 8

这对我有用:

启动文件

public void ConfigureServices(IServiceCollection services)
{
  ...
  services.AddLocalization(options => { options.ResourcesPath = "Resources"; });
  ...
}
Run Code Online (Sandbox Code Playgroud)

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; }
}
Run Code Online (Sandbox Code Playgroud)

我的resx文件存储如下:

Resources\Pages\Index.resx
Resources\Pages\Index.cs.resx
...
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

然后,根据 UICulture,在调用localizer["Field1"].

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

Resources\Pages\Index.resx
Resources\Pages\Index.cs.resx
...
Run Code Online (Sandbox Code Playgroud)

  • 您还可以将“@inject Microsoft.Extensions.Localization.IStringLocalizer&lt;Index&gt; Localizer”添加到剃须刀头。对我来说重要的是将其存储在正确的资源文件夹中。我忘记了 Pages 子文件夹。 (2认同)