From 1a6462dc73653d036c7d78b4719cbc1e7f8af01c Mon Sep 17 00:00:00 2001 From: arvalin Date: Sat, 26 Nov 2022 08:24:39 +0100 Subject: [PATCH] Add ressource files --- .../Controllers/CultureController.cs | 31 ++++ src/CraftSharp/CraftSharp.csproj | 3 +- src/CraftSharp/CraftSharp.sln | 50 +++---- src/CraftSharp/Program.cs | 37 ++++- .../Resources/Pages.List.fr-FR.resx | 135 ++++++++++++++++++ src/CraftSharp/Resources/Pages.List.resx | 135 ++++++++++++++++++ .../Resources/Pages.List.tr-TR.resx | 135 ++++++++++++++++++ src/CraftSharp/Shared/CultureSelector.razor | 44 ++++++ src/CraftSharp/Shared/MainLayout.razor | 4 +- 9 files changed, 546 insertions(+), 28 deletions(-) create mode 100644 src/CraftSharp/Controllers/CultureController.cs create mode 100644 src/CraftSharp/Resources/Pages.List.fr-FR.resx create mode 100644 src/CraftSharp/Resources/Pages.List.resx create mode 100644 src/CraftSharp/Resources/Pages.List.tr-TR.resx create mode 100644 src/CraftSharp/Shared/CultureSelector.razor diff --git a/src/CraftSharp/Controllers/CultureController.cs b/src/CraftSharp/Controllers/CultureController.cs new file mode 100644 index 0000000..7db1cda --- /dev/null +++ b/src/CraftSharp/Controllers/CultureController.cs @@ -0,0 +1,31 @@ +using Microsoft.AspNetCore.Localization; +using Microsoft.AspNetCore.Mvc; + +namespace CraftSharp.Controllers +{ + [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/src/CraftSharp/CraftSharp.csproj b/src/CraftSharp/CraftSharp.csproj index a53aa86..c9d13da 100644 --- a/src/CraftSharp/CraftSharp.csproj +++ b/src/CraftSharp/CraftSharp.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -10,6 +10,7 @@ + diff --git a/src/CraftSharp/CraftSharp.sln b/src/CraftSharp/CraftSharp.sln index 5b7bccb..e30afcd 100644 --- a/src/CraftSharp/CraftSharp.sln +++ b/src/CraftSharp/CraftSharp.sln @@ -1,25 +1,25 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.3.32929.385 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CraftSharp", "CraftSharp.csproj", "{C299F384-0C85-4A98-998B-D2704C557CC0}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {C299F384-0C85-4A98-998B-D2704C557CC0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C299F384-0C85-4A98-998B-D2704C557CC0}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C299F384-0C85-4A98-998B-D2704C557CC0}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C299F384-0C85-4A98-998B-D2704C557CC0}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {7E596F12-053A-4BAD-B71D-8EFE6B3C7C2F} - EndGlobalSection -EndGlobal + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32929.385 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CraftSharp", "CraftSharp.csproj", "{C299F384-0C85-4A98-998B-D2704C557CC0}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C299F384-0C85-4A98-998B-D2704C557CC0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C299F384-0C85-4A98-998B-D2704C557CC0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C299F384-0C85-4A98-998B-D2704C557CC0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C299F384-0C85-4A98-998B-D2704C557CC0}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7E596F12-053A-4BAD-B71D-8EFE6B3C7C2F} + EndGlobalSection +EndGlobal diff --git a/src/CraftSharp/Program.cs b/src/CraftSharp/Program.cs index 9a940f5..1390d26 100644 --- a/src/CraftSharp/Program.cs +++ b/src/CraftSharp/Program.cs @@ -1,7 +1,10 @@ using CraftSharp.Data; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; - +using Microsoft.AspNetCore.Localization; +using Microsoft.Extensions.Options; +using System.Globalization; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. @@ -9,6 +12,23 @@ builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); builder.Services.AddSingleton(); +// 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"), new CultureInfo("tr-TR") }; + options.SupportedUICultures = new List { new CultureInfo("en-US"), new CultureInfo("fr-FR"), new CultureInfo("tr-TR") }; +}); + var app = builder.Build(); // Configure the HTTP request pipeline. @@ -25,6 +45,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/src/CraftSharp/Resources/Pages.List.fr-FR.resx b/src/CraftSharp/Resources/Pages.List.fr-FR.resx new file mode 100644 index 0000000..aace2a0 --- /dev/null +++ b/src/CraftSharp/Resources/Pages.List.fr-FR.resx @@ -0,0 +1,135 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 + + + Inventaire + + + Se connecter + + + Ouverture + + + Créer un compte + + + Magasin + + \ No newline at end of file diff --git a/src/CraftSharp/Resources/Pages.List.resx b/src/CraftSharp/Resources/Pages.List.resx new file mode 100644 index 0000000..fe08772 --- /dev/null +++ b/src/CraftSharp/Resources/Pages.List.resx @@ -0,0 +1,135 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 + + + Inventory + + + Login + + + Opening + + + Register + + + Shop + + \ No newline at end of file diff --git a/src/CraftSharp/Resources/Pages.List.tr-TR.resx b/src/CraftSharp/Resources/Pages.List.tr-TR.resx new file mode 100644 index 0000000..8c050d1 --- /dev/null +++ b/src/CraftSharp/Resources/Pages.List.tr-TR.resx @@ -0,0 +1,135 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 + + + Envanter + + + Giriş yapmak + + + Açılış + + + Kayıt ol + + + Mağaza + + \ No newline at end of file diff --git a/src/CraftSharp/Shared/CultureSelector.razor b/src/CraftSharp/Shared/CultureSelector.razor new file mode 100644 index 0000000..ddca085 --- /dev/null +++ b/src/CraftSharp/Shared/CultureSelector.razor @@ -0,0 +1,44 @@ +@using System.Globalization +@inject NavigationManager NavigationManager + +

+ +

+ +@code +{ + private CultureInfo[] supportedCultures = new[] + { + new CultureInfo("en-US"), + new CultureInfo("fr-FR"), + new CultureInfo("tr-TR") + }; + + 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/src/CraftSharp/Shared/MainLayout.razor b/src/CraftSharp/Shared/MainLayout.razor index 78a06ec..798e5ac 100644 --- a/src/CraftSharp/Shared/MainLayout.razor +++ b/src/CraftSharp/Shared/MainLayout.razor @@ -11,7 +11,9 @@ - +
+ +
@Body