From 8ea2891b8a9b0a356c04e8ce013f8d77271aacce Mon Sep 17 00:00:00 2001 From: Yaser Moradi Date: Thu, 6 Jun 2024 16:59:41 +0200 Subject: [PATCH] fix(templates): resolve Boilerplate without multilingual feature issue in Blazor WASM #7718 (#7724) --- .../Extensions/IConfigurationExtensions.cs | 9 +++++++++ .../Boilerplate.Shared/Services/CultureInfoManager.cs | 6 +++--- .../Components/Layout/Footer.razor.cs | 11 +++++++---- .../Boilerplate.Client.Windows/MainWindow.xaml.cs | 5 ++++- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Boilerplate.Shared/Extensions/IConfigurationExtensions.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Boilerplate.Shared/Extensions/IConfigurationExtensions.cs index 0a504b86de..504d78097a 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Boilerplate.Shared/Extensions/IConfigurationExtensions.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Boilerplate.Shared/Extensions/IConfigurationExtensions.cs @@ -6,6 +6,15 @@ public static string GetApiServerAddress(this IConfiguration configuration) { var apiServerAddress = configuration.GetValue("ApiServerAddress", defaultValue: "/")!; + if (BuildConfiguration.IsDebug() && + apiServerAddress.Contains("localhost", StringComparison.InvariantCultureIgnoreCase) && + OperatingSystem.IsAndroid()) + { + const string androidEmulatorDevMachineIP = "10.0.2.2"; // Special alias to your host loopback interface in Android Emulators (127.0.0.1 on your development machine) + + apiServerAddress = apiServerAddress.Replace("localhost", androidEmulatorDevMachineIP, StringComparison.InvariantCultureIgnoreCase); + } + return Uri.TryCreate(apiServerAddress, UriKind.RelativeOrAbsolute, out _) ? apiServerAddress.TrimEnd('/') : throw new InvalidOperationException($"Api server address {apiServerAddress} is invalid"); diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Boilerplate.Shared/Services/CultureInfoManager.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Boilerplate.Shared/Services/CultureInfoManager.cs index 07ab57b423..08f9ddf510 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Boilerplate.Shared/Services/CultureInfoManager.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Boilerplate.Shared/Services/CultureInfoManager.cs @@ -4,9 +4,9 @@ namespace Boilerplate.Shared.Services; public class CultureInfoManager { - public static CultureInfo DefaultCulture { get; } = CreateCultureInfo("en-US"); + public static CultureInfo DefaultCulture => CreateCultureInfo("en-US"); - public static (string DisplayName, CultureInfo Culture)[] SupportedCultures { get; } = + public static (string DisplayName, CultureInfo Culture)[] SupportedCultures => [ ("English US", CreateCultureInfo("en-US")), ("English UK", CreateCultureInfo("en-GB")), @@ -37,7 +37,7 @@ public void SetCurrentCulture(string cultureName) /// /// This is an example to demonstrate the way you can customize application culture /// - public static CultureInfo CustomizeCultureInfoForFaCulture(CultureInfo cultureInfo) + private static CultureInfo CustomizeCultureInfoForFaCulture(CultureInfo cultureInfo) { cultureInfo.DateTimeFormat.AMDesignator = "ق.ظ"; cultureInfo.DateTimeFormat.PMDesignator = "ب.ظ"; diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/Layout/Footer.razor.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/Layout/Footer.razor.cs index d80bdbdab8..b1f034b640 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/Layout/Footer.razor.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/Layout/Footer.razor.cs @@ -13,11 +13,14 @@ public partial class Footer protected override async Task OnInitAsync() { - cultures = CultureInfoManager.SupportedCultures - .Select(sc => new BitDropdownItem { Value = sc.Culture.Name, Text = sc.DisplayName }) - .ToArray(); + if (AppRenderMode.MultilingualEnabled) + { + cultures = CultureInfoManager.SupportedCultures + .Select(sc => new BitDropdownItem { Value = sc.Culture.Name, Text = sc.DisplayName }) + .ToArray(); - SelectedCulture = CultureInfo.CurrentUICulture.Name; + SelectedCulture = CultureInfo.CurrentUICulture.Name; + } await base.OnInitAsync(); } diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/MainWindow.xaml.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/MainWindow.xaml.cs index 36d77c513e..f747e21699 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/MainWindow.xaml.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/MainWindow.xaml.cs @@ -11,7 +11,10 @@ public MainWindow() services.ConfigureServices(); InitializeComponent(); BlazorWebView.Services = services.BuildServiceProvider(); - BlazorWebView.Services.GetRequiredService().SetCurrentCulture(App.Current.Properties["Culture"]?.ToString() ?? CultureInfo.CurrentUICulture.Name); + if (AppRenderMode.MultilingualEnabled) + { + BlazorWebView.Services.GetRequiredService().SetCurrentCulture(App.Current.Properties["Culture"]?.ToString() ?? CultureInfo.CurrentUICulture.Name); + } BlazorWebView.Services.GetRequiredService().Subscribe(PubSubMessages.CULTURE_CHANGED, async culture => { App.Current.Shutdown();