John Kouraklis 94 Posted June 19, 2019 Hi, I want to be able to retrieve the currency code from the local settings. I've got a list with currency codes and countries, etc. For example, if the system is in the US, I would like to get the USD code, etc. The IFMXLocaleService.GetCurrentLangID returns a generic 'en' from which I am not able to retrieve the currency symbol. Anyone any ideas? Thanks a lot 1 Share this post Link to post
David Heffernan 2345 Posted June 20, 2019 Build the ISO 4217 table into your code and lookup the currency code from the country code. Share this post Link to post
John Kouraklis 94 Posted June 20, 2019 @David Heffernan I've got that but how do I get the country code in all platforms? Share this post Link to post
Lars Fosdal 1792 Posted June 20, 2019 https://restcountries.eu offers various free REST APIs for retrieving lists of country-specific info. Pretty nice. AFAIK, there are no cross-platform libs to get country-specific info. Ugly workaround for your apps: Let your user specify the locale. Alternatively, implement for each platform. Windows https://docs.microsoft.com/en-us/windows/desktop/api/winnls/nf-winnls-getlocaleinfoa Android https://developer.android.com/reference/java/util/Locale iOS https://developer.apple.com/documentation/foundation/nslocale/1643060-countrycode Linux No std way as far as I know. Share this post Link to post
Alexander Elagin 143 Posted June 20, 2019 1 hour ago, Lars Fosdal said: Linux No std way as far as I know. Function localeconv() returns a structure containing the locale information, including the currency symbol. http://man7.org/linux/man-pages/man3/localeconv.3.html Share this post Link to post
John Kouraklis 94 Posted June 20, 2019 After fiddling with different stuff, I went with the following: 1. use the local ip to retrieve the country code using ipstack,.com 2. use the country code to get the currency from restcountries.eu (thanks Lars :-)) Thanks for the help everyone 1 Share this post Link to post
Virgo 18 Posted June 21, 2019 I guess FormatSettings.CurrencyString is not suitable, because by default returns $ for US locale? Share this post Link to post
Cristian Peța 103 Posted June 21, 2019 23 hours ago, John Kouraklis said: @David Heffernan I've got that but how do I get the country code in all platforms? I've made it some years ago. Not the best solution but it works for what I need. And not Linux. function GetOSLangID: String; {$IFDEF MACOS} var Languages: NSArray; begin Languages := TNSLocale.OCClass.preferredLanguages; Result := String(TNSString.Wrap(Languages.objectAtIndex(0)).UTF8String); Result := UpperCase(Result.Substring(0, 2));//only first two chars end; {$ENDIF} {$IFDEF ANDROID} var LocServ: IFMXLocaleService; begin if TPlatformServices.Current.SupportsPlatformService(IFMXLocaleService, IInterface(LocServ)) then Result := LocServ.GetCurrentLangID else Result := 'EN'; Result := UpperCase(Result.Substring(0, 2));//only first two chars end; {$ENDIF} {$IFDEF MSWINDOWS} var buffer: MarshaledString; UserLCID: LCID; BufLen: Integer; begin // defaults UserLCID := GetUserDefaultLCID; BufLen := GetLocaleInfo(UserLCID, LOCALE_SISO639LANGNAME, nil, 0); buffer := StrAlloc(BufLen); if GetLocaleInfo(UserLCID, LOCALE_SISO639LANGNAME, buffer, BufLen) <> 0 then Result := buffer else Result := 'EN'; StrDispose(buffer); Result := UpperCase(Result.Substring(0, 2));//only first two chars end; {$ENDIF} Share this post Link to post
John Kouraklis 94 Posted June 21, 2019 5 hours ago, Virgo said: I guess FormatSettings.CurrencyString is not suitable, because by default returns $ for US locale? No, I want to get the currency code not the character, USD vs $ Share this post Link to post
John Kouraklis 94 Posted June 21, 2019 @Cristian Peța Does this retrieve the country code? It looks like it get the language code Share this post Link to post
Cristian Peța 103 Posted June 21, 2019 Yes. The language selected by the user... Share this post Link to post