diff --git a/BlazorTp1/BlazorTp1.csproj b/BlazorTp1/BlazorTp1.csproj index ce09efd..7d44c3c 100644 --- a/BlazorTp1/BlazorTp1.csproj +++ b/BlazorTp1/BlazorTp1.csproj @@ -17,6 +17,7 @@ + diff --git a/BlazorTp1/Controllers/CultureController.cs b/BlazorTp1/Controllers/CultureController.cs new file mode 100644 index 0000000..328c28e --- /dev/null +++ b/BlazorTp1/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/BlazorTp1/Pages/Index.razor b/BlazorTp1/Pages/Index.razor index 6085c4a..9fa6e36 100644 --- a/BlazorTp1/Pages/Index.razor +++ b/BlazorTp1/Pages/Index.razor @@ -1,4 +1,5 @@ @page "/" +@using System.Globalization Index @@ -6,4 +7,8 @@ Welcome to your new app. +

+ CurrentCulture: @CultureInfo.CurrentCulture +

+ diff --git a/BlazorTp1/Pages/List.razor b/BlazorTp1/Pages/List.razor index 1bd5c64..c4fef1e 100644 --- a/BlazorTp1/Pages/List.razor +++ b/BlazorTp1/Pages/List.razor @@ -1,7 +1,7 @@ @page "/list" @using BlazorTp1.Models -

List

+

@Localizer["Title"]

diff --git a/BlazorTp1/Pages/List.razor.cs b/BlazorTp1/Pages/List.razor.cs index 2cc442a..f20a1fb 100644 --- a/BlazorTp1/Pages/List.razor.cs +++ b/BlazorTp1/Pages/List.razor.cs @@ -5,6 +5,7 @@ using Blazored.LocalStorage; using Blazored.Modal.Services; using Blazored.Modal; using BlazorTp1.Modals; +using Microsoft.Extensions.Localization; namespace BlazorTp1.Pages { @@ -26,6 +27,9 @@ namespace BlazorTp1.Pages [CascadingParameter] public IModalService Modal { get; set; } + [Inject] + public IStringLocalizer Localizer { get; set; } + private async Task OnReadData(DataGridReadDataEventArgs e) { if (e.CancellationToken.IsCancellationRequested) diff --git a/BlazorTp1/Program.cs b/BlazorTp1/Program.cs index 993a31c..e3cd109 100644 --- a/BlazorTp1/Program.cs +++ b/BlazorTp1/Program.cs @@ -6,6 +6,9 @@ using BlazorTp1.Data; using Blazored.Modal; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; +using Microsoft.AspNetCore.Localization; +using System.Globalization; +using Microsoft.Extensions.Options; var builder = WebApplication.CreateBuilder(args); @@ -27,6 +30,23 @@ 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. @@ -43,6 +63,21 @@ 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/BlazorTp1/Resources/Pages.List.fr-FR.resx b/BlazorTp1/Resources/Pages.List.fr-FR.resx new file mode 100644 index 0000000..941e1c7 --- /dev/null +++ b/BlazorTp1/Resources/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 d'éléments + + \ No newline at end of file diff --git a/BlazorTp1/Resources/Pages.List.resx b/BlazorTp1/Resources/Pages.List.resx new file mode 100644 index 0000000..ae67689 --- /dev/null +++ b/BlazorTp1/Resources/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/BlazorTp1/Shared/CultureSelector.razor b/BlazorTp1/Shared/CultureSelector.razor new file mode 100644 index 0000000..0f46a4e --- /dev/null +++ b/BlazorTp1/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); + } + } +} diff --git a/BlazorTp1/Shared/MainLayout.razor b/BlazorTp1/Shared/MainLayout.razor index 46ade29..e497265 100644 --- a/BlazorTp1/Shared/MainLayout.razor +++ b/BlazorTp1/Shared/MainLayout.razor @@ -12,6 +12,10 @@ About
+
+ +
+
@Body