1

en_US gives me en for English but it's already in the code

I need US to return en for English or 'DE' to return de for German

echo Locale::getPrimaryLanguage('en_US');
echo Locale::getPrimaryLanguage('US');

Both of the above should output en, but the latter actually outputs us.

RoboTamer
  • 3,474
  • 2
  • 39
  • 43
  • You can use the Unicode CLDR [likely subtags data](http://cldr.unicode.org/index/cldr-spec/language-tag-equivalences#TOC-Likely-subtags) to get the “default” or “likely” language for a specific country, see https://stackoverflow.com/a/58512299/1031606 – ausi Oct 22 '19 at 21:12

2 Answers2

1

getPrimaryLanguage is not an ICU function so I have no idea how it's implemented. In ICU you could call uloc_addLikelySubtags which would expand und_US (You would put und for unknown before the region code US) to en_US or und_DE to de_DE - and then, calling uloc_getLanguage would return en, de, etc.

Your result of us for US is probably due to US being interpreted as a language code. You could try und_US to see what the behavior is.

Steven R. Loomis
  • 4,228
  • 28
  • 39
0

like @Steven R. Loomis said you can use a locale 'und_US' and Locale::getPrimaryLanguage('und_US') will just return 'und'

If using the php intl extension, it can accept a locale of 'und_US' , but it will no format your currencies properly according to the region US only, you need to specify a language.

$locales = array('de_DE', 'en_US', 'ja_JP', 'und_US', 'und_JP', 'und_DE', 'und_FR');
$amount = 1234567.891234567890000;
foreach ($locales as $locale) {
        echo Locale::getPrimaryLanguage($locale).'<br />';
        $formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY); 
        echo $formatter->getTextAttribute(NumberFormatter::CURRENCY_CODE); 
        echo '|' . $formatter->getSymbol(NumberFormatter::CURRENCY_SYMBOL);
        echo '|' . $formatter->getSymbol(NumberFormatter::INTL_CURRENCY_SYMBOL);
        echo ' - ' . $formatter->formatCurrency($amount, $formatter->getTextAttribute(NumberFormatter::CURRENCY_CODE));
        echo '<br /><br />';
}

returns

de EUR|€|EUR - 1.234.567,89 €

en USD|$|USD - $1,234,567.89

ja JPY|¥|JPY - ¥1,234,568

und USD|$|USD - $ 1234567.89

und JPY|¥|JPY - ¥ 1234568

und EUR|€|EUR - € 1234567.89

und EUR|€|EUR - € 1234567.89

notice how even though you are given the correct currency symbol for the region, the format of the number is incorrect.

kzap
  • 1,411
  • 13
  • 20