Skip to content

Commit dd9704d

Browse files
committed
Localization added
1 parent ec83398 commit dd9704d

File tree

13 files changed

+462
-22
lines changed

13 files changed

+462
-22
lines changed
Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
11
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
22

3-
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
5-
<Nullable>enable</Nullable>
6-
<ImplicitUsings>enable</ImplicitUsings>
7-
</PropertyGroup>
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<BlazorWebAssemblyLoadAllGlobalizationData>true</BlazorWebAssemblyLoadAllGlobalizationData>
8+
</PropertyGroup>
89

9-
<ItemGroup>
10-
<PackageReference Include="Blazored.LocalStorage" Version="4.3.0" />
11-
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="7.0.5" />
12-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.5" />
13-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.5" PrivateAssets="all" />
14-
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.2.0" />
15-
<PackageReference Include="MudBlazor" Version="6.2.2" />
16-
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
17-
<PackageReference Include="Toolbelt.Blazor.HttpClientInterceptor" Version="10.2.0" />
18-
</ItemGroup>
10+
<ItemGroup>
11+
<PackageReference Include="Blazored.LocalStorage" Version="4.3.0" />
12+
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="7.0.5" />
13+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.5" />
14+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.5" PrivateAssets="all" />
15+
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.2.0" />
16+
<PackageReference Include="Microsoft.Extensions.Localization" Version="7.0.5" />
17+
<PackageReference Include="MudBlazor" Version="6.2.2" />
18+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
19+
<PackageReference Include="Toolbelt.Blazor.HttpClientInterceptor" Version="10.2.0" />
20+
</ItemGroup>
1921

20-
<ItemGroup>
21-
<ProjectReference Include="..\Shared\BlazorEcommerce.Shared.csproj" />
22-
</ItemGroup>
22+
<ItemGroup>
23+
<ProjectReference Include="..\Shared\BlazorEcommerce.Shared.csproj" />
24+
</ItemGroup>
25+
26+
<ItemGroup>
27+
<Compile Update="Shared\ResourceFiles\Resource.Designer.cs">
28+
<DesignTime>True</DesignTime>
29+
<AutoGen>True</AutoGen>
30+
<DependentUpon>Resource.resx</DependentUpon>
31+
</Compile>
32+
</ItemGroup>
33+
34+
<ItemGroup>
35+
<EmbeddedResource Update="Shared\ResourceFiles\Resource.de.resx">
36+
<Generator>PublicResXFileCodeGenerator</Generator>
37+
</EmbeddedResource>
38+
<EmbeddedResource Update="Shared\ResourceFiles\Resource.resx">
39+
<Generator>PublicResXFileCodeGenerator</Generator>
40+
<LastGenOutput>Resource.Designer.cs</LastGenOutput>
41+
</EmbeddedResource>
42+
</ItemGroup>
2343

2444
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
2+
using Microsoft.JSInterop;
3+
using System.Globalization;
4+
5+
namespace BlazorEcommerce.Client.Extensions
6+
{
7+
public static class WebAssemblyHostExtension
8+
{
9+
public async static Task SetDefaultCulture(this WebAssemblyHost host)
10+
{
11+
var jsInterop = host.Services.GetRequiredService<IJSRuntime>();
12+
var result = await jsInterop.InvokeAsync<string>("blazorCulture.get");
13+
14+
CultureInfo culture;
15+
16+
if (result != null)
17+
culture = new CultureInfo(result);
18+
else
19+
culture = new CultureInfo("en-US");
20+
21+
CultureInfo.DefaultThreadCurrentCulture = culture;
22+
CultureInfo.DefaultThreadCurrentUICulture = culture;
23+
}
24+
}
25+
}

src/Presentation/Client/Pages/Index.razor

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
@page "/{categoryUrl}"
44
@inject IProductService ProductService
55
@implements IDisposable
6+
@inject IStringLocalizer<Resource> localizer
67

