Blazor WebAssembly 在 index.html 中设置的本地化无效。

huangapple go评论50阅读模式
英文:

Blazor Webassembly localization set in index.html does not have any effect

问题

我遵循了 https://learn.microsoft.com/en-us/aspnet/core/blazor/globalization-localization?view=aspnetcore-7.0&pivots=webassembly 中的说明。

但是,文化并没有按预期设置。

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">&#128473;</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&amp;pivots=webassembly

But the culture does not get set as expected.

index.html
<!DOCTYPE html>
<html lang="en">

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

&lt;body&gt;
    &lt;div id=&quot;app&quot;&gt;
        &lt;svg class=&quot;loading-progress&quot;&gt;
            &lt;circle r=&quot;40%&quot; cx=&quot;50%&quot; cy=&quot;50%&quot; /&gt;
            &lt;circle r=&quot;40%&quot; cx=&quot;50%&quot; cy=&quot;50%&quot; /&gt;
        &lt;/svg&gt;
        &lt;div class=&quot;loading-progress-text&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;

    &lt;div id=&quot;blazor-error-ui&quot;&gt;
        An unhandled error has occurred.
        &lt;a href=&quot;&quot; class=&quot;reload&quot;&gt;Reload&lt;/a&gt;
        &lt;a class=&quot;dismiss&quot;&gt;&#128473;&lt;/a&gt;
    &lt;/div&gt;
    &lt;script src=&quot;_framework/blazor.webassembly.js&quot; autostart=&quot;false&quot;&gt;&lt;/script&gt;
    &lt;script&gt;
        Blazor.start({
            applicationCulture: &#39;de&#39;
        });
    &lt;/script&gt;
&lt;/body&gt;

&lt;/html&gt;

Here's some code from the Blazor Index page

&lt;h2&gt;The Current culture is &quot;@CultureInfo.CurrentCulture&quot;&lt;/h2&gt;

which is displayed as

The Current culture is "en-US"

I would expect "de" here.

Solution file

&lt;Project Sdk=&quot;Microsoft.NET.Sdk.BlazorWebAssembly&quot;&gt;

    &lt;PropertyGroup&gt;
        &lt;TargetFramework&gt;net7.0&lt;/TargetFramework&gt;
        &lt;Nullable&gt;enable&lt;/Nullable&gt;
        &lt;ImplicitUsings&gt;enable&lt;/ImplicitUsings&gt;
        &lt;BlazorWebAssemblyLoadAllGlobalizationData&gt;true&lt;/BlazorWebAssemblyLoadAllGlobalizationData&gt;
    &lt;/PropertyGroup&gt;

    &lt;ItemGroup&gt;
        &lt;PackageReference Include=&quot;AutoMapper&quot; Version=&quot;12.0.1&quot; /&gt;
        &lt;PackageReference Include=&quot;AutoMapper.Extensions.Microsoft.DependencyInjection&quot; Version=&quot;12.0.1&quot; /&gt;
        &lt;PackageReference Include=&quot;Microsoft.AspNetCore.Components.Authorization&quot; Version=&quot;7.0.5&quot; /&gt;
        &lt;PackageReference Include=&quot;Microsoft.AspNetCore.Components.WebAssembly&quot; Version=&quot;7.0.5&quot; /&gt;
        &lt;PackageReference Include=&quot;Microsoft.AspNetCore.Components.WebAssembly.DevServer&quot; Version=&quot;7.0.5&quot; PrivateAssets=&quot;all&quot; /&gt;
        &lt;PackageReference Include=&quot;Syncfusion.Blazor.Grid&quot; Version=&quot;21.1.41&quot; /&gt;
    &lt;/ItemGroup&gt;

    &lt;ItemGroup&gt;
        &lt;ProjectReference Include=&quot;..\..\Test.Core\Test.Core.csproj&quot; /&gt;
        &lt;ProjectReference Include=&quot;..\Shared\Test.Server.Blazor.WebAssembly.Shared.csproj&quot; /&gt;
    &lt;/ItemGroup&gt;

    &lt;ItemGroup&gt;
        &lt;Compile Update=&quot;Resources\SfResources.Designer.cs&quot;&gt;
            &lt;DesignTime&gt;True&lt;/DesignTime&gt;
            &lt;AutoGen&gt;True&lt;/AutoGen&gt;
            &lt;DependentUpon&gt;SfResources.resx&lt;/DependentUpon&gt;
        &lt;/Compile&gt;
    &lt;/ItemGroup&gt;

    &lt;ItemGroup&gt;
        &lt;EmbeddedResource Update=&quot;Resources\SfResources.resx&quot;&gt;
            &lt;Generator&gt;PublicResXFileCodeGenerator&lt;/Generator&gt;
            &lt;LastGenOutput&gt;SfResources.Designer.cs&lt;/LastGenOutput&gt;
        &lt;/EmbeddedResource&gt;
    &lt;/ItemGroup&gt;

&lt;/Project&gt;

What's missing?

答案1

得分: 1

我终于找到了问题的原因。
在Microsoft链接中找到的指南正常工作。
我的错误是我将

&lt;BlazorWebAssemblyLoadAllGlobalizationData&gt;true&lt;/BlazorWebAssemblyLoadAllGlobalizationData&gt;

添加到了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

&lt;BlazorWebAssemblyLoadAllGlobalizationData&gt;true&lt;/BlazorWebAssemblyLoadAllGlobalizationData&gt;

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.

huangapple
  • 本文由 发表于 2023年6月8日 04:50:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定