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

Blazor WebAssembly在index.html设置本地化无效果,求排查

问题:Blazor WebAssembly 文化(Culture)未按预期设置

我已按照官方文档说明操作,但Culture并未按预期设置。页面显示当前文化为en-US,预期应为de。

index.html 代码

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <base href="/" />
    <link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
    <link href="css/app.css" rel="stylesheet" />
    <link rel="icon" type="image/png" href="favicon.png" />
    <link href="Test.WebAssembly.Client.styles.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/blazor/21.1.41/styles/bootstrap5.css" rel="stylesheet" />
    <script src="https://cdn.syncfusion.com/blazor/21.1.41/syncfusion-blazor.min.js"  type="text/javascript"></script>
</head>

<body>
    <div id="app">
        <svg class="loading-progress">
            <circle r="40%" cx="50%" cy="50%" />
            <circle r="40%" cx="50%" cy="50%" />
        </svg>
        <div class="loading-progress-text"></div>
    </div>

    <div id="blazor-error-ui">
        An unhandled error has occurred.
        <a href="" class="reload">Reload</a>
        <a class="dismiss">🗙</a>
    </div>
    <script src="_framework/blazor.webassembly.js" autostart="false"></script>
    <script>
        Blazor.start({
            applicationCulture: 'de'
        });
    </script>
</body>

</html>

Blazor Index页面代码片段

<h2>The Current culture is "@CultureInfo.CurrentCulture"</h2>

项目文件代码

<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

    <PropertyGroup>
        <TargetFramework>net7.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
        <BlazorWebAssemblyLoadAllGlobalizationData>true</BlazorWebAssemblyLoadAllGlobalizationData>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="AutoMapper" Version="12.0.1" />
        <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
        <PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="7.0.5" />
        <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.5" />
        <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.5" PrivateAssets="all" />
        <PackageReference Include="Syncfusion.Blazor.Grid" Version="21.1.41" />
    </ItemGroup>

    <ItemGroup>
        <ProjectReference Include="..\..\Test.Core\Test.Core.csproj" />
        <ProjectReference Include="..\Shared\Test.Server.Blazor.WebAssembly.Shared.csproj" />
    </ItemGroup>

    <ItemGroup>
        <Compile Update="Resources\SfResources.Designer.cs">
            <DesignTime>True</DesignTime>
            <AutoGen>True</AutoGen>
            <DependentUpon>SfResources.resx</DependentUpon>
        </Compile>
    </ItemGroup>

    <ItemGroup>
        <EmbeddedResource Update="Resources\SfResources.resx">
            <Generator>PublicResXFileCodeGenerator</Generator>
            <LastGenOutput>SfResources.Designer.cs</LastGenOutput>
        </EmbeddedResource>
    </ItemGroup>

</Project>

解决方案

你的配置缺少Program.cs中本地化服务的注册与请求本地化的启用,这是让CultureInfo.CurrentCulture全局生效的必要步骤,补充操作如下:

  1. 在Program.cs中添加本地化服务配置:
// 注册本地化服务,指定资源文件存放路径(若有自定义资源文件)
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");

// 配置支持的文化列表及默认文化
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
    var supportedCultures = new[] { "en-US", "de" };
    options.DefaultRequestCulture = new RequestCulture("de");
    options.SupportedCultures = supportedCultures;
    options.SupportedUICultures = supportedCultures;
});
  1. 在Program.cs末尾启用请求本地化:
var host = builder.Build();

// 启用请求本地化,使配置的文化生效
host.Services.UseRequestLocalization();

await host.RunAsync();

仅通过Blazor.start设置applicationCulture不足以让.NET的CultureInfo全局生效,必须在服务容器中完成本地化服务的注册与启用,才能确保组件中获取到正确的当前文化。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 21:24:53