PureBasic Forum
https://www.purebasic.fr/english/

Detecting the active display language in windows 7 upwards
https://www.purebasic.fr/english/viewtopic.php?f=5&t=72648
Page 1 of 1

Author:  Bitblazer [ Mon Apr 15, 2019 5:09 pm ]
Post subject:  Detecting the active display language in windows 7 upwards

Hi,

i am trying to correctly and reliably detect the currently used display language of a windows installation. It is simple to do by scanning the output, but i have'nt found a working reliable API way so far.

For exaple my main machine is using an english windows7 ultimate installation which has the german language pack installed as optional display language pack. If i call system commands on the commandline like "diskpart", the output differs depending on the display language i select.

The topic has been brought up on other forums including slashdot and there seems to be an easy solution by api calls like GetSystemDefaultLangID_() or GetUserDefaultLangID_() but none of them works reliably in windows 7 and windows 10 for me.

Does anybody know a reliable win32 API way to do it?

MSI maybe? It should have the answer, but where?

Thanks in advance!

Author:  RASHAD [ Mon Apr 15, 2019 6:01 pm ]
Post subject:  Re: Detecting the active display language in windows 7 upwar

Tested with Windows 10
Code:
kbd$ = Space(12)
OpenWindow(0,0,0,100,100,"test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StringGadget(0,0,0,0,0,"") ;Dummy Gadget
ButtonGadget(1,10,60,120,20,"Get Active Language")
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1
    Case #PB_Event_Gadget
      Select EventGadget()       
        Case 0
          Select EventType()
            Case #PB_EventType_Focus
              GetKeyboardLayoutName_(@kbd$)
              Debug kbd$
          EndSelect
         
        Case 1
          SetFocus_(GadgetID(0))
      EndSelect
  EndSelect
Until Quit = 1
End


Code:
kbd$ = Space(12)
OpenWindow(0,0,0,120,24,"test", #PB_Window_BorderLess | #PB_Window_ScreenCentered)
StringGadget(0,0,0,0,0,"") ;Dummy Gadget
ButtonGadget(1,0,0,120,24,"Get Active Language")
Repeat
  Select WaitWindowEvent()
    Case #WM_CHAR
      Quit = EventwParam()
     
    Case #PB_Event_Gadget
      Select EventGadget()       
        Case 0
          Select EventType()
            Case #PB_EventType_Focus
              GetKeyboardLayoutName_(@kbd$)
              SetActiveGadget(-1)
              MessageRequester("Info","Active Keboard : "+kbd$,#MB_OK|#MB_ICONINFORMATION)             
          EndSelect
         
        Case 1
          SetFocus_(GadgetID(0))
      EndSelect
  EndSelect
Until Quit = 27
End

Author:  VB6_to_PBx [ Mon Apr 15, 2019 7:04 pm ]
Post subject:  Re: Detecting the active display language in windows 7 upwar

Rashad , thank you for your Codes above , as i needed that also !

much more additional info in these 2 Links :

https://docs.microsoft.com/en-us/windows-hardware/manufacture/desktop/default-input-locales-for-windows-language-packs

https://docs.microsoft.com/en-us/windows-hardware/manufacture/desktop/available-language-packs-for-windows

Author:  Bitblazer [ Mon Apr 15, 2019 7:48 pm ]
Post subject:  Re: Detecting the active display language in windows 7 upwar

Thanks Rashad, but thats always 0407 (keyboard language name - just like MSI win32_operatingsystem\locale). What i need is the display language setting instead.

Author:  RASHAD [ Mon Apr 15, 2019 8:14 pm ]
Post subject:  Re: Detecting the active display language in windows 7 upwar

Check the Registry for
Windows 10 :
HKCU\Control Panel\Desktop\MuiCached

For other Windows versions
HKCU\Control Panel\Desktop\MultiUILanguageId

Code:
sRemMachName.s = ""
lTopLevelKey.l = #HKEY_CURRENT_USER
sKeyName.s = "Control Panel\Desktop\MuiCached"
sValueName.s = "MachinePreferredUILanguages"

Procedure.l QueryValueEx(lhkey.l, szValueName.s)
    Shared vValue.s
    cch = 255
    sValue.s = Space(255)
    lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, 0, @cch)       
    Select lType
        Case #REG_MULTI_SZ
            lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, @sValue.s, @cch)
            If lrc = 0
                vValue.s = Left(sValue.s, cch-1)
            Else
                vValue.s = "Empty"
            EndIf
        Default
            lrc = -1
    EndSelect
    ProcedureReturn lrc
EndProcedure

lRetVal = RegConnectRegistry_(sRemMachName, lTopLevelKey.l, @lHKeyhandle)
lRetVal = RegOpenKeyEx_(lHKeyhandle, sKeyName, 0, #KEY_ALL_ACCESS, @lhkey)
lRetVal = QueryValueEx(lhkey, sValueName)
RegCloseKey_(lhkey)

If lRetVal = 0
  Debug vValue.s
EndIf

Author:  Bitblazer [ Mon Apr 15, 2019 8:47 pm ]
Post subject:  Re: Detecting the active display language in windows 7 upwar

Thanks but same problem - it reports en-us while the german language is active (de-de i guess) in win7.

Maybe its just a win7 problem?
\
ps: a registry search and 3 tests did seem to point at hkey_currentuser\control panel\Desktop\MuiCached\MachinePreferedUILanguages in win7

Author:  RASHAD [ Mon Apr 15, 2019 9:02 pm ]
Post subject:  Re: Detecting the active display language in windows 7 upwar

When you start your OS
What is the Active display language ?

Author:  Bitblazer [ Tue Apr 16, 2019 5:36 am ]
Post subject:  Re: Detecting the active display language in windows 7 upwar

This works with win7 even if you switch the language after boot. Thanks Rashad.

Code:
sRemMachName.s = ""
lTopLevelKey.l = #HKEY_CURRENT_USER
sKeyName.s = "Control Panel\Desktop\"
sValueName.s = "PreferredUILanguages"

Procedure.l QueryValueEx(lhkey.l, szValueName.s)
    Shared vValue.s
    cch = 255
    sValue.s = Space(255)
    lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, 0, @cch)       
    Select lType
        Case #REG_MULTI_SZ
            lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, @sValue.s, @cch)
            If lrc = 0
                vValue.s = Left(sValue.s, cch-1)
            Else
                vValue.s = "Empty"
            EndIf
        Default
            lrc = -1
    EndSelect
    ProcedureReturn lrc
