You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Blazor WASM(.NET 8)应用复数化实现问题咨询

解决Blazor WASM (.NET 8) 复数化与PO文件支持问题

为什么安装OrchardCore.Localization.Core会报错

OrchardCore.Localization.Core包依赖的Microsoft.AspNetCore.App运行时包不支持browser-wasm运行时标识符,该包主要针对服务器端ASP.NET Core应用,无法在Blazor WASM环境中兼容使用。

支持Blazor WASM复数化的可行方案

方案1:使用原生IStringLocalizer结合.resx文件(官方推荐,无需额外包)

Blazor WASM原生支持IStringLocalizer,且内置复数化逻辑,操作步骤如下:

  • 在项目中添加.resx资源文件,比如Resources/Shared.en.resx、Resources/Shared.zh.resx
  • 复数内容遵循.NET本地化命名规则:例如在.resx中添加键ProductCount,值为You have {0} product;再添加键ProductCount_Plural,值为You have {0} products
  • 在组件中注入IStringLocalizer<Shared>,调用localizer["ProductCount", count],框架会自动根据数值匹配单复数文本
  • 在Program.cs中配置本地化服务:
    builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
    
    var supportedCultures = new[] { "en", "zh" };
    var localizationOptions = new RequestLocalizationOptions()
        .SetDefaultCulture(supportedCultures[0])
        .AddSupportedCultures(supportedCultures)
        .AddSupportedUICultures(supportedCultures);
    
    app.UseRequestLocalization(localizationOptions);
    

方案2:自定义PO文件加载实现复数化

如果必须使用PO文件,需要自定义IStringLocalizerFactory和IStringLocalizer解析PO文件的复数规则,步骤如下:

  1. 编写PO文件解析逻辑:PO文件通过msgid_plural和msgstr[]定义复数,需实现代码读取PO文件并解析单复数映射关系
  2. 实现自定义IStringLocalizer:在GetString方法中根据传入数值匹配对应复数文本
  3. 实现自定义IStringLocalizerFactory:负责根据当前文化加载对应PO文件,创建IStringLocalizer实例
  4. 注册服务:在Program.cs中将自定义工厂注册为IStringLocalizerFactory的实现:
    builder.Services.AddSingleton<IStringLocalizerFactory, PoStringLocalizerFactory>();
    builder.Services.AddLocalization();
    

示例PO文件内容(Resources/Shared.en.po):

msgid "ProductCount"
msgid_plural "ProductCount"
msgstr[0] "You have {0} product"
msgstr[1] "You have {0} products"

自定义IStringLocalizer核心逻辑示例:

public class PoStringLocalizer : IStringLocalizer
{
    private readonly Dictionary<string, string[]> _pluralStrings;

    public PoStringLocalizer(Dictionary<string, string[]> pluralStrings)
    {
        _pluralStrings = pluralStrings;
    }

    public LocalizedString this[string name] => new LocalizedString(name, _pluralStrings.TryGetValue(name, out var vals) ? vals[0] : name);

    public LocalizedString this[string name, params object[] arguments]
    {
        get
        {
            if (_pluralStrings.TryGetValue(name, out var vals) && arguments.Length > 0 && arguments[0] is int count)
            {
                var index = count == 1 ? 0 : 1; // 基础英文复数规则,可按不同文化扩展
                return new LocalizedString(name, string.Format(vals[index], arguments), resourceNotFound: false);
            }
            return new LocalizedString(name, string.Format(_pluralStrings.TryGetValue(name, out var vals) ? vals[0] : name, arguments));
        }
    }

    // 其余接口方法实现(略)
}

总结

  • 不要使用OrchardCore.Localization.Core,它与Blazor WASM不兼容
  • 优先选择原生.resx+IStringLocalizer方案,简单且有官方支持
  • 若必须用PO文件,需自定义本地化工厂和字符串本地化器来解析PO文件的复数规则

内容的提问来源于stack exchange,提问作者Jernej Habjan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 13:05:11