2

PHP has an intl module which allows for some cool things - like getting the currency symbol when using the NumberFormatter class. But this is making a massive assumption - that you already have the currency code of the currency you want to get the symbol for.

How do I get the currency code for any specific locale?

For example: en_gb => gbp, en_us => usd, es_es => eur

I am doing this so I can use the currencyFormat view helper in ZendFramework 2:

$locale = 'en_gb'; //from get parameter
$symbol = 'GBP'; //How do get this?

$this->serviceManager
         ->get('viewhelpermanager')
         ->get('currencyFormat')
         ->setLocale($locale)
         ->setCurrencyCode($symbol);
Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129
  • 1
    You would need a map of locale codes to currency codes. [first search result](https://gist.github.com/HarishChaudhari/4680482) – James Mar 08 '18 at 17:03
  • 1
    I think you need the ISO 4217 code in zend? See here: https://stackoverflow.com/questions/4299099/get-currency-iso-4217-code-based-on-locale – avy Mar 08 '18 at 17:16

1 Answers1

1

Based on the answer from Get currency ISO 4217 code based on locale (thanks @avy for link in comment),

$locale = 'en_gb'; //from $_GET parameter

//get symbol for this locale
$symbol = \NumberFormatter::create(
    $locale,
    \NumberFormatter::CURRENCY
)->getTextAttribute(\NumberFormatter::CURRENCY_CODE);

//Add locale to currency formatter so we get correct symbol and formatting
$this->serviceManager
        ->get('viewhelpermanager')
        ->get('currencyFormat')
        ->setLocale($locale)
        ->setCurrencyCode($symbol);
Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129