From db92b285c6f6e6a1c5dd9eaa61714ee48a73e9d8 Mon Sep 17 00:00:00 2001 From: Alexis DRAI Date: Wed, 1 Feb 2023 17:31:36 +0100 Subject: [PATCH] :construction: Get started on i18n https://codefirst.iut.uca.fr/documentation/julien.riboulet/docusaurus/Blazor/globalisation-localisation/translate-site-resources/#location-files --- blazor_lab/Controllers/CultureController.cs | 31 +++++++++++++++ blazor_lab/Pages/Index.razor | 5 +++ blazor_lab/Program.cs | 37 ++++++++++++++++++ blazor_lab/Shared/CultureSelector.razor | 43 +++++++++++++++++++++ blazor_lab/Shared/MainLayout.razor | 5 +++ 5 files changed, 121 insertions(+) create mode 100644 blazor_lab/Controllers/CultureController.cs create mode 100644 blazor_lab/Shared/CultureSelector.razor diff --git a/blazor_lab/Controllers/CultureController.cs b/blazor_lab/Controllers/CultureController.cs new file mode 100644 index 0000000..328c28e --- /dev/null +++ b/blazor_lab/Controllers/CultureController.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/blazor_lab/Pages/Index.razor b/blazor_lab/Pages/Index.razor index b1a9fbd..0c5b2dd 100644 --- a/blazor_lab/Pages/Index.razor +++ b/blazor_lab/Pages/Index.razor @@ -1,4 +1,5 @@ @page "/" +@using System.Globalization; Index @@ -7,3 +8,7 @@ Welcome to your new app. + +

+ CurrentCulture: @CultureInfo.CurrentCulture +

\ No newline at end of file diff --git a/blazor_lab/Program.cs b/blazor_lab/Program.cs index 6d7195f..77f6d67 100644 --- a/blazor_lab/Program.cs +++ b/blazor_lab/Program.cs @@ -5,6 +5,9 @@ using Blazorise.Icons.FontAwesome; using Blazored.LocalStorage; using blazor_lab.Services; using Blazored.Modal; +using Microsoft.AspNetCore.Localization; +using System.Globalization; +using Microsoft.Extensions.Options; var builder = WebApplication.CreateBuilder(args); @@ -25,6 +28,23 @@ builder.Services.AddBlazoredLocalStorage(); 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. @@ -41,6 +61,23 @@ 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/blazor_lab/Shared/CultureSelector.razor b/blazor_lab/Shared/CultureSelector.razor new file mode 100644 index 0000000..6d7f012 --- /dev/null +++ b/blazor_lab/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/blazor_lab/Shared/MainLayout.razor b/blazor_lab/Shared/MainLayout.razor index 93eed83..e642202 100644 --- a/blazor_lab/Shared/MainLayout.razor +++ b/blazor_lab/Shared/MainLayout.razor @@ -10,6 +10,11 @@
+
+
+ +
+
@Body