From a77611cc0897141acca3d8581531166d33ceeac6 Mon Sep 17 00:00:00 2001 From: Andres Pineda Date: Wed, 13 May 2020 19:31:31 -0400 Subject: [PATCH] feat(macOS): Add support for Localization --- src/Uno.UI/UI/Xaml/Application.macOS.cs | 27 ++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Uno.UI/UI/Xaml/Application.macOS.cs b/src/Uno.UI/UI/Xaml/Application.macOS.cs index c037312072a9..f0fa7c5efff5 100644 --- a/src/Uno.UI/UI/Xaml/Application.macOS.cs +++ b/src/Uno.UI/UI/Xaml/Application.macOS.cs @@ -9,6 +9,10 @@ using ObjCRuntime; using Windows.Graphics.Display; using Uno.UI.Services; +using System.Globalization; +using Uno.Extensions; +using Uno.Logging; +using System.Linq; namespace Windows.UI.Xaml { @@ -18,6 +22,7 @@ public partial class Application : NSApplicationDelegate public Application() { Current = this; + SetCurrentLanguage(); Windows.UI.Xaml.GenericStyles.Initialize(); ResourceHelper.ResourcesService = new ResourcesService(new[] { NSBundle.MainBundle }); } @@ -33,8 +38,8 @@ static partial void StartPartial(ApplicationInitializationCallback callback) public override void DidFinishLaunching(NSNotification notification) { - InitializationCompleted(); - OnLaunched(new LaunchActivatedEventArgs()); + InitializationCompleted(); + OnLaunched(new LaunchActivatedEventArgs()); } /// @@ -76,7 +81,23 @@ private ApplicationTheme GetDefaultSystemTheme() else { return ApplicationTheme.Dark; - } + } + } + + private void SetCurrentLanguage() + { + var language = NSLocale.PreferredLanguages.ElementAtOrDefault(0); + + try + { + var cultureInfo = CultureInfo.CreateSpecificCulture(language); + CultureInfo.CurrentUICulture = cultureInfo; + CultureInfo.CurrentCulture = cultureInfo; + } + catch (Exception ex) + { + this.Log().Error($"Failed to set culture for language: {language}", ex); + } } } }