From 9cab8ae58a29ecf7387e6865aa170715caeabf04 Mon Sep 17 00:00:00 2001 From: Stefan Parviainen Date: Tue, 30 Dec 2014 19:57:12 +0100 Subject: Language names via intl library. Fixes #773 --- library/intl/src/NumberFormat/NumberFormat.php | 269 +++++++++++++++++++++ .../src/NumberFormat/NumberFormatInterface.php | 185 ++++++++++++++ .../src/NumberFormat/NumberFormatRepository.php | 87 +++++++ .../NumberFormatRepositoryInterface.php | 19 ++ 4 files changed, 560 insertions(+) create mode 100644 library/intl/src/NumberFormat/NumberFormat.php create mode 100644 library/intl/src/NumberFormat/NumberFormatInterface.php create mode 100644 library/intl/src/NumberFormat/NumberFormatRepository.php create mode 100644 library/intl/src/NumberFormat/NumberFormatRepositoryInterface.php (limited to 'library/intl/src/NumberFormat') diff --git a/library/intl/src/NumberFormat/NumberFormat.php b/library/intl/src/NumberFormat/NumberFormat.php new file mode 100644 index 000000000..0c512b7ab --- /dev/null +++ b/library/intl/src/NumberFormat/NumberFormat.php @@ -0,0 +1,269 @@ +locale; + } + + /** + * {@inheritdoc} + */ + public function setLocale($locale) + { + $this->locale = $locale; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getNumberingSystem() + { + return $this->numberingSystem; + } + + /** + * {@inheritdoc} + */ + public function setNumberingSystem($numberingSystem) + { + $this->numberingSystem = $numberingSystem; + } + + /** + * {@inheritdoc} + */ + public function getDecimalSeparator() + { + return $this->decimalSeparator; + } + + /** + * {@inheritdoc} + */ + public function setDecimalSeparator($decimalSeparator) + { + $this->decimalSeparator = $decimalSeparator; + } + + /** + * {@inheritdoc} + */ + public function getGroupingSeparator() + { + return $this->groupingSeparator; + } + + /** + * {@inheritdoc} + */ + public function setGroupingSeparator($groupingSeparator) + { + $this->groupingSeparator = $groupingSeparator; + } + + /** + * {@inheritdoc} + */ + public function getPlusSign() + { + return $this->plusSign; + } + + /** + * {@inheritdoc} + */ + public function setPlusSign($plusSign) + { + $this->plusSign = $plusSign; + } + + /** + * {@inheritdoc} + */ + public function getMinusSign() + { + return $this->minusSign; + } + + /** + * {@inheritdoc} + */ + public function setMinusSign($minusSign) + { + $this->minusSign = $minusSign; + } + + /** + * {@inheritdoc} + */ + public function getPercentSign() + { + return $this->percentSign; + } + + /** + * {@inheritdoc} + */ + public function setPercentSign($percentSign) + { + $this->percentSign = $percentSign; + } + + /** + * {@inheritdoc} + */ + public function getDecimalPattern() + { + return $this->decimalPattern; + } + + /** + * {@inheritdoc} + */ + public function setDecimalPattern($decimalPattern) + { + $this->decimalPattern = $decimalPattern; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getPercentPattern() + { + return $this->percentPattern; + } + + /** + * {@inheritdoc} + */ + public function setPercentPattern($percentPattern) + { + $this->percentPattern = $percentPattern; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getCurrencyPattern() + { + return $this->currencyPattern; + } + + /** + * {@inheritdoc} + */ + public function setCurrencyPattern($currencyPattern) + { + $this->currencyPattern = $currencyPattern; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getAccountingCurrencyPattern() + { + return $this->accountingCurrencyPattern; + } + + /** + * {@inheritdoc} + */ + public function setAccountingCurrencyPattern($accountingCurrencyPattern) + { + $this->accountingCurrencyPattern = $accountingCurrencyPattern; + + return $this; + } +} diff --git a/library/intl/src/NumberFormat/NumberFormatInterface.php b/library/intl/src/NumberFormat/NumberFormatInterface.php new file mode 100644 index 000000000..fa382df70 --- /dev/null +++ b/library/intl/src/NumberFormat/NumberFormatInterface.php @@ -0,0 +1,185 @@ +definitionPath = $definitionPath ? $definitionPath : __DIR__ . '/../../resources/number_format/'; + } + + /** + * {@inheritdoc} + */ + public function get($locale, $fallbackLocale = null) + { + $locale = $this->resolveLocale($locale, $fallbackLocale); + if (!isset($this->definitions[$locale])) { + $filename = $this->definitionPath . $locale . '.json'; + $this->definitions[$locale] = json_decode(file_get_contents($filename), true); + } + + return $this->createNumberFormatFromDefinition($this->definitions[$locale], $locale); + } + + /** + * Creates a number format object from the provided definition. + * + * @param array $definition The number format definition. + * @param string $locale The locale of the number format definition. + * + * @return NumberFormat + */ + protected function createNumberFormatFromDefinition(array $definition, $locale) + { + if (!isset($definition['decimal_separator'])) { + $definition['decimal_separator'] = '.'; + } + if (!isset($definition['grouping_separator'])) { + $definition['grouping_separator'] = ','; + } + if (!isset($definition['plus_sign'])) { + $definition['plus_sign'] = '+'; + } + if (!isset($definition['minus_sign'])) { + $definition['minus_sign'] = '-'; + } + if (!isset($definition['percent_sign'])) { + $definition['percent_sign'] = '%'; + } + + $numberFormat = new NumberFormat(); + $numberFormat->setLocale($locale); + $numberFormat->setNumberingSystem($definition['numbering_system']); + $numberFormat->setDecimalSeparator($definition['decimal_separator']); + $numberFormat->setGroupingSeparator($definition['grouping_separator']); + $numberFormat->setPlusSign($definition['plus_sign']); + $numberFormat->setMinusSign($definition['minus_sign']); + $numberFormat->setPercentSign($definition['percent_sign']); + $numberFormat->setDecimalPattern($definition['decimal_pattern']); + $numberFormat->setPercentPattern($definition['percent_pattern']); + $numberFormat->setCurrencyPattern($definition['currency_pattern']); + $numberFormat->setAccountingCurrencyPattern($definition['accounting_currency_pattern']); + + return $numberFormat; + } +} diff --git a/library/intl/src/NumberFormat/NumberFormatRepositoryInterface.php b/library/intl/src/NumberFormat/NumberFormatRepositoryInterface.php new file mode 100644 index 000000000..ff162b522 --- /dev/null +++ b/library/intl/src/NumberFormat/NumberFormatRepositoryInterface.php @@ -0,0 +1,19 @@ +