Jump to content
John Kouraklis

How to get the Currency Symbol in multiple platforms

Recommended Posts

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

 

  • Like 1

Share this post


Link to post

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

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

  • Like 1

Share this post


Link to post

I guess FormatSettings.CurrencyString is not suitable, because by default returns $ for US locale?

Share this post


Link to post
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
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

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

×