Basic.php in Currency 8.3
File
src/Plugin/Currency/AmountFormatter/Basic.php
View source
<?php
namespace Drupal\currency\Plugin\Currency\AmountFormatter;
use Commercie\Currency\CurrencyInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\currency\LocaleResolverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Basic extends PluginBase implements AmountFormatterInterface, ContainerFactoryPluginInterface {
protected $localeDelegator;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, TranslationInterface $translation_manager, LocaleResolverInterface $locale_delegator) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->localeDelegator = $locale_delegator;
$this->stringTranslation = $translation_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('string_translation'), $container
->get('currency.locale_resolver'));
}
public function formatAmount(CurrencyInterface $currency, $amount, $language_type = LanguageInterface::TYPE_CONTENT) {
$decimals = strlen($amount) - strpos($amount, '.') - 1;
$currency_locale = $this->localeDelegator
->resolveCurrencyLocale();
$formatted_amount = number_format($amount, $decimals, $currency_locale
->getDecimalSeparator(), $currency_locale
->getGroupingSeparator());
$arguments = array(
'@currency_code' => $currency
->getCurrencyCode(),
'@currency_sign' => $currency
->getSign(),
'@amount' => $formatted_amount,
);
return $this
->t('@currency_code @amount', $arguments);
}
}