EndProcedure

lRetVal = RegConnectRegistry_(sRemMachName, lTopLevelKey.l, @lHKeyhandle)
lRetVal = RegOpenKeyEx_(lHKeyhandle, sKeyName, 0, #KEY_ALL_ACCESS, @lhkey)
lRetVal = QueryValueEx(lhkey, sValueName)
RegCloseKey_(lhkey)

If lRetVal = 0
  Debug vValue.s
EndIf

Author:  kvitaliy [ Thu Apr 18, 2019 5:38 am ]
Post subject:  Re: Detecting the active display language in windows 7 upwar

There is no such key in my system (win 7x64)
Quote:
lTopLevelKey.l = #HKEY_CURRENT_USER
sKeyName.s = "Control Panel\Desktop\"
sValueName.s = "PreferredUILanguages"

This code works:
Code:
sRemMachName.s = ""
lTopLevelKey.l = #HKEY_CURRENT_USER
sKeyName.s = "Control Panel\International"
sValueName.s ="LocaleName" ;  or "sLanguage"  ; or "sCountry"

Procedure.l QueryValueEx(lhkey.l, szValueName.s)
    Shared vValue.s
    cch = 255
    sValue.s = Space(255)
    lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, 0, @cch)       
    Select lType
        Case #REG_SZ
            lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, @sValue.s, @cch)
            If lrc = 0
                vValue.s = Left(sValue.s, cch-1)
            Else
                vValue.s = "Empty"
            EndIf
        Default
            lrc = -1
    EndSelect
    ProcedureReturn lrc
EndProcedure

lRetVal = RegConnectRegistry_(sRemMachName, lTopLevelKey.l, @lHKeyhandle)
lRetVal = RegOpenKeyEx_(lHKeyhandle, sKeyName, 0, #KEY_ALL_ACCESS, @lhkey)
lRetVal = QueryValueEx(lhkey, sValueName)
RegCloseKey_(lhkey)

If lRetVal = 0
  Debug vValue.s
EndIf

Author:  Bitblazer [ Thu Apr 18, 2019 11:56 am ]
Post subject:  Re: Detecting the active display language in windows 7 upwar

kvitaliy wrote:
There is no such key in my system (win 7x64)
Quote:
lTopLevelKey.l = #HKEY_CURRENT_USER
sKeyName.s = "Control Panel\Desktop\"
sValueName.s = "PreferredUILanguages"

This code works:
Code:
sRemMachName.s = ""
lTopLevelKey.l = #HKEY_CURRENT_USER
sKeyName.s = "Control Panel\International"
sValueName.s ="LocaleName" ;  or "sLanguage"  ; or "sCountry"

Procedure.l QueryValueEx(lhkey.l, szValueName.s)
    Shared vValue.s
    cch = 255
    sValue.s = Space(255)
    lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, 0, @cch)       
    Select lType
        Case #REG_SZ
            lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, @sValue.s, @cch)
            If lrc = 0
                vValue.s = Left(sValue.s, cch-1)
            Else
                vValue.s = "Empty"
            EndIf
        Default
            lrc = -1
    EndSelect
    ProcedureReturn lrc
EndProcedure

lRetVal = RegConnectRegistry_(sRemMachName, lTopLevelKey.l, @lHKeyhandle)
lRetVal = RegOpenKeyEx_(lHKeyhandle, sKeyName, 0, #KEY_ALL_ACCESS, @lhkey)
lRetVal = QueryValueEx(lhkey, sValueName)
RegCloseKey_(lhkey)

If lRetVal = 0
  Debug vValue.s
EndIf


beware - on my english win7 ultimate installation with two installed display languages (german and english), the following keys all point to german

Code:
lTopLevelKey.l = #HKEY_CURRENT_USER
sKeyName.s = "Control Panel\International"
sValueName.s ="LocaleName" ;  or "sLanguage"  ; or "sCountry"


while
Code:
lTopLevelKey.l = #HKEY_CURRENT_USER
sKeyName.s = "Control Panel\Desktop\MuiCached"
sValueName.s ="MachinePreferredUIlanguages"

correctly points to the currently active display language.

So i guess we should use that key and only if it doesnt exist, use the others as fallback option. Thats what im going to do now (and probably evaluate it by calling an OS tool and check which language the tool actually uses ;). What a mess :o

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/