Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
stefansjfw committed Sep 18, 2024
1 parent 7f92260 commit 174d44f
Show file tree
Hide file tree
Showing 22 changed files with 238 additions and 2 deletions.
1 change: 1 addition & 0 deletions PowerToys.sln
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "utils", "utils", "{B39DC643
src\common\utils\HDropIterator.h = src\common\utils\HDropIterator.h
src\common\utils\HttpClient.h = src\common\utils\HttpClient.h
src\common\utils\json.h = src\common\utils\json.h
src\common\utils\language_helper.h = src\common\utils\language_helper.h
src\common\utils\logger_helper.h = src\common\utils\logger_helper.h
src\common\utils\modulesRegistry.h = src\common\utils\modulesRegistry.h
src\common\utils\MsiUtils.h = src\common\utils\MsiUtils.h
Expand Down
50 changes: 50 additions & 0 deletions src/common/ManagedCommon/LanguageHelper.cs
Original file line number Diff line number Diff line change
@@ -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 sealed 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<OutGoingLanguageSettings>(data).LanguageTag;
}
catch (Exception)
{
}
}

return string.Empty;
}
}
}
22 changes: 22 additions & 0 deletions src/common/utils/language_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <filesystem>
#include <common/SettingsAPI/settings_helpers.h>
#include <common/utils/json.h>

