Get the language Operating system

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Mesa
Enthusiast
Enthusiast
Posts: 433
Joined: Fri Feb 24, 2012 10:19 am

Get the language Operating system

Post by Mesa »

Sometimes is usefull to know the OS language.

Code: Select all

; keya http://www.purebasic.fr/english/viewtopic.php?f=12&t=66552
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
	Define Buffer$, buflen, bytesread, LC
	LC = #LOCALE_SLANGUAGE
	;Note - some apps would be better using #LOCALE_SYSTEM_DEFAULT instead of #LOCALE_USER_DEFAULT
	buflen = GetLocaleInfo_(#LOCALE_USER_DEFAULT, LC, @Buffer$, 0)    ;MS recommends using GetLocaleInfoEx_() if only supporting Vista and later
	Buffer$ = Space(buflen)
	bytesread = GetLocaleInfo_(#LOCALE_USER_DEFAULT, LC, @Buffer$, buflen)
	If bytesread = 0
		Debug "Failed" ;call GetLastError()
	Else
		Debug Buffer$ ; Français (France)
	EndIf
	
CompilerElse ;#PB_OS_Linux, (and #PB_OS_MacOS ?)
	#LC_CTYPE = 0
	#LC_NUMERIC = 1
	#LC_TIME = 2
	#LC_COLLATE = 3
	#LC_MONETARY = 4
	#LC_MESSAGES = 5
	#LC_ALL = 6
	#LC_PAPER = 7
	#LC_NAME = 8
	#LC_ADDRESS = 9
	#LC_TELEPHONE = 10
	#LC_MEASUREMENT = 11
	#LC_IDENTIFICATION = 12
	
	lcaddr.i = setlocale_(#LC_ALL, #Null)  ;If last param is Null the function reads instead of writes
	If lcaddr
		Debug "Result=" + PeekS(lcaddr,-1,#PB_Ascii)
	Else
		Debug "Failed"
	EndIf  

	;// Or alternatively ...
	Debug "LANG environment var = " + GetEnvironmentVariable("LANG")
	
	;Macos
	CurrentLocale = CocoaMessage(0, 0, "NSLocale currentLocale")
LocaleIdentifer = CocoaMessage(0, CurrentLocale, "objectForKey:$", @"kCFLocaleIdentifierKey")
CocoaMessage(@Language, LocaleIdentifer, "UTF8String")
Debug PeekS(Language, -1, #PB_UTF8)
	
	
	
CompilerEndIf

Procedure.s GetUserLocale()  ;by Danilo - http://www.purebasic.fr/english/viewtopic.php?f=19&t=50795&start=137
    Protected result.s, NSUserDefaults_defs, NSString_locale
    NSUserDefaults_defs = CocoaMessage(0,0,"NSUserDefaults standardUserDefaults")
    If NSUserDefaults_defs
        NSString_locale  = CocoaMessage(0,NSUserDefaults_defs,"objectForKey:$",@"AppleLocale")
        If NSString_locale
            result = PeekS(CocoaMessage(0, NSString_locale, "UTF8String"), -1, #PB_UTF8)
            autorelease(NSString_locale)
        EndIf
        autorelease(NSUserDefaults_defs)
    EndIf
    ProcedureReturn result
EndProcedure

Mesa.