英文:
Blazor Webassembly localization set in index.html does not have any effect
问题
但是,文化并没有按预期设置。
index.html
<!DOCTYPE html>
<html lang="en">
<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 索引页面的一些代码
<h2>The Current culture is "@CultureInfo.CurrentCulture"</h2>
显示为
当前文化是 "en-US"
我期望这里是 "de"。
解决方案文件
<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>
缺少什么?
英文:
I've followed the instructions at https://learn.microsoft.com/en-us/aspnet/core/blazor/globalization-localization?view=aspnetcore-7.0&pivots=webassembly
But the culture does not get set as expected.
index.html
<!DOCTYPE html>
<html lang="en">
<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>
Here's some code from the Blazor Index page
<h2>The Current culture is "@CultureInfo.CurrentCulture"</h2>
which is displayed as
The Current culture is "en-US"
I would expect "de" here.
Solution file
<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>
What's missing?
答案1
得分: 1
我终于找到了问题的原因。
在Microsoft链接中找到的指南正常工作。
我的错误是我将
<BlazorWebAssemblyLoadAllGlobalizationData>true</BlazorWebAssemblyLoadAllGlobalizationData>
添加到了WebAssembly客户端的解决方案文件,而不是托管客户端的asp.net核心项目。
移到正确的解决方案文件后,一切都按预期工作。
英文:
I finally found the reason for the issue.
The instructions found at the Microsoft link works fine. My mistake was that I added the
<BlazorWebAssemblyLoadAllGlobalizationData>true</BlazorWebAssemblyLoadAllGlobalizationData>
To the solution file for the WebAssembly client, and not the asp.net core project that hosted the client. Moved to the correct solution file, it all works as expected.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论