namespace LanguageHelpers
{
inline std::wstring load_language()
{
std::filesystem::path languageJsonFilePath(PTSettingsHelper::get_root_save_folder_location() + L"\\language.json");

auto langJson = json::from_file(languageJsonFilePath.c_str());
if (!langJson.has_value())
{
return {};
}

std::wstring language = langJson->GetNamedString(L"language", L"").c_str();
return language;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ public partial class App : Application, IDisposable
/// </summary>
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) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public static T GetService<T>()
/// </summary>
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) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public partial class App : Application
/// </summary>
public App()
{
string appLanguage = LanguageHelper.LoadLanguage();
if (!string.IsNullOrEmpty(appLanguage))
{
Microsoft.Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = appLanguage;
}

Logger.InitializeLogger("\\File Locksmith\\FileLocksmithUI\\Logs");

this.InitializeComponent();
Expand Down
6 changes: 6 additions & 0 deletions src/modules/Hosts/Hosts/HostsXAML/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public partial class App : Application
/// </summary>
public App()
{
string appLanguage = LanguageHelper.LoadLanguage();
if (!string.IsNullOrEmpty(appLanguage))
{
Microsoft.Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = appLanguage;
}

InitializeComponent();

Host.HostInstance = Microsoft.Extensions.Hosting.Host.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public App()
{
Logger.InitializeLogger("\\Measure Tool\\MeasureToolUI\\Logs");

string appLanguage = LanguageHelper.LoadLanguage();
if (!string.IsNullOrEmpty(appLanguage))
{
Microsoft.Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = appLanguage;
}

this.InitializeComponent();
}

Expand Down
14 changes: 14 additions & 0 deletions src/modules/PowerOCR/PowerOCR/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Globalization;
using System.Threading;
using System.Windows;

Expand All @@ -28,6 +29,19 @@ public App()
{
Logger.InitializeLogger("\\TextExtractor\\Logs");

try
{
string appLanguage = LanguageHelper.LoadLanguage();
if (!string.IsNullOrEmpty(appLanguage))
{
PowerOCR.Properties.Resources.Culture = new System.Globalization.CultureInfo(appLanguage);
}
}
catch (CultureNotFoundException ex)
{
Logger.LogError("CultureNotFoundException: " + ex.Message);
}

NativeThreadCTS = new CancellationTokenSource();
}

Expand Down
15 changes: 15 additions & 0 deletions src/modules/Workspaces/WorkspacesEditor/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Globalization;
using System.Threading;
using System.Windows;

Expand Down Expand Up @@ -40,6 +41,20 @@ private void OnStartup(object sender, StartupEventArgs e)
Logger.InitializeLogger("\\Workspaces\\Logs");
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;

var languageTag = LanguageHelper.LoadLanguage();

if (!string.IsNullOrEmpty(languageTag))
{
try
{
WorkspacesEditor.Properties.Resources.Culture = new System.Globalization.CultureInfo(languageTag);
}
catch (CultureNotFoundException ex)
{
Logger.LogError("CultureNotFoundException: " + ex.Message);
}
}

const string appName = "Local\\PowerToys_Workspaces_Editor_InstanceMutex";
bool createdNew;
_instanceMutex = new Mutex(true, appName, out createdNew);
Expand Down
17 changes: 15 additions & 2 deletions src/modules/Workspaces/WorkspacesLauncherUI/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Globalization;
using System.Threading;
using System.Windows;
using System.Windows.Forms.Design.Behavior;

using Common.UI;
using ManagedCommon;
using WorkspacesLauncherUI.Utils;
using WorkspacesLauncherUI.ViewModels;

namespace WorkspacesLauncherUI
Expand Down Expand Up @@ -38,6 +37,20 @@ private void OnStartup(object sender, StartupEventArgs e)
Logger.InitializeLogger("\\Workspaces\\Logs");
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;

var languageTag = LanguageHelper.LoadLanguage();

if (!string.IsNullOrEmpty(languageTag))
{
try
{
WorkspacesLauncherUI.Properties.Resources.Culture = new System.Globalization.CultureInfo(languageTag);
}
catch (CultureNotFoundException ex)
{
Logger.LogError("CultureNotFoundException: " + ex.Message);
}
}

const string appName = "Local\\PowerToys_Workspaces_Launcher_InstanceMutex";
bool createdNew;
_instanceMutex = new Mutex(true, appName, out createdNew);
Expand Down
14 changes: 14 additions & 0 deletions src/modules/colorPicker/ColorPickerUI/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.ComponentModel.Composition;
using System.Globalization;
using System.Threading;
using System.Windows;

Expand All @@ -29,6 +30,19 @@ public partial class App : Application, IDisposable

protected override void OnStartup(StartupEventArgs e)
{
try
{
string appLanguage = LanguageHelper.LoadLanguage();
if (!string.IsNullOrEmpty(appLanguage))
{
ColorPicker.Properties.Resources.Culture = new System.Globalization.CultureInfo(appLanguage);
}
}
catch (CultureNotFoundException ex)
{
Logger.LogError("CultureNotFoundException: " + ex.Message);
}

NativeThreadCTS = new CancellationTokenSource();
ExitToken = NativeThreadCTS.Token;

Expand Down
15 changes: 15 additions & 0 deletions src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using System.Windows;
using System.Windows.Input;
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions src/modules/imageresizer/ui/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/

using System;
using System.Globalization;
using System.Text;
using System.Windows;

Expand All @@ -19,6 +20,19 @@ public partial class App : Application, IDisposable
{
static App()
{
try
{
string appLanguage = LanguageHelper.LoadLanguage();
if (!string.IsNullOrEmpty(appLanguage))
{
ImageResizer.Properties.Resources.Culture = new System.Globalization.CultureInfo(appLanguage);
}
}
catch (CultureNotFoundException)
{
// error
}

Console.InputEncoding = Encoding.Unicode;
}

Expand Down
1 change: 1 addition & 0 deletions src/modules/imageresizer/ui/ImageResizerUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<ProjectReference Include="..\..\..\common\GPOWrapperProjection\GPOWrapperProjection.csproj" />
<ProjectReference Include="..\..\..\common\interop\PowerToys.Interop.vcxproj" />
<ProjectReference Include="..\..\..\common\Common.UI\Common.UI.csproj" />
<ProjectReference Include="..\..\..\common\ManagedCommon\ManagedCommon.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
Expand Down
13 changes: 13 additions & 0 deletions src/modules/launcher/PowerLauncher/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ public static void Main()
{
NativeThreadCTS = new CancellationTokenSource();

try
{
string appLanguage = LanguageHelper.LoadLanguage();
if (!string.IsNullOrEmpty(appLanguage))
{
PowerLauncher.Properties.Resources.Culture = new System.Globalization.CultureInfo(appLanguage);
}
}
catch (CultureNotFoundException ex)
{
Logger.LogError("CultureNotFoundException: " + ex.Message);
}

Log.Info($"Starting PowerToys Run with PID={Environment.ProcessId}", typeof(App));
if (PowerToys.GPOWrapperProjection.GPOWrapper.GetConfiguredPowerLauncherEnabledValue() == PowerToys.GPOWrapperProjection.GpoRuleConfigured.Disabled)
{
Expand Down
6 changes: 6 additions & 0 deletions src/modules/peek/Peek.UI/PeekXAML/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public IHost Host
/// </summary>
public App()
{
string appLanguage = LanguageHelper.LoadLanguage();
if (!string.IsNullOrEmpty(appLanguage))
{
Microsoft.Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = appLanguage;
}

InitializeComponent();
Logger.InitializeLogger("\\Peek\\Logs");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <common/logger/logger.h>
#include <common/logger/logger_settings.h>
#include <common/utils/language_helper.h>
#include <common/utils/logger_helper.h>
#include <common/utils/gpo.h>

Expand All @@ -33,6 +34,12 @@ const std::wstring moduleName = L"PowerRename";
/// </summary>
App::App()
{
std::wstring appLanguage = LanguageHelpers::load_language();
if (!appLanguage.empty())
{
Microsoft::Windows::Globalization::ApplicationLanguages::PrimaryLanguageOverride(appLanguage);
}

InitializeComponent();

#if defined _DEBUG && !defined DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
Expand Down
1 change: 1 addition & 0 deletions src/modules/powerrename/PowerRenameUILib/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@
#include <winrt/Microsoft.UI.Xaml.Shapes.h>
#include <winrt/Microsoft.UI.Dispatching.h>
#include <winrt/Microsoft.Windows.ApplicationModel.Resources.h>
#include <winrt/Microsoft.Windows.Globalization.h>
#include <wil/cppwinrt_helpers.h>
Loading

1 comment on commit 174d44f

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@check-spelling-bot Report

🔴 Please review

See the 📜action log or 📝 job summary for details.

Unrecognized words (1)

initlanguages

Previously acknowledged words that are now absent applayout appsfolder cswinrt systemsettings SYSTEMWOW USEPOSITION USESIZE 🫥
To accept these unrecognized words as correct and remove the previously acknowledged and now absent words, you could run the following commands

... in a clone of the [email protected]:microsoft/PowerToys.git repository
on the stefan/lang_override branch (ℹ️ how do I use this?):

curl -s -S -L 'https://raw.githubusercontent.com/check-spelling/check-spelling/v0.0.22/apply.pl' |
perl - 'https://github.com/microsoft/PowerToys/actions/runs/10921519481/attempts/1'
Available 📚 dictionaries could cover words (expected and unrecognized) not in the 📘 dictionary

This includes both expected items (1897) from .github/actions/spell-check/expect.txt and unrecognized words (1)

Dictionary Entries Covers Uniquely
cspell:r/src/r.txt 543 1 1
cspell:cpp/src/people.txt 23 1
cspell:cpp/src/ecosystem.txt 51 1

Consider adding them (in .github/workflows/spelling2.yml) for uses: check-spelling/[email protected] in its with:

      with:
        extra_dictionaries:
          cspell:r/src/r.txt
          cspell:cpp/src/people.txt
          cspell:cpp/src/ecosystem.txt

To stop checking additional dictionaries, add (in .github/workflows/spelling2.yml) for uses: check-spelling/[email protected] in its with:

check_extra_dictionaries: ''
Warnings (1)

See the 📜action log or 📝 job summary for details.

ℹ️ Warnings Count
ℹ️ non-alpha-in-dictionary 1

See ℹ️ Event descriptions for more information.

If the flagged items are 🤯 false positives

If items relate to a ...

  • binary file (or some other file you wouldn't want to check at all).

    Please add a file path to the excludes.txt file matching the containing file.

    File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

    ^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

  • well-formed pattern.

    If you can write a pattern that would match it,
    try adding it to the patterns.txt file.

    Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

    Note that patterns can't match multiline strings.

Please sign in to comment.