Sign in to follow this  
Followers 0

_GetOSLanguage() - Retrieve the language of the OS.

18 posts in this topic

Posted (edited)

I currently have a Language UDF which I need to tidy up and one day will release on the AutoIt Forums (I just need to add the function headers.) But in the interim I thought I would upload _GetOSLanguage() which retrieves the language of the OS. Now for most on here this code is really really simple, but I've noticed some still find Arrays a little hard to grasp and despite a brilliant wiki page, a 'real world' example can help with understanding this data type a little bit more.

It's also a different take on the Help file example which uses Select...EndSelect.

Function:

#include <MsgBoxConstants.au3>
#include <StringConstants.au3>

MsgBox($MB_SYSTEMMODAL, '', 'The default language translation will be ' & _GetOSLanguage() & '.' & @CRLF)

; #FUNCTION# ====================================================================================================================
; Name ..........: _GetOSLanguage
; Description ...: Retrieves the language of the OS, this supports 19 of the most popular languages.
; Syntax ........: _GetOSLanguage()
; Parameters ....: None
; Return values .: OS language
; Author ........: guinness
; Link ..........: http://www.autoitscript.com/forum/topic/131832-getoslanguage-retrieve-the-language-of-the-os/
; Example .......: Yes
; ===============================================================================================================================
Func _GetOSLanguage()
    Local Const $sID = StringRight(@OSLang, StringLen('09'))
    Return StringRegExp('|06Chinese|' & _
            '06Danish|13Dutch|' & _
            '25Estonian|' & _
            '0bFinnish|0cFrench|' & _
            '07German|08Greek|' & _
            '0eHungarian|' & _
            '10Italian|' & _
            '11Japanese|' & _
            '14Norwegian|' & _
            '15Polish|16Portuguese|' & _
            '18Romanian|19Russian|' & _
            '1aSerbian|0aSpanish|1dSwedish|' & _
            $sID & 'English|', _
            '\|' & $sID & '([^|]+)\|', $STR_REGEXPARRAYGLOBALMATCH)[0]
EndFunc   ;==>_GetOSLanguage
Edited by guinness

Share this post


Link to post
Share on other sites



Posted (edited)

@Guinness

Thanks for posting.

I've created a similar function, never posted and don't know why.

I use @MUILang instead of @OSLang (Only for Vista and Above)

There is a simple reason for this, @OSLang shows the Language of the OS but not the displayed language. So when you buy for example a HP computer with a dutch OS, you will get an English OS (@OSLang) with a dutch Language Pack (@MUILang) preinstalled.

When i was working on something similar as your UDF i came across this problem, that why is made a request for something as @MUILang http://www.autoitscript.com/trac/autoit/ticket/869

Best regards,

Emiel

Edited by Emiel Wieldraaijer

Share this post


Link to post
Share on other sites

Posted

On win 7, @MUILang returns 0409 (English (united States)) when in fact everything on my pc is English (United Kingdom)

Share this post


Link to post
Share on other sites

Posted (edited)

On win 7, @MUILang returns 0409 (English (united States)) when in fact everything on my pc is English (United Kingdom)

Very strang, maybe you should report it as a bug.

I don't know for sure but @MUILang retrieves the information from your registry.

Edited by Emiel Wieldraaijer

Share this post


Link to post
Share on other sites

Posted

When installing windows, I am always very carefull to make sure of those points, and control panel region and language show UK for everything in it.

Share this post


Link to post
Share on other sites

Posted

I have no english (UK) OS so i cannot check

Share this post


Link to post
Share on other sites

Posted (edited)

It may be easier.

#Include <WinAPIEx.au3>

ConsoleWrite(_WinAPI_GetLocaleInfo(Dec(@OSLang), $LOCALE_SLANGUAGE) & @CR)
ConsoleWrite(_WinAPI_GetLocaleInfo(Dec(@OSLang), $LOCALE_SENGLANGUAGE) & @CR)
ConsoleWrite(_WinAPI_GetLocaleInfo(Dec(@OSLang), $LOCALE_SABBREVLANGNAME) & @CR)
ConsoleWrite(_WinAPI_GetLocaleInfo(Dec(@OSLang), $LOCALE_SNATIVELANGNAME) & @CR)
Edited by Yashied

Share this post


