/** * {@inheritdoc} */ public function formatAmount(CurrencyInterface $currency, $amount, $language_type = LanguageInterface::TYPE_CONTENT) { $currency_locale = $this->localeDelegator->resolveCurrencyLocale(); $decimal_position = strpos($amount, '.'); $number_of_decimals = $decimal_position !== FALSE ? strlen(substr($amount, $decimal_position + 1)) : 0; $formatter = new \NumberFormatter($currency_locale->getLocale(), \NumberFormatter::PATTERN_DECIMAL, $currency_locale->getPattern()); $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $number_of_decimals); $formatter->setSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL, $currency_locale->getDecimalSeparator()); $formatter->setSymbol(\NumberFormatter::MONETARY_SEPARATOR_SYMBOL, $currency_locale->getDecimalSeparator()); $formatter->setSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL, $currency_locale->getGroupingSeparator()); $formatter->setSymbol(\NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL, $currency_locale->getGroupingSeparator()); $formatter->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, $currency->getSign()); $formatter->setSymbol(\NumberFormatter::INTL_CURRENCY_SYMBOL, $currency->getCurrencyCode()); return $formatted = $formatter->format($amount); }
/** * @covers ::resolveCurrencyLocale * * @expectedException \RuntimeException */ function testResolveCurrencyLocaleMissingFallback() { $this->prepareLanguageManager(); $config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock(); $config->expects($this->any())->method('get')->with('country.default')->willReturn(NULL); $this->configFactory->expects($this->once())->method('get')->with('system.data')->willReturn($config); $this->currencyLocaleStorage->expects($this->any())->method('load')->with(LocaleResolverInterface::DEFAULT_LOCALE)->willReturn(NULL); // Test loading the fallback locale. $this->sut->resolveCurrencyLocale(); }