get locale (language and country)

Share your advanced PureBasic knowledge/code with the community.
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

get locale (language and country)

Post 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.
Last edited by Keya on Sat Sep 10, 2016 11:48 am, edited 11 times in total.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: get locale (language and country)

Post by netmaestro »

Works well here on Windows.
BERESHEIT
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: get locale (language and country)

Post 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 ?
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: get locale (language and country)

Post by Kwai chang caine »

Result wrote:Result=Français (France)
Works great here W7 x86 v5.40
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
User avatar
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: get locale (language and country)

Post by falsam »

Thanks Keya.

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
User avatar
mk-soft
Always Here
Always Here
Posts: 6209
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: get locale (language and country)

Post 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)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: get locale (language and country)

Post 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 :)
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: get locale (language and country)

Post 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.
Last edited by wilbert on Sat Sep 10, 2016 3:45 pm, edited 2 times in total.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: get locale (language and country)

Post 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:
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: get locale (language and country)

Post 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
Post Reply