Skip to content

Commit

Permalink
Per-app Language Preferences 1.1 (#1458)
Browse files Browse the repository at this point in the history
* Determines language ID for each process only once when launching

* Also update language ID on mod settings change
  • Loading branch information
yezhiyi9670 authored Jan 23, 2025
1 parent 68d9676 commit c2fe499
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions mods/per-app-language-preferences.wh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// @id per-app-language-preferences
// @name Per-app Language Preferences
// @description Override the preferred UI language for specific apps.
// @version 1.0
// @version 1.1
// @author yezhiyi9670
// @github https://github.com/yezhiyi9670
// @include *
Expand All @@ -23,7 +23,9 @@ This mod allow you to override the preferred language for specific apps using th
Instructions on the settings:
- The filename pattern is parsed using [PathMatchSpecExW](https://learn.microsoft.com/en-us/windows/win32/api/shlwapi/nf-shlwapi-pathmatchspecexw). It supports wildcard `*` `?`. It also supports multiple semicolon-separated patterns.
- The file path pattern is parsed using [PathMatchSpecExW](https://learn.microsoft.com/en-us/windows/win32/api/shlwapi/nf-shlwapi-pathmatchspecexw). It supports wildcard `*` `?`. It also supports multiple semicolon-separated patterns.
Note that the full path (e.g. `C:\Program Files\MyApp\my-app.exe`), rather than the base filename (e.g. `my-app.exe`), is checked against the pattern.
To check the actual executable file path of your app, use the task manager or observe log output from this mod.
- The language ID can be found in the [MS-LCID](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-lcid/70feba9f-294e-491e-b6eb-56532684c37f) specification. For example, the language ID for English (United States) is 1033.
Use cases:
Expand All @@ -49,9 +51,9 @@ Note that it changes the language only and does not affect the encoding used in
- - glob: "*\\example-executable-file-114514.exe"
$name: Filename pattern
$description: "The pattern corresponding to the program filename."
- langId: 1033
- langId: -1
$name: Language identifier
$description: "The language ID to apply."
$description: "The language ID to apply. Set -1 to use the system default value."
$name: Overrides List
$description: >-
Assign a new language to these programs.
Expand All @@ -65,30 +67,41 @@ Note that it changes the language only and does not affect the encoding used in

// ===========================================================

int my_langid = -1;

using GetUserDefaultUILanguage_t = short(WINAPI*)();
GetUserDefaultUILanguage_t GetUserDefaultUILanguage_Original;
short WINAPI GetUserDefaultUILanguage_Hook() {
wchar_t filename_buf[2049];
// Wh_Log(L">GetUserDefaultUILanguage");

Wh_Log(L">GetUserDefaultUILanguage");
if(my_langid == -1) {
return GetUserDefaultUILanguage_Original();
} else {
return my_langid;
}
}

GetModuleFileNameW(NULL, filename_buf, 2049);
void determine_my_langid() {
wchar_t *filename_buf = new wchar_t[2048];
GetModuleFileNameW(NULL, filename_buf, 2048);
Wh_Log(L">Process file: %ls", filename_buf);

int lang_id = -1;
for(int index = 0; ; index++) {
PCWSTR glob = Wh_GetStringSetting(L"programList[%d].glob", index);
if(!*glob) {
Wh_FreeStringSetting(glob);
break;
}
if(S_OK == PathMatchSpecExW(filename_buf, glob, PMSF_MULTIPLE)) {
int lang_id = Wh_GetIntSetting(L"programList[%d].langId", index);
return lang_id;
lang_id = Wh_GetIntSetting(L"programList[%d].langId", index);
Wh_FreeStringSetting(glob);
break;
}
Wh_FreeStringSetting(glob);
}

return GetUserDefaultUILanguage_Original();
delete[] filename_buf;
my_langid = lang_id;
}

// ===========================================================
Expand Down Expand Up @@ -133,9 +146,17 @@ BOOL ModInit() {
BOOL Wh_ModInit() {
Wh_Log(L">");

determine_my_langid();

return ModInit();
}

void Wh_ModSettingsChanged() {
Wh_Log(L">");

determine_my_langid();
}

// The mod is being unloaded, free all allocated resources.
void Wh_ModUninit() {
Wh_Log(L">");
Expand Down

0 comments on commit c2fe499

Please sign in to comment.