Link to post
Share on other sites

Posted

I still get this with Your code Yashied.

English (United States)

English

ENU

English

I cannot figure out why.

Share this post


Link to post
Share on other sites

Posted

I still get this with Your code Yashied.

I cannot figure out why.

Your language isn't completly English (UK) but 0409 English (US)

English - United States 0409 1033

English - United Kingdom 0809 2057

http://msdn.microsoft.com/en-us/goglobal/bb964664

Share this post


Link to post
Share on other sites

Posted (edited)

Thanks, but that list is also in the autoit3 help file, I know what it should be, it just is not right.

I cannot find anywhere on my PC that says my language,location, region or indeed keyboard, is English US :mellow:

Its rather puzzling.

Edited by JohnOne

Share this post


Link to post
Share on other sites

Posted

_WinAPI_GetLocaleInfo()

Another function I've missed from WinAPIEx!

Share this post


Link to post
Share on other sites

Posted

Updated the original post with an idea by GEOSoft. Thanks.

Share this post


Link to post
Share on other sites

Posted (edited)

I've updated the function by using the available (though as of yet undocumented) constant variables.

Edit: Now I exit the loop instead of using a return command, Single Entry-Single Exit point.

Edited by guinness

Share this post


Link to post
Share on other sites

Posted (edited)

Keep it simple :thumbsup:

; for language ids see http://msdn.microsoft.com/goglobal/bb964664
MsgBox(Default, Default, 'The default language translation of this PC is ' & __GetOSLanguage())
MsgBox(Default, Default, 'The default English translation is ' & __GetOSLanguage(9))
MsgBox(Default, Default, 'The translation for London is ' & __GetOSLanguage(809))
MsgBox(Default, Default, 'The translation for Montego Bay is ' & __GetOSLanguage(2009))
Func __GetOSLanguage($Lang = "")
Local Const $GetLanguage = 114
Local $a = DllCall('kernel32.dll', 'int', 'GetLocaleInfoW', 'ulong', Dec($Lang), 'dword', $GetLanguage, 'wstr', '', 'int', 2048)
Return $a[3]
EndFunc ;==>__GetOSLanguage
Edited by Exit

Share this post


Link to post
Share on other sites

Posted

Thanks.

Share this post


Link to post
Share on other sites

Posted (edited)

Updated the original function using a regular expression.

Edited by guinness

Share this post


Link to post
Share on other sites

Posted (edited)

I didn't want to open another thread for this, since it's just a function that wraps some _WinAPI_ UDF functions together and does almost the same thing yours does.

So here you go:

; #FUNCTION# ====================================================================================================================
; Name ..........: _UILanguageGet
; Description ...: Retrieves the language of either the system or the current user.
; Syntax ........: _LanguageGet([$iSystem = 0[, $iType = 0]])
; Parameters ....: $iSystem             - [optional] Specifies which language will be referred to.
;                  |  0  - Current users UI language
;                  |  1  - Default system UI language
;                  $iType               - [optional] An integer value. Default is 0.
;                  |  0  - English language string
;                  |  1  - Native language string
; Return values .: The desired language string.
; ===============================================================================================================================
Func _UILanguageGet($iSystem = 0, $iType = 0)
    Local $iID

    If $iSystem = 1 Then
        $iID = _WinAPI_GetSystemDefaultUILanguage()
    Else
        $iID = _WinAPI_GetUserDefaultUILanguage()
    EndIf
    If $iType = 1 Then
        $iType = $LOCALE_SNATIVELANGNAME
    Else
        $iType = $LOCALE_SENGLANGUAGE
    EndIf

    Return _WinAPI_GetLocaleInfo($iID, $iType)
EndFunc   ;==>_LanguageGet

You could easily add more functionaltiy by including more constants like $LOCALE_SABBREVLANGNAME (abbreviated language) or $LOCALE_SENGCOUNTRY (english country name). These constants are defined in APILocaleConstants.au3.

In case you want to use this for multilingual AutoIt GUIs:

To exclude invalid languages from the output and replace them with a default language, you would still need to add a string or array with valid Hex($iID, 2) languages and StringRegExp or _ArraySearch them like guinness did.

Edited by Synix

Share this post


Link to post
Share on other sites

Posted

Thanks.

1 person likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.