Revision #2 has been created by CeBe on Aug 9, 2018, 6:51:30 PM with the memo:
add information about currency symbol
« previous (#1) next (#3) »
Changes
Title
unchanged
Getting information from the current locale
Category
unchanged
How-tos
Yii version
unchanged
2.0
Tags
changed
i18n,locale,language,formatting rules
Content
changed
Yii 2.0 comes with a [formatter component](https://www.yiiframework.com/doc/api/2.0/yii-i18n-formatter) to format dates, numbers and other values for international users according to their locale. This is very useful for displaying data. When working with incoming data or when using enhanced input methods like the [MaskedInput widget](https://www.yiiframework.com/doc/api/2.0/yii-widgets-maskedinput) you sometimes need to access part the symbols used by the formatter to generate a pattern.
The Yii formatter does not have methods to access this data, it relies on the [PHP intl extension](https://secure.php.net/manual/en/book.intl.php) for formatting. If you want to access the formatting symbols you need to work with intl directly.
## Getting decimal and thousand separator
```php
$locale = 'de_DE';
$formatter = new \NumberFormatter($locale,\NumberFormatter::DECIMAL);
$decimalSeparator = $formatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
$thousandSeparator = $formatter->getSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL);
```
There are more symbols than shown above, for the full list of constants see <https://secure.php.net/manual/en/class.numberformatter.php#intl.numberformatter-constants.unumberformatsymbol>.
# Getting a currency symbol
Getting the currency symbol is a bit more complicated if you do not want the symbol of the default currency of a locale, but another currency:
```php
$locale = 'de_DE'; // get the currency symbol of Germanys default currency EUR = "€"
$locale = 'de_DE@currency=USD'; // get the currency symbol of USD in German locale = "$"
$formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
$currencySymbol = $formatter->getSymbol(\NumberFormatter::CURRENCY_SYMBOL);
```
Since Yii 2.0.14 Yii has this method builtin in the [Locale](https://www.yiiframework.com/doc/api/2.0/yii-i18n-locale#getCurrencySymbol()-detail) class.