How to determine region value (CN, SG) from HKL?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
Seven0528
Posts: 390
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

How to determine region value (CN, SG) from HKL?

09 Apr 2024, 08:29

20240409_215427.png
20240409_215427.png (9.85 KiB) Viewed 122 times

Code: Select all

msgbox getUserLanguageTagsFromPS()
msgbox getUserLanguageTagsFromPS(false)

GetUserLanguageTagsFromPS(force:=true, timeout:=1)    { ;  ahk2.0
  static command:="
    (Join LTrim RTrim0
        powershell.exe -NoLogo -NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -Command 
        $languages = Get-WinUserLanguageList;
        foreach ($lang in $languages) {;
            Write-Host ""$($lang.LanguageTag)"";
        };
    )"
    ,prevStr
    if (!force && isSet(prevStr))
        return prevStr
	detectHiddenWindows (prevDHW:=A_DetectHiddenWindows,"On")
    loop 1    {
        run(A_ComSpec,, "Hide", &pid)
        if (!winWait("ahk_pid " pid,, timeout))
            break
        if (dllCall("Kernel32.dll\AttachConsole", "UInt",pid))    {
            try str:=rTrim(comObject("WScript.Shell").Exec(command).StdOut.ReadAll(),"`n")
            dllCall("Kernel32.dll\FreeConsole")
        }
        winClose("ahk_pid " pid)
    }
    detectHiddenWindows prevDHW
    if (isSet(str))
        prevStr:=str
    return str
}
 The code is much slower than I anticipated.
Does anyone have a better idea to obtain LanguageTag?

My ultimate goal is to determine the region values of Simplified Chinese and Traditional Chinese installed on a computer. (Therefore, it's okay if it's not this method specifically.)
For example, Simplified Chinese could have values like zh-CN or zh-SG. For Traditional Chinese, it could be one of zh-HK, zh-MO, or zh-TW.
If you're wondering why this functionality is needed, I need to apply different Chinese fonts based on the region values in the program I'm developing.
I want to reflect slight differences in languages across regions, in addition to this.
(e.g., I want to apply Noto Sans HK and Noto Sans TW according to the installed language's tag.)

I've tried quite a few things so far.
GetKeyboardLayout is not useful in this case as it returns 0x08040804 or 0x04040404 regardless of the region.
GetKeyboardLayoutList and EnumUILanguages were also not useful in this case.
Referencing the registry under HKEY_CURRENT_USER\Keyboard Layout\Preload or HKEY_CURRENT_USER\Keyboard Layout\Substitutes was also not meaningful as they contain 00000804 or 00000404 regardless of the region.
GetUserDefaultLCID or GetUserDefaultLocaleName was quite useful when the display language was Chinese, but it's not meaningful if the display language is not Chinese. (The anticipated users are not limited to Chinese individuals.)
I even went as far as exploring ITfInputProcessorProfileMgr::EnumProfiles through C++, but even then, I couldn't determine the region values.
It seems that the locale values for Chinese have different characteristics compared to those for English locales.
For instance, while en-UK and en-US can be installed simultaneously, zh-CN and zh-SG cannot.
The former reports different HKL values, whereas the latter uses completely identical HKLs.


The only successful method I've found is to check Get-WinUserLanguageList and parse the returned values.
But I'm looking for a better method than this.
I'm awaiting your advice. Thank you for reading!
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Theleifblower and 22 guests