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

.NET 8 Blazor WebAssembly独立项目az-AZ文化设置无效

Blazor WebAssembly .NET8中az-AZ文化不生效的解决方案

问题分析

你设置的az-AZ并非.NET认可的有效文化标识符,阿塞拜疆文化存在拉丁(az-Latn-AZ)和西里尔(az-Cyrl-AZ)两种书写体系,模糊的az-AZ会被.NET自动回退到默认文化,导致配置不生效。

解决步骤

1. 使用正确的文化标识符更新Program.cs

将原代码中的az-AZ替换为明确的文化名称,例如使用拉丁体系的阿塞拜疆语:

using BlazorApp1;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using System.Globalization;

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

// 使用明确的阿塞拜疆拉丁文化标识符
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("az-Latn-AZ");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("az-Latn-AZ");

await builder.Build().RunAsync();

如果需要西里尔体系,替换为az-Cyrl-AZ即可。

2. 优化项目全球化配置(可选)

虽然你已开启BlazorWebAssemblyLoadAllGlobalizationData加载全部全球化数据,但如果只需要支持特定文化,可在项目文件中指定支持的文化列表,减少打包体积:

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

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <BlazorWebAssemblyLoadAllGlobalizationData>true</BlazorWebAssemblyLoadAllGlobalizationData>
    <!-- 指定支持的阿塞拜疆文化变体 -->
    <BlazorWebAssemblySupportedCultures>az-Latn-AZ;az-Cyrl-AZ</BlazorWebAssemblySupportedCultures>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.2" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.2" PrivateAssets="all" />
  </ItemGroup>

</Project>

3. 验证配置生效

在任意组件(如Index.razor)中添加代码,验证当前文化是否正确:

<div>
  <p>当前文化:@CultureInfo.CurrentCulture.Name</p>
  <p>当前UI文化:@CultureInfo.CurrentUICulture.Name</p>
  <p>格式化示例:@DateTime.Now.ToString("C")</p>
</div>

运行项目后,若显示的文化名称为你指定的az-Latn-AZ或az-Cyrl-AZ,且日期/数字格式符合阿塞拜疆语规范,说明配置生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 03:32:41