AutoHotkey Community

It is currently May 26th, 2012, 4:42 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: March 31st, 2009, 8:45 am 
Offline

Joined: February 25th, 2008, 1:24 pm
Posts: 22
If you have a script which should work differently according to the selected language then it is needed to know about which are installed.

In the registry there is a list about all installed languages and with the DLL function "GetLocaleInfoA" it is quite easy to get all related informations.

The following code samples are adaptations from a Visual Basic script fount at http://vbnet.mvps.org/index.html?code/l ... ocales.htm.
A lot of thanks to them for helping me in solving my problem

Code:

; This routine enumerates all keyboards which are installed on the system
; and returns the ID, the long and abbreviated name of the language
loop HKEY_CURRENT_USER, Keyboard Layout\Preload,0,0
{
RegRead Locale

locale := "0x" locale

msgbox locale %locale%

VarSetCapacity(name, 128, 32) ; Set capacity new to clean up the buffer


Lang := DllCall("GetLocaleInfoA"
           ,Uint,  Locale
         ,Uint,  2 ; &H2  'localized name of language
         ,Str,  name
         ,UInt,  128)

msgbox long name %name%

Lang := DllCall("GetLocaleInfoA"
           ,Uint,  Locale
         ,Uint,  3 ; &H2  'Abbreviates  name of language
         ,Str,  name
         ,UInt,  128)

msgbox abbr name %name%
}


To get the full numbers of the available languages on a system a different way is necessary. In this case we have to use the Callback dll function "EnumSystemLocalesA"
Code:
; This routine enumerates all availabel languages on a system
; and stores them in a drop down list

 Gui, Add,  DropDownList, x10 y10 w200  vLanguages

 EnumAddress := RegisterCallback("EnumerateLanguagesProc", Fast)   
   
   Long := DllCall("EnumSystemLocalesA"
           ,Uint,  EnumAddress
           ,Uint,  1)         
         
 

 EnumerateLanguagesProc(lang)
 {
   lang := lang + 4 ; The first four signs are unimportant for our case
   
   locale := ""
   
   loop 4  ; I think there should be a better way to do this, but it works
   {
     x := *lang
    x := chr(x)
    locale := locale x
    lang := lang + 1
   }
   
   locale := "0x" locale

   VarSetCapacity(name, 128, 32) ; Set capacity new to clean up the buffer


   Lang := DllCall("GetLocaleInfoA"
           ,Uint,  Locale
         ,Uint,  2 ; &H2  'localized name of language
         ,Str,  name
         ,UInt,  128)

   locale := locale "    " name      

    Lang := DllCall("GetLocaleInfoA"
           ,Uint,  Locale
         ,Uint,  3 ; &H2  'Abbreviates  name of language
         ,Str,  name
         ,UInt,  128)
   
   locale := locale "    " name
   
   GUIControl,,Languages, %locale%   

   return True
 }
 
 
 q:: gui,show


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 31st, 2009, 10:13 am 
Haven't fully digested your scripts content, nevertheless - thank you for sharing them. :D

Btw, is it possible to identify the current default keyboard/language this way (looks I might have overseen it ?) ?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 31st, 2009, 10:34 am 
Offline

Joined: February 25th, 2008, 1:24 pm
Posts: 22
The current / default language code is already stored in the inbuilt variable
"A_Language" So it is not necessary to use this code. But with the followig code you can get the name of it.

Code:
VarSetCapacity(name, 128, 32) ; Set capacity new to clean up the buffer

Lang := DllCall("GetLocaleInfoA"
         ,Uint,  %A_Language%
         ,Uint,  2 ; &H2  'localized name of language
         ,Str,  name
         ,UInt,  128)

msgbox long name %name%


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 31st, 2009, 10:56 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
%A_Language% looks like an invalid double-deref. I suppose it works because 0 means "the current language", and %A_Language% ends up retrieving an empty value (equivalent to 0) from an implicit numerically-named variable.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 31st, 2009, 12:32 pm 
Offline

Joined: February 25th, 2008, 1:24 pm
Posts: 22
It needs to be %A_Language%, otherwise the function does not return any useful information.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 31st, 2009, 1:23 pm 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
Run the following, change %A_Language% to 0 in your script, run it, read my previous post one last time, then tell me again that it needs to be %A_Language%. ;)
Code:
var := ( %A_Language% )
ListVars
Pause


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 31st, 2009, 11:55 pm 
Offline
User avatar

Joined: September 8th, 2008, 12:26 am
Posts: 1048
Location: Ploieşti, RO
Thank you for the scripts.

Two issues while I'm here:
1. The loop in first script should recurse subkeys. On my 98SE system, Preload has 4 subkeys called 1, 2, 3 and 4 respectively. Following line works:
loop HKEY_CURRENT_USER, Keyboard Layout\Preload,0,1

2. What about IMEs, how to retrieve additional languages/layouts? I have the Japanese IME installed (for Office XP) and JP becomes available when opening an app from the Office suite. Strangely enough, JP is also available when Character Map is open and focused. But it's not listed anywhere by either of the two scripts above. Food for thought...

Slightly modified script #1:
Code:

; This routine enumerates all keyboards which are installed on the system
; and returns the ID, the long and abbreviated name of the language
loop HKEY_CURRENT_USER, Keyboard Layout\Preload,0,1
{
RegRead Locale

locale := "0x" locale

VarSetCapacity(name, 128, 32) ; Set capacity new to clean up the buffer

Lang := DllCall("GetLocaleInfoA"
   ,Uint,   Locale
   ,Uint,   2   ; &H2  'localized name of language
   ,Str,      name
   ,UInt,   128)

lname := name

Lang := DllCall("GetLocaleInfoA"
   ,Uint,   Locale
   ,Uint,   3   ; &H2  'Abbreviates  name of language
   ,Str,      name
   ,UInt,   128)

msgbox Locale code:`t%locale%`nLong name:`t%lname%`nShort name:`t%name%
}


[Later edit]
Here's second script modified for a more elegant string retrieval and automatic display of the default locale in the dropdown; this was only tested in Win98SE so please check if it works correctly on your system(s):
Code:
; This routine enumerates all available languages on a system
; and stores them in a drop down list

 Gui, Add,  DropDownList, x10 y10 w250 r10  Sort vLanguages

 EnumAddress := RegisterCallback("EnumerateLanguagesProc", Fast)
 RegRead, def, HKEY_CURRENT_USER, Control Panel\International, Locale
 StringRight, def, def, 4   
 Long := DllCall("EnumSystemLocalesA"
   ,Uint,   EnumAddress
   ,Uint,   1)
 Gui, Show
 return

 EnumerateLanguagesProc(lang)
 {
   global def
   VarSetCapacity(loc, 4, 0)
   DllCall("crtdll\strcpy", Str, loc, UInt, lang+4, cdecl)
   locale := "0x" loc

   VarSetCapacity(name, 128, 32) ; Set capacity new to clean up the buffer

   Lang := DllCall("GetLocaleInfoA"
   ,Uint,   Locale
   ,Uint,   2   ; &H2  'localized name of language
   ,Str,      name
   ,UInt,   128)

   locale := locale "    " name

    Lang := DllCall("GetLocaleInfoA"
   ,Uint,   Locale
   ,Uint,   3   ; &H2  'Abbreviated  name of language
   ,Str,      name
   ,UInt,   128)
   
   setdef := (def=loc) ? "||" : ""
   locale := locale "    " name setdef
   GUIControl,,Languages, %locale%
   return True
 }

 GuiClose:
 ExitApp

_________________
AHK tools by Drugwash (AHK 1.0.48.05 and Win98SE)


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Rajat and 11 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group