Locale.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\i18n;
  8. use Yii;
  9. use yii\base\Component;
  10. use yii\base\InvalidConfigException;
  11. /**
  12. * Locale provides various locale information via convenient methods.
  13. *
  14. * The class requires [PHP intl extension](https://secure.php.net/manual/en/book.intl.php) to be installed.
  15. *
  16. * @property string $currencySymbol This property is read-only.
  17. *
  18. * @since 2.0.14
  19. */
  20. class Locale extends Component
  21. {
  22. /**
  23. * @var string the locale ID.
  24. * If not set, [[\yii\base\Application::language]] will be used.
  25. */
  26. public $locale;
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function init()
  31. {
  32. if (!extension_loaded('intl')) {
  33. throw new InvalidConfigException('Locale component requires PHP intl extension to be installed.');
  34. }
  35. if ($this->locale === null) {
  36. $this->locale = Yii::$app->language;
  37. }
  38. }
  39. /**
  40. * Returns a currency symbol
  41. *
  42. * @param string $currencyCode the 3-letter ISO 4217 currency code to get symbol for. If null,
  43. * method will attempt using currency code from [[locale]].
  44. * @return string
  45. */
  46. public function getCurrencySymbol($currencyCode = null)
  47. {
  48. $locale = $this->locale;
  49. if ($currencyCode !== null) {
  50. $locale .= '@currency=' . $currencyCode;
  51. }
  52. $formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
  53. return $formatter->getSymbol(\NumberFormatter::CURRENCY_SYMBOL);
  54. }
  55. }