Detecting the active display language in windows 7 upwards

Windows specific forum
Bitblazer
Enthusiast
Enthusiast
Posts: 732
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

Detecting the active display language in windows 7 upwards

Post by Bitblazer »

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!
webpage - discord chat links -> purebasic GPT4All
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4635
Joined: Sun Apr 12, 2009 6:27 am

Re: Detecting the active display language in windows 7 upwar

Post by RASHAD »

Tested with Windows 10

Code: Select all

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: Select all

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
Egypt my love
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 617
Joined: Mon May 09, 2011 9:36 am

Re: Detecting the active display language in windows 7 upwar

Post by VB6_to_PBx »

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/window ... uage-packs

https://docs.microsoft.com/en-us/window ... or-windows
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
Bitblazer
Enthusiast
Enthusiast
Posts: 732
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

Re: Detecting the active display language in windows 7 upwar

Post by Bitblazer »

Thanks Rashad, but thats always 0407 (keyboard language name - just like MSI win32_operatingsystem\locale). What i need is the display language setting instead.
webpage - discord chat links -> purebasic GPT4All
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4635
Joined: Sun Apr 12, 2009 6:27 am

Re: Detecting the active display language in windows 7 upwar

Post by RASHAD »

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

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

Code: Select all

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
Egypt my love
Bitblazer
Enthusiast
Enthusiast
Posts: 732
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

Re: Detecting the active display language in windows 7 upwar

Post by Bitblazer »

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
Last edited by Bitblazer on Mon Apr 15, 2019 9:11 pm, edited 1 time in total.
webpage - discord chat links -> purebasic GPT4All
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4635
Joined: Sun Apr 12, 2009 6:27 am

Re: Detecting the active display language in windows 7 upwar

Post by RASHAD »

When you start your OS
What is the Active display language ?
Egypt my love
Bitblazer
Enthusiast
Enthusiast
Posts: 732
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

Re: Detecting the active display language in windows 7 upwar

Post by Bitblazer »

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

Code: Select all

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
webpage - discord chat links -> purebasic GPT4All
kvitaliy
Enthusiast
Enthusiast
Posts: 162
Joined: Mon May 10, 2010 4:02 pm

Re: Detecting the active display language in windows 7 upwar

Post by kvitaliy »

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

Code: Select all

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
Bitblazer
Enthusiast
Enthusiast
Posts: 732
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

Re: Detecting the active display language in windows 7 upwar

Post by Bitblazer »

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

Code: Select all

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: Select all

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

Code: Select all

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
webpage - discord chat links -> purebasic GPT4All
Post Reply