Page 1 of 1

get locale (language and country)

Posted: Wed Sep 07, 2016 8:45 pm
by Keya

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  Define Buffer$, buflen, bytesread, LC
  LC = #LOCALE_SLANGUAGE
  ;LC = #LOCAL_SLANGUAGE #LOCALE_SINTLSYMBOL ;#LOCALE_SNATIVECTRYNAME ;#LOCALE_SNATIVELANGNAME ;#LOCALE_SISO639LANGNAME
  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 "Result=" + Buffer$
  EndIf
  
CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux  ;(on #PB_OS_MacOS it also works but tends to only return default of "C")
  #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_CTYPE, #Null)  ;If the last paramter is Null the function works as GETlocale
  If lcaddr
    Debug "Result=" + PeekS(lcaddr,-1,#PB_Ascii)
  Else
    Debug "Failed"
  EndIf  
  
CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS   ;thanks to mk-soft. Tends to return the full locale, as opposed to just default of "C"
  ;NSString *language = [[NSLocale currentLocale] localeIdentifier];
  ;NSLog(@"Language: %@", language)  
  CurrentLocale = CocoaMessage(0, 0, "NSLocale currentLocale")
  LocaleIdentifer = CocoaMessage(0, CurrentLocale, "localeIdentifier")
  CocoaMessage(@Language, LocaleIdentifer, "UTF8String")
  Debug "Result=" + PeekS(Language, -1, #PB_UTF8)
  
CompilerEndIf


;// and another way, for Linux and Mac:
;Debug "LANG environment var = " + GetEnvironmentVariable("LANG")
Be aware that the output format of each can be a little different, especially Windows.
Note also that with both Linux and Mac the default locale is simply "C", so it'd probably be wise to add some fallback support for that.

Re: get locale (language and country)

Posted: Wed Sep 07, 2016 11:00 pm
by netmaestro
Works well here on Windows.

Re: get locale (language and country)

Posted: Thu Sep 08, 2016 5:50 am
by wilbert
Keya wrote:It's strange though because when I type "locale" from the OSX console i get the exact same output that i get from Linux - not "C", so i'm still a bit confused...
Is this the same kind of information the NSLocale class offers on OSX or is it different ?

Re: get locale (language and country)

Posted: Thu Sep 08, 2016 10:22 am
by Kwai chang caine
Result wrote:Result=Français (France)
Works great here W7 x86 v5.40
Thanks for sharing 8)

Re: get locale (language and country)

Posted: Thu Sep 08, 2016 10:04 pm
by falsam
Thanks Keya.

Re: get locale (language and country)

Posted: Sat Sep 10, 2016 10:43 am
by mk-soft
Have a very long time for the first time himself to use "CocoaMessage".
but Come slowly because behind ...
;NSString *language = [[NSLocale currentLocale] localeIdentifier];
;NSLog(@"Language: %@", language) ;

CurrentLocale = CocoaMessage(0, 0, "NSLocale currentLocale")
LocaleIdentifer = CocoaMessage(0, CurrentLocale, "localeIdentifier")
CocoaMessage(@Language, LocaleIdentifer, "UTF8String")
Debug PeekS(Language, -1, #PB_UTF8)

Re: get locale (language and country)

Posted: Sat Sep 10, 2016 11:35 am
by Keya
mk-soft, yes!! on my Mac El Capitan your CocoaMessage code does correctly give me a valid country and language (as opposed to default "C"), just like 'locale' from terminal does. Great work thankyou, ive updated the OP :)

Re: get locale (language and country)

Posted: Sat Sep 10, 2016 2:48 pm
by wilbert
mk-soft wrote:Have a very long time for the first time himself to use "CocoaMessage".
That's great !
Always good to learn how to use CocoaMessage :D

The way you did, is a shortcut for

Code: Select all

CurrentLocale = CocoaMessage(0, 0, "NSLocale currentLocale")
LocaleIdentifer = CocoaMessage(0, CurrentLocale, "objectForKey:$", @"kCFLocaleIdentifierKey")
CocoaMessage(@Language, LocaleIdentifer, "UTF8String")
Debug PeekS(Language, -1, #PB_UTF8)
Your code is easier but objectForKey gives you the option to ask for more different things like this.

Code: Select all

CurrentLocale = CocoaMessage(0, 0, "NSLocale currentLocale")

LanguageCode = CocoaMessage(0, CurrentLocale, "objectForKey:$", @"kCFLocaleLanguageCodeKey")
CocoaMessage(@Language, LanguageCode, "UTF8String")
Debug PeekS(Language, -1, #PB_UTF8)

CountryCode = CocoaMessage(0, CurrentLocale, "objectForKey:$", @"kCFLocaleCountryCodeKey")
CocoaMessage(@Country, CountryCode, "UTF8String")
Debug PeekS(Country, -1, #PB_UTF8)

DecimalSeparator = CocoaMessage(0, CurrentLocale, "objectForKey:$", @"kCFLocaleDecimalSeparatorKey")
CocoaMessage(@Separator, DecimalSeparator, "UTF8String")
Debug PeekS(Separator, -1, #PB_UTF8)

CurrencySymbol = CocoaMessage(0, CurrentLocale, "objectForKey:$", @"kCFLocaleCurrencySymbolKey")
CocoaMessage(@Currency, CurrencySymbol, "UTF8String")
Debug PeekS(Currency, -1, #PB_UTF8)
There's quite a few keys you can use to get information.

Re: get locale (language and country)

Posted: Sat Sep 10, 2016 3:11 pm
by Shardik
Keya wrote:mk-soft, yes!! on my Mac El Capitan your CocoaMessage code does correctly give me a valid country and language (as opposed to default "C"), just like 'locale' from terminal does.
Danilo posted already more than 3 years ago in [PB Cocoa] Methods, Tips & Tricks for MacOS how to get
- default language
- user locale
- user language
- user language identifier

So before reinventing the wheel you should at first always take a look into Wilbert's excellent list of Cocoa tips & tricks... :wink:

Re: get locale (language and country)

Posted: Sat Sep 10, 2016 4:10 pm
by Keya
Shardik many thanks for pointing that out! im not sure how Danilo's managed to escape my initial search :(
And it appears to be different to the other versions posted here! i dont know which is best though lol

Code: Select all

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
wilbert wrote:Your code is easier but objectForKey gives you the option to ask for more different things
very cool! http://opensource.apple.com/source/CF/C ... caleKeys.c