diff --git a/src/common/ManagedCommon/LanguageHelper.cs b/src/common/ManagedCommon/LanguageHelper.cs new file mode 100644 index 00000000000..49ed7835d1d --- /dev/null +++ b/src/common/ManagedCommon/LanguageHelper.cs @@ -0,0 +1,50 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.IO; +using System.IO.Abstractions; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace ManagedCommon +{ + public static class LanguageHelper + { + public const string SettingsFilePath = "\\Microsoft\\PowerToys\\"; + public const string SettingsFile = "language.json"; + + internal class OutGoingLanguageSettings + { + [JsonPropertyName("language")] + public string LanguageTag { get; set; } + } + + public static string LoadLanguage() + { + FileSystem fileSystem = new FileSystem(); + var localAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); + var file = localAppDataDir + SettingsFilePath + SettingsFile; + + if (fileSystem.File.Exists(file)) + { + try + { + Stream inputStream = fileSystem.File.Open(file, FileMode.Open); + StreamReader reader = new StreamReader(inputStream); + string data = reader.ReadToEnd(); + inputStream.Close(); + reader.Dispose(); + + return JsonSerializer.Deserialize(data).LanguageTag; + } + catch (Exception) + { + } + } + + return string.Empty; + } + } +} diff --git a/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/App.xaml.cs b/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/App.xaml.cs index 3e3fc9c4b50..3f990ef6faf 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/App.xaml.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/App.xaml.cs @@ -50,6 +50,12 @@ public partial class App : Application, IDisposable /// public App() { + string appLanguage = LanguageHelper.LoadLanguage(); + if (!string.IsNullOrEmpty(appLanguage)) + { + Microsoft.Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = appLanguage; + } + this.InitializeComponent(); Host = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder().UseContentRoot(AppContext.BaseDirectory).ConfigureServices((context, services) => diff --git a/src/modules/EnvironmentVariables/EnvironmentVariables/EnvironmentVariablesXAML/App.xaml.cs b/src/modules/EnvironmentVariables/EnvironmentVariables/EnvironmentVariablesXAML/App.xaml.cs index 61c8e33f178..bc6f5aa1dac 100644 --- a/src/modules/EnvironmentVariables/EnvironmentVariables/EnvironmentVariablesXAML/App.xaml.cs +++ b/src/modules/EnvironmentVariables/EnvironmentVariables/EnvironmentVariablesXAML/App.xaml.cs @@ -44,6 +44,12 @@ public static T GetService() /// public App() { + string appLanguage = LanguageHelper.LoadLanguage(); + if (!string.IsNullOrEmpty(appLanguage)) + { + Microsoft.Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = appLanguage; + } + this.InitializeComponent(); Host = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder().UseContentRoot(AppContext.BaseDirectory).ConfigureServices((context, services) => diff --git a/src/modules/FileLocksmith/FileLocksmithUI/FileLocksmithXAML/App.xaml.cs b/src/modules/FileLocksmith/FileLocksmithUI/FileLocksmithXAML/App.xaml.cs index 8e5ff669de5..e6186c46c8f 100644 --- a/src/modules/FileLocksmith/FileLocksmithUI/FileLocksmithXAML/App.xaml.cs +++ b/src/modules/FileLocksmith/FileLocksmithUI/FileLocksmithXAML/App.xaml.cs @@ -23,6 +23,12 @@ public partial class App : Application /// public App() { + string appLanguage = LanguageHelper.LoadLanguage(); + if (!string.IsNullOrEmpty(appLanguage)) + { + Microsoft.Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = appLanguage; + } + Logger.InitializeLogger("\\File Locksmith\\FileLocksmithUI\\Logs"); this.InitializeComponent(); diff --git a/src/modules/colorPicker/ColorPickerUI/App.xaml.cs b/src/modules/colorPicker/ColorPickerUI/App.xaml.cs index b90e2318924..95c4aa648cb 100644 --- a/src/modules/colorPicker/ColorPickerUI/App.xaml.cs +++ b/src/modules/colorPicker/ColorPickerUI/App.xaml.cs @@ -4,11 +4,13 @@ using System; using System.ComponentModel.Composition; +using System.Globalization; using System.Threading; using System.Windows; using ColorPicker.Mouse; using ManagedCommon; +using Microsoft.PowerToys.Settings.UI.Library; namespace ColorPickerUI { @@ -29,6 +31,19 @@ public partial class App : Application, IDisposable protected override void OnStartup(StartupEventArgs e) { + try + { + string appLanguage = LanguageModel.LoadSetting(); + if (!string.IsNullOrEmpty(appLanguage)) + { + ColorPicker.Properties.Resources.Culture = new System.Globalization.CultureInfo(appLanguage); + } + } + catch (CultureNotFoundException) + { + // Add logging here + } + NativeThreadCTS = new CancellationTokenSource(); ExitToken = NativeThreadCTS.Token; diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs b/src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs index 2edb096018b..2906c759f1b 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs @@ -4,6 +4,7 @@ using System; using System.Diagnostics; +using System.Globalization; using System.Threading; using System.Windows; using System.Windows.Input; @@ -55,6 +56,20 @@ private void DebugModeCheck() public App() { + var languageTag = LanguageHelper.LoadLanguage(); + + if (!string.IsNullOrEmpty(languageTag)) + { + try + { + FancyZonesEditor.Properties.Resources.Culture = new System.Globalization.CultureInfo(languageTag); + } + catch (CultureNotFoundException ex) + { + Logger.LogError("CultureNotFoundException: " + ex.Message); + } + } + Logger.InitializeLogger("\\FancyZones\\Editor\\Logs"); // DebugModeCheck();