\ No newline at end of file
diff --git a/Sources/BlazorApp/BlazorApp/Components/ShowItems.razor.cs b/Sources/BlazorApp/BlazorApp/Components/ShowItems.razor.cs
new file mode 100644
index 0000000..75587b0
--- /dev/null
+++ b/Sources/BlazorApp/BlazorApp/Components/ShowItems.razor.cs
@@ -0,0 +1,13 @@
+using Microsoft.AspNetCore.Components;
+
+namespace BlazorApp.Components
+{
+ public partial class ShowItems
+ {
+ [Parameter]
+ public List Items { get; set; }
+
+ [Parameter]
+ public RenderFragment ShowTemplate { get; set; }
+ }
+}
diff --git a/Sources/BlazorApp/BlazorApp/Components/TestRenderFragment.razor b/Sources/BlazorApp/BlazorApp/Components/TestRenderFragment.razor
new file mode 100644
index 0000000..176ee34
--- /dev/null
+++ b/Sources/BlazorApp/BlazorApp/Components/TestRenderFragment.razor
@@ -0,0 +1,8 @@
+
TestRenderFragment
+
+@code {
+ [Parameter]
+ public RenderFragment ChildContent { get; set; }
+}
+
+@ChildContent
\ No newline at end of file
diff --git a/Sources/BlazorApp/BlazorApp/Controllers/CultureControllers.cs b/Sources/BlazorApp/BlazorApp/Controllers/CultureControllers.cs
new file mode 100644
index 0000000..328c28e
--- /dev/null
+++ b/Sources/BlazorApp/BlazorApp/Controllers/CultureControllers.cs
@@ -0,0 +1,31 @@
+using Microsoft.AspNetCore.Localization;
+using Microsoft.AspNetCore.Mvc;
+
+///
+/// The culture controller.
+///
+[Route("[controller]/[action]")]
+public class CultureController : Controller
+{
+ ///
+ /// Sets the culture.
+ ///
+ /// The culture.
+ /// The redirect URI.
+ ///
+ /// The action result.
+ ///
+ public IActionResult SetCulture(string culture, string redirectUri)
+ {
+ if (culture != null)
+ {
+ // Define a cookie with the selected culture
+ this.HttpContext.Response.Cookies.Append(
+ CookieRequestCultureProvider.DefaultCookieName,
+ CookieRequestCultureProvider.MakeCookieValue(
+ new RequestCulture(culture)));
+ }
+
+ return this.LocalRedirect(redirectUri);
+ }
+}
\ No newline at end of file
diff --git a/Sources/BlazorApp/BlazorApp/Models/Cake.cs b/Sources/BlazorApp/BlazorApp/Models/Cake.cs
new file mode 100644
index 0000000..5593683
--- /dev/null
+++ b/Sources/BlazorApp/BlazorApp/Models/Cake.cs
@@ -0,0 +1,8 @@
+namespace BlazorApp.Models;
+
+public class Cake
+{
+ public int Id { get; set; }
+ public string Name { get; set; }
+ public decimal Cost { get; set; }
+}
\ No newline at end of file
diff --git a/Sources/BlazorApp/BlazorApp/Pages/Index.razor b/Sources/BlazorApp/BlazorApp/Pages/Index.razor
index 6085c4a..16aaf67 100644
--- a/Sources/BlazorApp/BlazorApp/Pages/Index.razor
+++ b/Sources/BlazorApp/BlazorApp/Pages/Index.razor
@@ -1,4 +1,6 @@
@page "/"
+@using System.Globalization
+@using BlazorApp.Components
Index
@@ -7,3 +9,42 @@
Welcome to your new app.
+
+
+ CurrentCulture: @CultureInfo.CurrentCulture
+
+
+
+
+
+ Cake Token Number - @headContext.Id
+
+
+
+
+
@bodyContext.Name
+
$ @bodyContext.Cost
+
+
+
+
+
+
+
+
+ Cake Token Id - @CakeContext.Id
+
+
+
@CakeContext.Name
+
Price $@CakeContext.Cost
+
+
+
+
+
+
+
+
Content of my TestRenderFragment
+
\ No newline at end of file
diff --git a/Sources/BlazorApp/BlazorApp/Pages/Index.razor.cs b/Sources/BlazorApp/BlazorApp/Pages/Index.razor.cs
new file mode 100644
index 0000000..22c3121
--- /dev/null
+++ b/Sources/BlazorApp/BlazorApp/Pages/Index.razor.cs
@@ -0,0 +1,35 @@
+namespace BlazorApp.Pages;
+using BlazorApp.Models;
+
+public partial class Index
+{
+ private Cake CakeItem = new Cake
+ {
+ Id = 1,
+ Name = "Black Forest",
+ Cost = 50
+ };
+
+ public List Cakes { get; set; }
+
+ protected override Task OnAfterRenderAsync(bool firstRender)
+ {
+ LoadCakes();
+ StateHasChanged();
+ return base.OnAfterRenderAsync(firstRender);
+ }
+
+ public void LoadCakes()
+ {
+ Cakes = new List
+ {
+ // items hidden for display purpose
+ new Cake
+ {
+ Id = 1,
+ Name = "Red Velvet",
+ Cost = 60
+ },
+ };
+ }
+}
\ No newline at end of file
diff --git a/Sources/BlazorApp/BlazorApp/Pages/List.razor b/Sources/BlazorApp/BlazorApp/Pages/List.razor
index 26dd94a..f58987f 100644
--- a/Sources/BlazorApp/BlazorApp/Pages/List.razor
+++ b/Sources/BlazorApp/BlazorApp/Pages/List.razor
@@ -1,7 +1,7 @@
@page "/list"
@using BlazorApp.Models
-
List
+
@Localizer["Title"]
diff --git a/Sources/BlazorApp/BlazorApp/Pages/List.razor.cs b/Sources/BlazorApp/BlazorApp/Pages/List.razor.cs
index 668d889..370d201 100644
--- a/Sources/BlazorApp/BlazorApp/Pages/List.razor.cs
+++ b/Sources/BlazorApp/BlazorApp/Pages/List.razor.cs
@@ -16,6 +16,9 @@ public partial class List
private int totalItem;
+ [Inject]
+ public IStringLocalizer Localizer { get; set; }
+
[Inject]
public IDataService DataService { get; set; }
diff --git a/Sources/BlazorApp/BlazorApp/Pages/Pets1.razor b/Sources/BlazorApp/BlazorApp/Pages/Pets1.razor
new file mode 100644
index 0000000..065feec
--- /dev/null
+++ b/Sources/BlazorApp/BlazorApp/Pages/Pets1.razor
@@ -0,0 +1,29 @@
+@page "/pets1"
+
+
Pets
+
+
+
+
ID
+
Name
+
+
+
@pet.PetId
+
@pet.Name
+
+
+
+@code {
+ private List pets = new()
+ {
+ new Pet { PetId = 2, Name = "Mr. Bigglesworth" },
+ new Pet { PetId = 4, Name = "Salem Saberhagen" },
+ new Pet { PetId = 7, Name = "K-9" }
+ };
+
+ private class Pet
+ {
+ public int PetId { get; set; }
+ public string? Name { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/Sources/BlazorApp/BlazorApp/Pages/Pets2.razor b/Sources/BlazorApp/BlazorApp/Pages/Pets2.razor
new file mode 100644
index 0000000..c545e37
--- /dev/null
+++ b/Sources/BlazorApp/BlazorApp/Pages/Pets2.razor
@@ -0,0 +1,29 @@
+@page "/pets2"
+
+
Pets
+
+
+
+
ID
+
Name
+
+
+
@pet.PetId
+
@pet.Name
+
+
+
+@code {
+ private List pets = new()
+ {
+ new Pet { PetId = 2, Name = "Mr. Bigglesworth" },
+ new Pet { PetId = 4, Name = "Salem Saberhagen" },
+ new Pet { PetId = 7, Name = "K-9" }
+ };
+
+ private class Pet
+ {
+ public int PetId { get; set; }
+ public string? Name { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/Sources/BlazorApp/BlazorApp/Pages/Pets3.razor b/Sources/BlazorApp/BlazorApp/Pages/Pets3.razor
new file mode 100644
index 0000000..fd52259
--- /dev/null
+++ b/Sources/BlazorApp/BlazorApp/Pages/Pets3.razor
@@ -0,0 +1,29 @@
+@page "/pets3"
+
+
Pets
+
+
+
+
ID
+
Name
+
+
+
@context.PetId
+
@context.Name
+
+
+
+@code {
+ private List pets = new()
+ {
+ new Pet { PetId = 2, Name = "Mr. Bigglesworth" },
+ new Pet { PetId = 4, Name = "Salem Saberhagen" },
+ new Pet { PetId = 7, Name = "K-9" }
+ };
+
+ private class Pet
+ {
+ public int PetId { get; set; }
+ public string? Name { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/Sources/BlazorApp/BlazorApp/Pages/Pets4.razor b/Sources/BlazorApp/BlazorApp/Pages/Pets4.razor
new file mode 100644
index 0000000..ac0d918
--- /dev/null
+++ b/Sources/BlazorApp/BlazorApp/Pages/Pets4.razor
@@ -0,0 +1,29 @@
+@page "/pets4"
+
+
Pets
+
+
+
+
ID
+
Name
+
+
+
@context.PetId
+
@context.Name
+
+
+
+@code {
+ private List pets = new()
+ {
+ new Pet { PetId = 2, Name = "Mr. Bigglesworth" },
+ new Pet { PetId = 4, Name = "Salem Saberhagen" },
+ new Pet { PetId = 7, Name = "K-9" }
+ };
+
+ private class Pet
+ {
+ public int PetId { get; set; }
+ public string? Name { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/Sources/BlazorApp/BlazorApp/Program.cs b/Sources/BlazorApp/BlazorApp/Program.cs
index 207ee55..e62dc74 100644
--- a/Sources/BlazorApp/BlazorApp/Program.cs
+++ b/Sources/BlazorApp/BlazorApp/Program.cs
@@ -5,6 +5,9 @@ using Blazorise;
using Blazorise.Bootstrap;
using Blazorise.Icons.FontAwesome;
using Blazored.Modal;
+using Microsoft.AspNetCore.Localization;
+using System.Globalization;
+using Microsoft.Extensions.Options;
var builder = WebApplication.CreateBuilder(args);
@@ -21,6 +24,22 @@ builder.Services
builder.Services.AddScoped();
builder.Services.AddBlazoredModal();
+// Add the controller of the app
+builder.Services.AddControllers();
+
+// Add the localization to the app and specify the resources path
+builder.Services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });
+
+// Configure the localtization
+builder.Services.Configure(options =>
+{
+ // Set the default culture of the web site
+ options.DefaultRequestCulture = new RequestCulture(new CultureInfo("en-US"));
+
+ // Declare the supported culture
+ options.SupportedCultures = new List { new CultureInfo("en-US"), new CultureInfo("fr-FR") };
+ options.SupportedUICultures = new List { new CultureInfo("en-US"), new CultureInfo("fr-FR") };
+});
var app = builder.Build();
// Configure the HTTP request pipeline.
@@ -36,7 +55,20 @@ app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
+// Get the current localization options
+var options = ((IApplicationBuilder)app).ApplicationServices.GetService>();
+if (options?.Value != null)
+{
+ // use the default localization
+ app.UseRequestLocalization(options.Value);
+}
+
+// Add the controller to the endpoint
+app.UseEndpoints(endpoints =>
+{
+ endpoints.MapControllers();
+});
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
diff --git a/Sources/BlazorApp/BlazorApp/Ressources/Pages.List.fr-FR.resx b/Sources/BlazorApp/BlazorApp/Ressources/Pages.List.fr-FR.resx
new file mode 100644
index 0000000..50bb302
--- /dev/null
+++ b/Sources/BlazorApp/BlazorApp/Ressources/Pages.List.fr-FR.resx
@@ -0,0 +1,123 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ text/microsoft-resx
+
+
+ 2.0
+
+
+ System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
+ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
+ Liste des éléments
+
+
\ No newline at end of file
diff --git a/Sources/BlazorApp/BlazorApp/Ressources/Pages.List.resx b/Sources/BlazorApp/BlazorApp/Ressources/Pages.List.resx
new file mode 100644
index 0000000..ae67689
--- /dev/null
+++ b/Sources/BlazorApp/BlazorApp/Ressources/Pages.List.resx
@@ -0,0 +1,123 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ text/microsoft-resx
+
+
+ 2.0
+
+
+ System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
+ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
+ Items List
+
+
\ No newline at end of file
diff --git a/Sources/BlazorApp/BlazorApp/Shared/CultureSelector.razor b/Sources/BlazorApp/BlazorApp/Shared/CultureSelector.razor
new file mode 100644
index 0000000..6d7f012
--- /dev/null
+++ b/Sources/BlazorApp/BlazorApp/Shared/CultureSelector.razor
@@ -0,0 +1,43 @@
+@using System.Globalization
+@inject NavigationManager NavigationManager
+
+
+
+
+
+@code
+{
+ private CultureInfo[] supportedCultures = new[]
+ {
+ new CultureInfo("en-US"),
+ new CultureInfo("fr-FR")
+ };
+
+ private CultureInfo Culture
+ {
+ get => CultureInfo.CurrentCulture;
+ set
+ {
+ if (CultureInfo.CurrentUICulture == value)
+ {
+ return;
+ }
+
+ var culture = value.Name.ToLower(CultureInfo.InvariantCulture);
+
+ var uri = new Uri(this.NavigationManager.Uri).GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped);
+ var query = $"?culture={Uri.EscapeDataString(culture)}&" + $"redirectUri={Uri.EscapeDataString(uri)}";
+
+ // Redirect the user to the culture controller to set the cookie
+ this.NavigationManager.NavigateTo("/Culture/SetCulture" + query, forceLoad: true);
+ }
+ }
+}
\ No newline at end of file
diff --git a/Sources/BlazorApp/BlazorApp/Shared/MainLayout.razor b/Sources/BlazorApp/BlazorApp/Shared/MainLayout.razor
index dd158b5..5ff591e 100644
--- a/Sources/BlazorApp/BlazorApp/Shared/MainLayout.razor
+++ b/Sources/BlazorApp/BlazorApp/Shared/MainLayout.razor
@@ -10,6 +10,9 @@