78
<PageTitle>My Shop</PageTitle>
89

@@ -11,6 +12,9 @@
1112
Welcome to your new app.
1213
1314
<SurveyPrompt Title="How is Blazor working for you?" />*@
15+
16+
<h1>@localizer["welcome"]</h1>
17+
1418
<ErrorBoundary @ref="errorBoundary">
1519
<ChildContent>
1620
@if (SearchText == null && CategoryUrl == null)

src/Presentation/Client/Program.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
global using BlazorEcommerce.Shared.Response.Concrete;
1515
global using System.Net.Http.Json;
1616
using BlazorEcommerce.Client;
17+
using BlazorEcommerce.Client.Extensions;
1718
using Blazored.LocalStorage;
1819
using Microsoft.AspNetCore.Components.Authorization;
1920
using Microsoft.AspNetCore.Components.Web;
@@ -27,7 +28,7 @@
2728

2829
builder.Services.AddBlazoredLocalStorage();
2930
builder.Services.AddMudServices();
30-
31+
builder.Services.AddLocalization();
3132

3233
//builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
3334
var apiUrl = builder.Configuration.GetValue<string>("AppConfig:ApiUrl");
@@ -53,4 +54,8 @@
5354
builder.Services.AddAuthorizationCore();
5455
builder.Services.AddScoped<AuthenticationStateProvider, CustomAuthStateProvider>();
5556

56-
await builder.Build().RunAsync();
57+
var host = builder.Build();
58+
59+
await host.SetDefaultCulture();
60+
61+
await host.RunAsync();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<select class="form-control" @bind="Culture" style="width:110px; margin-left:10px;">
2+
@foreach (var culture in cultures)
3+
{
4+
<option value="@culture">@culture.DisplayName</option>
5+
}
6+
</select>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Microsoft.AspNetCore.Components;
2+
using Microsoft.JSInterop;
3+
using System.Globalization;
4+
5+
namespace BlazorEcommerce.Client.Shared
6+
{
7+
public partial class CultureSelector
8+
{
9+
[Inject]
10+
public NavigationManager NavManager { get; set; }
11+
12+
[Inject]
13+
public IJSRuntime JSRuntime { get; set; }
14+
15+
CultureInfo[] cultures = new[]
16+
{
17+
new CultureInfo("en-US"),
18+
new CultureInfo("de-DE")
19+
};
20+
21+
CultureInfo Culture
22+
{
23+
get => CultureInfo.CurrentCulture;
24+
set
25+
{
26+
if (CultureInfo.CurrentCulture != value)
27+
{
28+
var js = (IJSInProcessRuntime)JSRuntime;
29+
js.InvokeVoid("blazorCulture.set", value.Name);
30+
31+
NavManager.NavigateTo(NavManager.Uri, forceLoad: true);
32+
}
33+
}
34+
}
35+
}
36+
}

src/Presentation/Client/Shared/FeatureProducts.razor

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
@inject IProductService ProductService
22
@implements IDisposable
3+
@inject IStringLocalizer<Resource> localizer
4+
5+
<center><h2>@localizer["featuredproductpage"]</h2></center>
36

