diff --git a/BlazorTuto/BlazorTuto.csproj b/BlazorTuto/BlazorTuto.csproj index f2eba01..5f6950d 100644 --- a/BlazorTuto/BlazorTuto.csproj +++ b/BlazorTuto/BlazorTuto.csproj @@ -12,6 +12,7 @@ + diff --git a/BlazorTuto/Components/Card.razor b/BlazorTuto/Components/Card.razor new file mode 100644 index 0000000..8dfeeae --- /dev/null +++ b/BlazorTuto/Components/Card.razor @@ -0,0 +1,6 @@ +@typeparam TItem +
+ @CardHeader(Item) + @CardBody(Item) + @CardFooter +
\ No newline at end of file diff --git a/BlazorTuto/Components/Card.razor.cs b/BlazorTuto/Components/Card.razor.cs new file mode 100644 index 0000000..1695559 --- /dev/null +++ b/BlazorTuto/Components/Card.razor.cs @@ -0,0 +1,19 @@ +using Microsoft.AspNetCore.Components; + +namespace BlazorTuto.Components +{ + public partial class Card + { + [Parameter] + public RenderFragment CardBody { get; set; } + + [Parameter] + public RenderFragment CardFooter { get; set; } + + [Parameter] + public RenderFragment CardHeader { get; set; } + + [Parameter] + public TItem Item { get; set; } + } +} diff --git a/BlazorTuto/Controllers/CultureController.cs b/BlazorTuto/Controllers/CultureController.cs new file mode 100644 index 0000000..a79cf98 --- /dev/null +++ b/BlazorTuto/Controllers/CultureController.cs @@ -0,0 +1,36 @@ +using Microsoft.AspNetCore.Localization; +using Microsoft.AspNetCore.Mvc; + + +namespace BlazorTuto.Controllers +{ + + /// + /// 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); + } + } +} diff --git a/BlazorTuto/Models/Cake.cs b/BlazorTuto/Models/Cake.cs new file mode 100644 index 0000000..7bbf40d --- /dev/null +++ b/BlazorTuto/Models/Cake.cs @@ -0,0 +1,9 @@ +namespace BlazorTuto.Models +{ + public class Cake + { + public int Id { get; set; } + public string Name { get; set; } + public decimal Cost { get; set; } + } +} diff --git a/BlazorTuto/Pages/Index.razor b/BlazorTuto/Pages/Index.razor index b1a9fbd..b3caa37 100644 --- a/BlazorTuto/Pages/Index.razor +++ b/BlazorTuto/Pages/Index.razor @@ -1,4 +1,10 @@ @page "/" +@using System.Globalization +@using BlazorTuto.Components + +

+ CurrentCulture: @CultureInfo.CurrentCulture +

Index @@ -6,4 +12,23 @@ Welcome to your new app. + + +
+ Cake Token Number - @context.Id +
+
+ +
+
@context.Name
+
$ @context.Cost
+
+
+ + + +
+ diff --git a/BlazorTuto/Pages/Index.razor.cs b/BlazorTuto/Pages/Index.razor.cs new file mode 100644 index 0000000..6e0960a --- /dev/null +++ b/BlazorTuto/Pages/Index.razor.cs @@ -0,0 +1,14 @@ +using BlazorTuto.Models; + +namespace BlazorTuto.Pages +{ + public partial class Index + { + private Cake CakeItem = new Cake + { + Id = 1, + Name = "Black Forest", + Cost = 50 + }; + } +} diff --git a/BlazorTuto/Pages/List.razor b/BlazorTuto/Pages/List.razor index ac12fd2..93e3256 100644 --- a/BlazorTuto/Pages/List.razor +++ b/BlazorTuto/Pages/List.razor @@ -1,7 +1,7 @@ @page "/list" @using BlazorTuto.Models -

List

+

@Localizer["Title"]

diff --git a/BlazorTuto/Pages/List.razor.cs b/BlazorTuto/Pages/List.razor.cs index 436e135..164587d 100644 --- a/BlazorTuto/Pages/List.razor.cs +++ b/BlazorTuto/Pages/List.razor.cs @@ -6,6 +6,7 @@ using BlazorTuto.Modals; using BlazorTuto.Models; using BlazorTuto.Services; using Microsoft.AspNetCore.Components; +using Microsoft.Extensions.Localization; namespace BlazorTuto.Pages { @@ -15,6 +16,9 @@ namespace BlazorTuto.Pages private int totalItem; + [Inject] + public IStringLocalizer Localizer { get; set; } + [Inject] public HttpClient Http { get; set; } diff --git a/BlazorTuto/Program.cs b/BlazorTuto/Program.cs index f48387e..5d54d9f 100644 --- a/BlazorTuto/Program.cs +++ b/BlazorTuto/Program.cs @@ -7,6 +7,9 @@ using BlazorTuto.Services; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; using Blazored.Modal; +using Microsoft.AspNetCore.Localization; +using System.Globalization; +using Microsoft.Extensions.Options; var builder = WebApplication.CreateBuilder(args); @@ -23,6 +26,23 @@ builder.Services.AddScoped(); builder.Services.AddBlazoredLocalStorage(); 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. @@ -39,6 +59,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/BlazorTuto/Resources/Page.List.fr-FR.resx b/BlazorTuto/Resources/Page.List.fr-FR.resx new file mode 100644 index 0000000..5fcc384 --- /dev/null +++ b/BlazorTuto/Resources/Page.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/BlazorTuto/Resources/Page.List.resx b/BlazorTuto/Resources/Page.List.resx new file mode 100644 index 0000000..ddc4ba1 --- /dev/null +++ b/BlazorTuto/Resources/Page.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/BlazorTuto/Shared/CultureSelector.razor b/BlazorTuto/Shared/CultureSelector.razor new file mode 100644 index 0000000..302a9ea --- /dev/null +++ b/BlazorTuto/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/BlazorTuto/Shared/MainLayout.razor b/BlazorTuto/Shared/MainLayout.razor index 25453dc..2fafdfb 100644 --- a/BlazorTuto/Shared/MainLayout.razor +++ b/BlazorTuto/Shared/MainLayout.razor @@ -10,6 +10,10 @@
About + +
+ +