【问题标题】:Blazor Server Side Localization without cookie没有 cookie 的 Blazor 服务器端本地化
【发布时间】:2021-06-16 15:54:58
【问题描述】:

所以我尝试在我的 Blazor 服务器端项目中添加没有 cookie 的本地化。

https://docs.microsoft.com/en-us/aspnet/core/blazor/globalization-localization?view=aspnetcore-5.0#provide-ui-to-choose-the-culture 的文档中说:

使用 cookie 可确保 WebSocket 连接能够正确传播文化。如果本地化方案基于 URL 路径或查询字符串,则该方案可能无法与 WebSockets 一起使用,因此无法保持文化。因此,推荐使用本地化文化 cookie。

所以我知道它不推荐,但如果可能的话,我希望它是基于路径的。可以用标准的 services.AddLocalization();和 IStringLocalizer,还是我必须构建一个自定义的?

【问题讨论】:

  • 根据the document,可以使用以下方法之一设置文化:Cookies或提供UI以选择文化。如果您想使用提供 UI 方法来选择文化,您可以按照教程配置应用程序。
  • @ZhiLv 是吗?而“提供 UI 以选择文化”只是对设置语言 cookie 的 IActionResult SetCulture 的调用?

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


【解决方案1】:

对于本地化,我更喜欢包含语言的前 2 个字母的直接、清晰且可读的链接(例如:mysite.com/en/mypage),这样可以使用所需的语言获得正确的链接。

我发现与 Blazor Server 完美配合而无需 Cookie 的唯一解决方案是使用 “Pier-Luc Bonneville” 创建的包 “BlazorServerUrlRequestCultureProvider”;它使用起来毫不费力,而且效果极佳。

用法:

安装包BlazorServerUrlRequestCultureProvider(与.Net 5.0完美配合):

Install-Package BlazorServerUrlRequestCultureProvider -Version 1.0.0

Startup.cs

using BlazorServerUrlRequestCultureProvider;
// ...
//------------------------------
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // ...
        // Set the Resources folder name:
        services.AddLocalization(options => options.ResourcesPath = "Resources");
        // ...
    }
}
//------------------------------
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");
    }
    app.UseHttpsRedirection();
    app.UseStaticFiles();

    #region Localization
    var supportedCultures = new[]
                {
                    new CultureInfo("en"),
                    new CultureInfo("ar"),
                };
    var options = new RequestLocalizationOptions
    {
        // Select a default language:
        DefaultRequestCulture = new RequestCulture("en"),
        // For Numbers, Dates, etc:
        SupportedCultures = supportedCultures,
        // For strings that we have localized in .resx files:
        SupportedUICultures = supportedCultures
    };
    app.UseUrlRequestLocalization(options);
    #endregion
    
    app.UseRouting();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapBlazorHub();
        endpoints.MapFallbackToPage("/_Host");
    });
    app.UseRequestLocalization();
}

这就是你所需要的!没有 Cookie 和控制器!

现在像往常一样使用 StringLocalizer(在我的例子中,资源文件名是 Resource 文件夹中的 App.resx 和 App.ar.resx):

MyPage.razor:

@page "/ar/MyPage"
@page "/en/MyPage"

@inject IStringLocalizer<App> localizer

<h1>@localizer["My Translated Text Here !"]</h1>

感谢和感谢“Pier-Luc Bonneville”的出色工作。 Project Website , NuGet .

--

奖励: 为了让您的生活更轻松,请使用工具 ResXManager 轻松管理和翻译资源文件,它是一款免费且出色的工具,可为您节省大量时间。

【讨论】:

    猜你喜欢
    • 2023-01-05
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    • 2019-11-25
    • 2020-05-23
    • 2021-05-28
    • 2020-05-15
    • 1970-01-01
    相关资源
    最近更新 更多