4-
<center><h2>Top Products of Today</h2></center>
57
@if (ProductService.Products == null || ProductService.Products.Count == 0)
68
{
79
<span>@ProductService.Message</span>

src/Presentation/Client/Shared/ResourceFiles/Resource.Designer.cs

Lines changed: 81 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<root>
3+
<!--
4+
Microsoft ResX Schema
5+
6+
Version 2.0
7+
8+
The primary goals of this format is to allow a simple XML format
9+
that is mostly human readable. The generation and parsing of the
10+
various data types are done through the TypeConverter classes
11+
associated with the data types.
12+
13+
Example:
14+
15+
... ado.net/XML headers & schema ...
16+
<resheader name="resmimetype">text/microsoft-resx</resheader>
17+
<resheader name="version">2.0</resheader>
18+
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
19+
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
20+
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
21+
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
22+
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
23+
<value>[base64 mime encoded serialized .NET Framework object]</value>
24+
</data>
25+
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
26+
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
27+
<comment>This is a comment</comment>
28+
</data>
29+
30+
There are any number of "resheader" rows that contain simple
31+
name/value pairs.
32+
33+
Each data row contains a name, and value. The row also contains a
34+
type or mimetype. Type corresponds to a .NET class that support
35+
text/value conversion through the TypeConverter architecture.
36+
Classes that don't support this are serialized and stored with the
37+
mimetype set.
38+
39+
The mimetype is used for serialized objects, and tells the
40+
ResXResourceReader how to depersist the object. This is currently not
41+
extensible. For a given mimetype the value must be set accordingly:
42+
43+
Note - application/x-microsoft.net.object.binary.base64 is the format
44+
that the ResXResourceWriter will generate, however the reader can
45+
read any of the formats listed below.
46+
47+
mimetype: application/x-microsoft.net.object.binary.base64
48+
value : The object must be serialized with
49+
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
50+
: and then encoded with base64 encoding.
51+
52+
mimetype: application/x-microsoft.net.object.soap.base64
53+
value : The object must be serialized with
54+
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
55+
: and then encoded with base64 encoding.
56+
57+
mimetype: application/x-microsoft.net.object.bytearray.base64
58+
value : The object must be serialized into a byte array
59+
: using a System.ComponentModel.TypeConverter
60+
: and then encoded with base64 encoding.
61+
-->
62+
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
63+
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
64+
<xsd:element name="root" msdata:IsDataSet="true">
65+
<xsd:complexType>
66+
<xsd:choice maxOccurs="unbounded">
67+
<xsd:element name="metadata">
68+
<xsd:complexType>
69+
<xsd:sequence>
70+
<xsd:element name="value" type="xsd:string" minOccurs="0" />
71+
</xsd:sequence>
72+
<xsd:attribute name="name" use="required" type="xsd:string" />
73+
<xsd:attribute name="type" type="xsd:string" />
74+
<xsd:attribute name="mimetype" type="xsd:string" />
75+
<xsd:attribute ref="xml:space" />
76+
</xsd:complexType>
77+
</xsd:element>
78+
<xsd:element name="assembly">
79+
<xsd:complexType>
80+
<xsd:attribute name="alias" type="xsd:string" />
81+
<xsd:attribute name="name" type="xsd:string" />
82+
</xsd:complexType>
83+
</xsd:element>
84+
<xsd:element name="data">
85+
<xsd:complexType>
86+
<xsd:sequence>
87+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
88+
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
89+
</xsd:sequence>
90+
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
91+
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
92+
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
93+
<xsd:attribute ref="xml:space" />
94+
</xsd:complexType>
95+
</xsd:element>
96+
<xsd:element name="resheader">
97+
<xsd:complexType>
98+
<xsd:sequence>
99+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
100+
</xsd:sequence>
101+
<xsd:attribute name="name" type="xsd:string" use="required" />
102+
</xsd:complexType>
103+
</xsd:element>
104+
</xsd:choice>
105+
</xsd:complexType>
106+
</xsd:element>
107+
</xsd:schema>
108+
<resheader name="resmimetype">
109+
<value>text/microsoft-resx</value>
110+
</resheader>
111+
<resheader name="version">
112+
<value>2.0</value>
113+
</resheader>
114+
<resheader name="reader">
115+
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
116+
</resheader>
117+
<resheader name="writer">
118+
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119+
</resheader>
120+
<data name="featuredproductpage" xml:space="preserve">
121+
<value>Top-Produkte von heute</value>
122+
</data>
123+
<data name="welcome" xml:space="preserve">
124+
<value>Willkommen in Ihrer neuen App.</value>
125+
</data>
126+
</root>

0 commit comments

Comments
 (0)