diff --git a/TutoBlazer/App.razor b/TutoBlazer/App.razor index 623580d..54966a1 100644 --- a/TutoBlazer/App.razor +++ b/TutoBlazer/App.razor @@ -1,12 +1,14 @@ - - - - - - - Not found - -

Sorry, there's nothing at this address.

-
-
-
+ + + + + + + + Not found + +

Sorry, there's nothing at this address.

+
+
+
+
diff --git a/TutoBlazer/Controllers/CultureController.cs b/TutoBlazer/Controllers/CultureController.cs new file mode 100644 index 0000000..6c790cd --- /dev/null +++ b/TutoBlazer/Controllers/CultureController.cs @@ -0,0 +1,36 @@ +using Microsoft.AspNetCore.Localization; +using Microsoft.AspNetCore.Mvc; + +namespace TutoBlazer.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/TutoBlazer/Modals/DeleteConfirmation.razor b/TutoBlazer/Modals/DeleteConfirmation.razor new file mode 100644 index 0000000..32baae9 --- /dev/null +++ b/TutoBlazer/Modals/DeleteConfirmation.razor @@ -0,0 +1,10 @@ +
+ +

+ Are you sure you want to delete @item.DisplayName ? +

+ + + + +
diff --git a/TutoBlazer/Modals/DeleteConfirmation.razor.cs b/TutoBlazer/Modals/DeleteConfirmation.razor.cs new file mode 100644 index 0000000..5752b94 --- /dev/null +++ b/TutoBlazer/Modals/DeleteConfirmation.razor.cs @@ -0,0 +1,37 @@ +using Blazored.Modal.Services; +using Blazored.Modal; +using Microsoft.AspNetCore.Components; +using TutoBlazer.Models; + +namespace TutoBlazer.Modals +{ + public partial class DeleteConfirmation + { + [CascadingParameter] + public BlazoredModalInstance ModalInstance { get; set; } + + [Inject] + public IDataService DataService { get; set; } + + [Parameter] + public int Id { get; set; } + + private Item item = new Item(); + + protected override async Task OnInitializedAsync() + { + // Get the item + item = await DataService.GetById(Id); + } + + void ConfirmDelete() + { + ModalInstance.CloseAsync(ModalResult.Ok(true)); + } + + void Cancel() + { + ModalInstance.CancelAsync(); + } + } +} diff --git a/TutoBlazer/Pages/Index.razor b/TutoBlazer/Pages/Index.razor index b1a9fbd..b221db1 100644 --- a/TutoBlazer/Pages/Index.razor +++ b/TutoBlazer/Pages/Index.razor @@ -1,4 +1,5 @@ -@page "/" +@using System.Globalization +@page "/" Index @@ -6,4 +7,8 @@ Welcome to your new app. +

+ CurrentCulture: @CultureInfo.CurrentCulture +

+ diff --git a/TutoBlazer/Pages/List.razor b/TutoBlazer/Pages/List.razor index 98cba6e..59162a9 100644 --- a/TutoBlazer/Pages/List.razor +++ b/TutoBlazer/Pages/List.razor @@ -1,7 +1,8 @@ @page "/list" @using Models -

List

+ +

@Localizer["Title"]

diff --git a/TutoBlazer/Pages/List.razor.cs b/TutoBlazer/Pages/List.razor.cs index bd4a147..51f22e9 100644 --- a/TutoBlazer/Pages/List.razor.cs +++ b/TutoBlazer/Pages/List.razor.cs @@ -1,6 +1,10 @@ using Blazored.LocalStorage; +using Blazored.Modal; +using Blazored.Modal.Services; using Blazorise.DataGrid; using Microsoft.AspNetCore.Components; +using Microsoft.Extensions.Localization; +using TutoBlazer.Modals; using TutoBlazer.Models; namespace TutoBlazer.Pages @@ -11,12 +15,21 @@ namespace TutoBlazer.Pages private int totalItem; + [Inject] + public IStringLocalizer Localizer { get; set; } + [Inject] public IDataService DataService { get; set; } [Inject] public IWebHostEnvironment WebHostEnvironment { get; set; } + [Inject] + public NavigationManager NavigationManager { get; set; } + + [CascadingParameter] + public IModalService Modal { get; set; } + private async Task OnReadData(DataGridReadDataEventArgs e) { if (e.CancellationToken.IsCancellationRequested) @@ -31,9 +44,23 @@ namespace TutoBlazer.Pages } } - private void OnDelete(int id) + private async void OnDelete(int id) { + var parameters = new ModalParameters(); + parameters.Add(nameof(Item.Id), id); + + var modal = Modal.Show("Delete Confirmation", parameters); + var result = await modal.Result; + + if (result.Cancelled) + { + return; + } + + await DataService.Delete(id); + // Reload the page + NavigationManager.NavigateTo("list", true); } } } diff --git a/TutoBlazer/Pages/_Layout.cshtml b/TutoBlazer/Pages/_Layout.cshtml index 3577ddf..33c480f 100644 --- a/TutoBlazer/Pages/_Layout.cshtml +++ b/TutoBlazer/Pages/_Layout.cshtml @@ -33,5 +33,7 @@ + + diff --git a/TutoBlazer/Program.cs b/TutoBlazer/Program.cs index 9b55886..b1fc793 100644 --- a/TutoBlazer/Program.cs +++ b/TutoBlazer/Program.cs @@ -6,6 +6,10 @@ using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; using TutoBlazer.Data; using TutoBlazer.Services; +using Blazored.Modal; +using Microsoft.AspNetCore.Localization; +using System.Globalization; +using Microsoft.Extensions.Options; var builder = WebApplication.CreateBuilder(args); @@ -20,6 +24,24 @@ builder.Services .AddFontAwesomeIcons(); 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(); @@ -37,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/TutoBlazer/Resources/Pages.List.fr-FR.resx b/TutoBlazer/Resources/Pages.List.fr-FR.resx new file mode 100644 index 0000000..50bb302 --- /dev/null +++ b/TutoBlazer/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 des éléments + + \ No newline at end of file diff --git a/TutoBlazer/Resources/Pages.List.resx b/TutoBlazer/Resources/Pages.List.resx new file mode 100644 index 0000000..ae67689 --- /dev/null +++ b/TutoBlazer/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/TutoBlazer/Shared/CultureSelector.razor b/TutoBlazer/Shared/CultureSelector.razor new file mode 100644 index 0000000..6d7f012 --- /dev/null +++ b/TutoBlazer/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/TutoBlazer/Shared/MainLayout.razor b/TutoBlazer/Shared/MainLayout.razor index e1b765d..04a4578 100644 --- a/TutoBlazer/Shared/MainLayout.razor +++ b/TutoBlazer/Shared/MainLayout.razor @@ -10,6 +10,9 @@
About +
+ +
diff --git a/TutoBlazer/TutoBlazer.csproj b/TutoBlazer/TutoBlazer.csproj index b5ab17c..009c95f 100644 --- a/TutoBlazer/TutoBlazer.csproj +++ b/TutoBlazer/TutoBlazer.csproj @@ -12,6 +12,7 @@ + diff --git a/TutoBlazer/_Imports.razor b/TutoBlazer/_Imports.razor index 2bbafed..019fff7 100644 --- a/TutoBlazer/_Imports.razor +++ b/TutoBlazer/_Imports.razor @@ -9,3 +9,5 @@ @using TutoBlazer @using TutoBlazer.Shared @using Blazorise.DataGrid +@using Blazored.Modal +@using Blazored.Modal.Services diff --git a/TutoBlazer/wwwroot/images/lolol.png b/TutoBlazer/wwwroot/images/lolol.png deleted file mode 100644 index 935fc52..0000000 Binary files a/TutoBlazer/wwwroot/images/lolol.png and /dev/null differ diff --git a/TutoBlazer/wwwroot/images/lololo.png b/TutoBlazer/wwwroot/images/lololo.png deleted file mode 100644 index 935fc52..0000000 Binary files a/TutoBlazer/wwwroot/images/lololo.png and /dev/null differ