public function buildForm(FormBuilderInterface $builder, array $options) { $format = $options['format']; $pattern = null; $allowedFormatOptionValues = array(\IntlDateFormatter::FULL, \IntlDateFormatter::LONG, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT); // If $format is not in the allowed options, it's considered as the pattern of the formatter if it is a string if (!in_array($format, $allowedFormatOptionValues, true)) { if (is_string($format)) { $format = self::DEFAULT_FORMAT; $pattern = $options['format']; } else { throw new CreationException('The "format" option must be one of the IntlDateFormatter constants (FULL, LONG, MEDIUM, SHORT) or a string representing a custom pattern'); } } $formatter = new \IntlDateFormatter(\Locale::getDefault(), $format, \IntlDateFormatter::NONE, 'UTC', \IntlDateFormatter::GREGORIAN, $pattern); $formatter->setLenient(false); $builder->addViewTransformer(new DateTimeToLocalizedStringTransformer($options['data_timezone'], $options['user_timezone'], $format, \IntlDateFormatter::NONE, \IntlDateFormatter::GREGORIAN, $pattern)); if ('string' === $options['input']) { $builder->addModelTransformer(new ReversedTransformer(new DateTimeToStringTransformer($options['data_timezone'], $options['data_timezone'], 'Y-m-d'))); } elseif ('timestamp' === $options['input']) { $builder->addModelTransformer(new ReversedTransformer(new DateTimeToTimestampTransformer($options['data_timezone'], $options['data_timezone']))); } elseif ('array' === $options['input']) { $builder->addModelTransformer(new ReversedTransformer(new DateTimeToArrayTransformer($options['data_timezone'], $options['data_timezone'], array('year', 'month', 'day')))); } $builder->setAttribute('date_pattern', $formatter->getPattern()); }
/** * Constructor. * * @param MailLoaderInterface $loader The mail loader * @param \Twig_Environment $renderer The twig environment * @param EventDispatcherInterface $dispatcher The event dispatcher */ public function __construct(MailLoaderInterface $loader, \Twig_Environment $renderer, EventDispatcherInterface $dispatcher) { $this->loader = $loader; $this->renderer = $renderer; $this->locale = \Locale::getDefault(); $this->dispatcher = $dispatcher; }
/** * Returns the set locale * * @return string */ public function getLocale() { if (null === $this->locale) { $this->locale = \Locale::getDefault(); } return $this->locale; }
public function testDefaultLocaleFromHttpHeader() { $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'sv-se,sv;q=0.8,en-us;q=0.6,en;q=0.4'; \Locale::setDefault('fi'); \Library\Application::init('Console', true); $this->assertEquals('sv_SE', \Locale::getDefault()); }
/** * {@inheritdoc} */ public function buildView(FormView $view, FormInterface $form, array $options) { $value = $form->getViewData(); // set string representation if (true === $value) { $value = 'true'; } elseif (false === $value) { $value = 'false'; } elseif (null === $value) { $value = 'null'; } elseif (is_array($value)) { $value = implode(', ', $value); } elseif ($value instanceof \DateTime) { $dateFormat = is_int($options['date_format']) ? $options['date_format'] : DateType::DEFAULT_FORMAT; $timeFormat = is_int($options['time_format']) ? $options['time_format'] : DateType::DEFAULT_FORMAT; $calendar = \IntlDateFormatter::GREGORIAN; $pattern = is_string($options['date_pattern']) ? $options['date_pattern'] : null; $formatter = new \IntlDateFormatter(\Locale::getDefault(), $dateFormat, $timeFormat, 'UTC', $calendar, $pattern); $formatter->setLenient(false); $value = $formatter->format($value); } elseif (is_object($value)) { if (method_exists($value, '__toString')) { $value = $value->__toString(); } else { $value = get_class($value); } } $view->vars['value'] = (string) $value; }
protected function setUp() { $this->locale = \Locale::getDefault(); parent::setUp(); $this->cmMock = $this->getMockBuilder('Oro\\Bundle\\ConfigBundle\\Config\\ConfigManager')->disableOriginalConstructor()->getMock(); $this->formType = new LanguageType($this->cmMock); }
public function currencySymbolFunction($locale) { $locale = $locale == null ? \Locale::getDefault() : $locale; $formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); $symbol = $formatter->getSymbol(\NumberFormatter::CURRENCY_SYMBOL); return $symbol; }
public function __construct($key, array $options = array()) { $this->addOption('trim', true); $this->addOption('required', true); $this->addOption('disabled', false); $this->addOption('property_path', (string)$key); $this->addOption('value_transformer'); $this->addOption('normalization_transformer'); $this->key = (string)$key; if ($this->locale === null) { $this->locale = class_exists('\Locale', false) ? \Locale::getDefault() : 'en'; } parent::__construct($options); if ($this->getOption('value_transformer')) { $this->setValueTransformer($this->getOption('value_transformer')); } if ($this->getOption('normalization_transformer')) { $this->setNormalizationTransformer($this->getOption('normalization_transformer')); } $this->normalizedData = $this->normalize($this->data); $this->transformedData = $this->transform($this->normalizedData); $this->required = $this->getOption('required'); $this->setPropertyPath($this->getOption('property_path')); }
/** * @param FormView $view * @param FormInterface $form * @param array $options */ public function buildView(FormView $view, FormInterface $form, array $options) { $dataType = self::DATA_INTEGER; if (isset($options['data_type'])) { $dataType = $options['data_type']; } $formatterOptions = array(); switch ($dataType) { case self::PERCENT: $formatterOptions['decimals'] = 2; $formatterOptions['grouping'] = false; $formatterOptions['percent'] = true; break; case self::DATA_DECIMAL: $formatterOptions['decimals'] = 2; $formatterOptions['grouping'] = true; break; case self::DATA_INTEGER: default: $formatterOptions['decimals'] = 0; $formatterOptions['grouping'] = false; } $formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL); $formatterOptions['orderSeparator'] = $formatterOptions['grouping'] ? $formatter->getSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL) : ''; $formatterOptions['decimalSeparator'] = $formatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL); $view->vars['formatter_options'] = array_merge($formatterOptions, $options['formatter_options']); }
/** * {@inheritdoc} */ public function setDefaultOptions(OptionsResolverInterface $resolver) { $configs = array_merge($this->options, array('lang' => \Locale::getDefault())); $resolver->setDefaults(array('configs' => array(), 'validator' => array(), 'error_bubbling' => false))->setAllowedTypes(array('configs' => 'array', 'validator' => 'array'))->setNormalizers(array('configs' => function (Options $options, $value) use($configs) { return array_merge($configs, $value); })); }
/** * {@inheritdoc} */ public function buildForm(FormBuilder $builder, array $options) { $formatter = new \IntlDateFormatter(\Locale::getDefault(), $options['format'], \IntlDateFormatter::NONE, \DateTimeZone::UTC); if ($options['widget'] === 'single_text') { $builder->appendClientTransformer(new DateTimeToLocalizedStringTransformer($options['data_timezone'], $options['user_timezone'], $options['format'], \IntlDateFormatter::NONE)); } else { $yearOptions = $monthOptions = $dayOptions = array(); if ($options['widget'] === 'choice') { if (is_array($options['empty_value'])) { $options['empty_value'] = array_merge(array('year' => null, 'month' => null, 'day' => null), $options['empty_value']); } else { $options['empty_value'] = array('year' => $options['empty_value'], 'month' => $options['empty_value'], 'day' => $options['empty_value']); } // Only pass a subset of the options to children $yearOptions = array('choice_list' => new PaddedChoiceList(array_combine($options['years'], $options['years']), 4, '0', STR_PAD_LEFT), 'empty_value' => $options['empty_value']['year'], 'required' => $options['required']); $monthOptions = array('choice_list' => new MonthChoiceList($formatter, $options['months']), 'empty_value' => $options['empty_value']['month'], 'required' => $options['required']); $dayOptions = array('choice_list' => new PaddedChoiceList(array_combine($options['days'], $options['days']), 2, '0', STR_PAD_LEFT), 'empty_value' => $options['empty_value']['day'], 'required' => $options['required']); } $builder->add('year', $options['widget'], $yearOptions)->add('month', $options['widget'], $monthOptions)->add('day', $options['widget'], $dayOptions)->appendClientTransformer(new DateTimeToArrayTransformer($options['data_timezone'], $options['user_timezone'], array('year', 'month', 'day'))); } if ($options['input'] === 'string') { $builder->appendNormTransformer(new ReversedTransformer(new DateTimeToStringTransformer($options['data_timezone'], $options['data_timezone'], 'Y-m-d'))); } else { if ($options['input'] === 'timestamp') { $builder->appendNormTransformer(new ReversedTransformer(new DateTimeToTimestampTransformer($options['data_timezone'], $options['data_timezone']))); } else { if ($options['input'] === 'array') { $builder->appendNormTransformer(new ReversedTransformer(new DateTimeToArrayTransformer($options['data_timezone'], $options['data_timezone'], array('year', 'month', 'day')))); } } } $builder->setAttribute('formatter', $formatter)->setAttribute('widget', $options['widget']); }
/** * Parse header * * @param null|\Zend\Http\Header\AcceptLanguage $header * @return array */ public function parseHeader($header) { $locales = array(); $controller = $this->getController(); if ($controller instanceof ServiceLocatorAwareInterface) { $availables = $controller->getServiceLocator()->get('Locale')->getAvailableLocales(); } else { $availables = array(IntlLocale::getDefault()); } if ($header instanceof AcceptLanguage) { foreach ($header->getPrioritized() as $part) { if ($part instanceof LanguageFieldValuePart) { $locale = IntlLocale::parseLocale($part->getLanguage()); $key = $locale['language']; if (isset($locale['region'])) { $key .= '_' . $locale['region']; } if ($availables) { $key = IntlLocale::lookup($availables, $key, false, ''); } if ($key) { $locales[$key] = max($part->getPriority(), empty($locales[$key]) ? 0 : $locales[$key]); } } } } return $locales; }
function twig_localized_date_filter(Twig_Environment $env, $date, $dateFormat = 'medium', $timeFormat = 'medium', $locale = null, $timezone = null) { $date = twig_date_converter($env, $date, $timezone); $formatValues = array('none' => IntlDateFormatter::NONE, 'short' => IntlDateFormatter::SHORT, 'medium' => IntlDateFormatter::MEDIUM, 'long' => IntlDateFormatter::LONG, 'full' => IntlDateFormatter::FULL); $formatter = IntlDateFormatter::create($locale !== null ? $locale : Locale::getDefault(), $formatValues[$dateFormat], $formatValues[$timeFormat], $date->getTimezone()->getName()); return $formatter->format($date->getTimestamp()); }
public function createView(ViewFactory $factory, $data, array $options) : ViewInterface { $dateFormat = $this->resolveFormat($options['date_format']); $timeFormat = $this->resolveFormat($options['time_format']); $formatter = new \IntlDateFormatter(\Locale::getDefault(), $dateFormat, $timeFormat, null, \IntlDateFormatter::GREGORIAN); return new DateTimeView($formatter->format($data), $data, $options['tag']); }
/** * @param FormInterface $form * @param Request $request * @param ObjectManager $manager */ public function __construct(FormInterface $form, Request $request, ObjectManager $manager) { $this->form = $form; $this->request = $request; $this->manager = $manager; $this->defaultLocale = \Locale::getDefault(); }
/** * @param mixed $installed * @param bool $isSetLocale * @dataProvider onKernelRequestDataProvider */ public function testOnKernelRequest($installed, $isSetLocale) { $customLanguage = 'ru'; $customLocale = 'fr'; $request = new Request(); $context = new RequestContext(); $request->setDefaultLocale($this->defaultLocale); if ($isSetLocale) { $this->localeSettings->expects($this->once())->method('getLanguage')->will($this->returnValue($customLanguage)); $this->localeSettings->expects($this->once())->method('getLocale')->will($this->returnValue($customLocale)); $this->router->expects($this->once())->method('getContext')->willReturn($context); } else { $this->localeSettings->expects($this->never())->method('getLanguage'); $this->localeSettings->expects($this->never())->method('getLocale'); } $this->container->expects($this->any())->method('getParameter')->with('installed')->willReturn($installed); $this->listener = new LocaleListener($this->container); $this->listener->onKernelRequest($this->createGetResponseEvent($request)); if ($isSetLocale) { $this->assertEquals($customLanguage, $request->getLocale()); $this->assertEquals($customLanguage, $context->getParameter('_locale')); $this->assertEquals($customLocale, \Locale::getDefault()); } else { $this->assertEquals($this->defaultLocale, $request->getLocale()); $this->assertEquals($this->defaultLocale, \Locale::getDefault()); } }
public function buildView(FormView $view, FormInterface $form, array $options) { $defaultOptions = array('height' => 150, 'width' => 150, 'language' => \Locale::getDefault(), 'route' => null, 'srcRoute' => 'uploads/orig/', 'dstRoute' => 'uploads/', 'aspect' => 1); $defaultOptions = array_merge($defaultOptions, $options['pupOptions']); // \tool::xlog('picker', $defaultImageUploadOptions); $view->vars = array_replace($view->vars, array('pupOptions' => $defaultOptions)); }
/** * Returns a string representation of the value. * * This method returns the equivalent PHP tokens for most scalar types * (i.e. "false" for false, "1" for 1 etc.). Strings are always wrapped * in double quotes ("). Objects, arrays and resources are formatted as * "object", "array" and "resource". If the parameter $prettyDateTime * is set to true, {@link \DateTime} objects will be formatted as * RFC-3339 dates ("Y-m-d H:i:s"). * * Be careful when passing message parameters to a constraint violation * that (may) contain objects, arrays or resources. These parameters * should only be displayed for technical users. Non-technical users * won't know what an "object", "array" or "resource" is and will be * confused by the violation message. * * @param mixed $value The value to format as string * @param bool $prettyDateTime Whether to format {@link \DateTime} * objects as RFC-3339 dates ("Y-m-d H:i:s") * * @return string The string representation of the passed value */ protected function formatValue($value, $prettyDateTime = false) { if ($prettyDateTime && $value instanceof \DateTime) { if (class_exists('IntlDateFormatter')) { $locale = \Locale::getDefault(); $formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT); return $formatter->format($value); } return $value->format('Y-m-d H:i:s'); } if (is_object($value)) { return 'object'; } if (is_array($value)) { return 'array'; } if (is_string($value)) { return '"' . $value . '"'; } if (is_resource($value)) { return 'resource'; } if (null === $value) { return 'null'; } if (false === $value) { return 'false'; } if (true === $value) { return 'true'; } return (string) $value; }
public function buildForm(FormBuilder $builder, array $options) { $formatter = new \IntlDateFormatter(\Locale::getDefault(), $options['format'], \IntlDateFormatter::NONE, \DateTimeZone::UTC); if ($options['widget'] === 'single-text') { $builder->appendClientTransformer(new DateTimeToLocalizedStringTransformer($options['data_timezone'], $options['user_timezone'], $options['format'], \IntlDateFormatter::NONE)); } else { $yearOptions = $monthOptions = $dayOptions = array(); $widget = $options['widget']; if ($widget === 'choice') { // Only pass a subset of the options to children $yearOptions = array('choice_list' => new PaddedChoiceList(array_combine($options['years'], $options['years']), 4, '0', STR_PAD_LEFT)); $monthOptions = array('choice_list' => new MonthChoiceList($formatter, $options['months'])); $dayOptions = array('choice_list' => new PaddedChoiceList(array_combine($options['days'], $options['days']), 2, '0', STR_PAD_LEFT)); } $builder->add('year', $widget, $yearOptions)->add('month', $widget, $monthOptions)->add('day', $widget, $dayOptions)->appendClientTransformer(new DateTimeToArrayTransformer($options['data_timezone'], $options['user_timezone'], array('year', 'month', 'day'))); } if ($options['input'] === 'string') { $builder->appendNormTransformer(new ReversedTransformer(new DateTimeToStringTransformer($options['data_timezone'], $options['data_timezone'], 'Y-m-d'))); } else { if ($options['input'] === 'timestamp') { $builder->appendNormTransformer(new ReversedTransformer(new DateTimeToTimestampTransformer($options['data_timezone'], $options['data_timezone']))); } else { if ($options['input'] === 'array') { $builder->appendNormTransformer(new ReversedTransformer(new DateTimeToArrayTransformer($options['data_timezone'], $options['data_timezone'], array('year', 'month', 'day')))); } else { if ($options['input'] !== 'datetime') { throw new FormException('The "input" option must be "datetime", "string", "timestamp" or "array".'); } } } } $builder->setAttribute('formatter', $formatter)->setAttribute('widget', $options['widget']); }
/** * Test defaults */ public function testDefaults() { $locale = new Locale(); $this->assertSame(Locale::DEFAULT_LOCALE, $locale->getDefault()); $this->assertSame(Locale::DEFAULT_LOCALE, $locale->getFallback()); $this->assertSame(array(Locale::DEFAULT_LOCALE), $locale->getAvailableLocales()); }
/** * {@inheritdoc} */ public function configureOptions(OptionsResolver $resolver) { $configs = array_merge(array('language' => \Locale::getDefault()), $this->options); $resolver->setDefaults(array('configs' => array(), 'required' => false, 'theme' => 'default'))->setAllowedTypes(array('configs' => 'array', 'theme' => 'string'))->setNormalizers(array('configs' => function (Options $options, $value) use($configs) { return array_merge($configs, $value); })); }
/** * {@inheritdoc} */ public function configureOptions(OptionsResolver $resolver) { $format = function (Options $options) { $date_format = \IntlDateFormatter::NONE; $time_format = \IntlDateFormatter::NONE; if ($options['date_picker']) { $date_format = \IntlDateFormatter::SHORT; } if ($options['time_picker']) { $time_format = $options['with_seconds'] ? \IntlDateFormatter::MEDIUM : \IntlDateFormatter::SHORT; } $formater = new \IntlDateFormatter($options['locale'], $date_format, $time_format, $options['user_timezone'], \IntlDateFormatter::GREGORIAN, null); $formater->setLenient(false); $pattern = $formater->getPattern(); if (false === strpos($pattern, 'yyyy')) { if (false !== strpos($pattern, 'yy')) { $pattern = str_replace('yy', 'yyyy', $pattern); } elseif (false !== strpos($pattern, 'y')) { $pattern = str_replace('y', 'yyyy', $pattern); } } return $pattern; }; $resolver->setDefaults(array('widget' => 'single_text', 'locale' => \Locale::getDefault(), 'user_timezone' => null, 'date_picker' => true, 'time_picker' => true, 'time_picker_first' => false, 'button_id' => null, 'open_focus' => true, 'hour_min' => null, 'hour_max' => null, 'hour_step' => null, 'minute_min' => null, 'minute_max' => null, 'minute_step' => null, 'second_min' => null, 'second_max' => null, 'second_step' => null, 'format' => $format, 'empty_value' => null, 'with_minutes' => true, 'with_seconds' => false)); }
public function format($datetime, $dateFormat = null, $timeFormat = null, $timezone = null, $locale = null) { $dt = null; $tz = null; if (null !== $timezone) { $tz = is_string($timezone) ? new \DateTimeZone($timezone) : $timezone; } if ($datetime instanceof \DateTimeImmutable) { $dt = new \DateTime($datetime->format('Y-m-d H:i:s'), $tz); } elseif (!$datetime instanceof \DateTime) { $dt = new \DateTime($datetime, $tz); } else { $dt = clone $datetime; } if (null === $tz) { $tz = $dt->getTimezone(); } if (null === $locale) { $locale = \Locale::getDefault(); } if (null === $dateFormat) { $dateFormat = \IntlDateFormatter::MEDIUM; } if (null === $timeFormat) { $timeFormat = \IntlDateFormatter::NONE; } $currentLocale = \Locale::getDefault(); \Locale::setDefault($locale); $formatter = new \IntlDateFormatter($locale, $dateFormat, $timeFormat, $tz); $result = $formatter->format($dt); \Locale::setDefault($currentLocale); return $result; }
protected function configure() { $this->setName('fakery:info')->setDescription('list available Fakery generator configuration sections')->setHelp(<<<EOF The <info>%command.name%</info> list all available Fakery generator configuration sections : <info>%command.full_name%</info> The <info>%command.name%</info> list available Faker locales : <info>%command.full_name%</info> locale To change language display translation set the <info>locale</info> option : <info>%command.full_name% --locale=fr_FR</info> locale The <info>%command.name%</info> list available output file formats : <info>%command.full_name%</info> format The <info>%command.name%</info> list available columns converters : <info>%command.full_name%</info> converter The <info>%command.name%</info> list available Faker providers : <info>%command.full_name%</info> provider The <info>%command.name%</info> list available Faker methods : <info>%command.full_name%</info> method EOF )->addArgument('sections', InputArgument::IS_ARRAY, 'List of sections to display : ' . implode(', ', static::$availableSections), static::$defaultSections)->addOption('locale', 'l', InputOption::VALUE_REQUIRED, 'locale used to display language name', \Locale::getDefault())->addOption('filter-provider', 'p', InputOption::VALUE_REQUIRED, 'filter methods on provider')->addOption('filter-locale', 'o', InputOption::VALUE_REQUIRED, 'filter methods on locale'); }
protected function configure() { $this->addOption('widget', self::CHOICE, self::$widgets); $this->addOption('type', self::DATETIME, self::$types); $this->addOption('pattern'); $this->addOption('years', range(date('Y') - 5, date('Y') + 5)); $this->addOption('months', range(1, 12)); $this->addOption('days', range(1, 31)); $this->addOption('format', self::MEDIUM, self::$formats); $this->addOption('data_timezone', 'UTC'); $this->addOption('user_timezone', 'UTC'); $this->formatter = new \IntlDateFormatter(\Locale::getDefault(), self::$intlFormats[$this->getOption('format')], \IntlDateFormatter::NONE); if ($this->getOption('type') === self::STRING) { $this->setNormalizationTransformer(new ReversedTransformer(new DateTimeToStringTransformer(array('input_timezone' => $this->getOption('data_timezone'), 'output_timezone' => $this->getOption('data_timezone'), 'format' => 'Y-m-d')))); } else { if ($this->getOption('type') === self::TIMESTAMP) { $this->setNormalizationTransformer(new ReversedTransformer(new DateTimeToTimestampTransformer(array('output_timezone' => $this->getOption('data_timezone'), 'input_timezone' => $this->getOption('data_timezone'))))); } else { if ($this->getOption('type') === self::RAW) { $this->setNormalizationTransformer(new ReversedTransformer(new DateTimeToArrayTransformer(array('input_timezone' => $this->getOption('data_timezone'), 'output_timezone' => $this->getOption('data_timezone'), 'fields' => array('year', 'month', 'day'))))); } } } if ($this->getOption('widget') === self::INPUT) { $this->setValueTransformer(new DateTimeToLocalizedStringTransformer(array('date_format' => $this->getOption('format'), 'time_format' => DateTimeToLocalizedStringTransformer::NONE, 'input_timezone' => $this->getOption('data_timezone'), 'output_timezone' => $this->getOption('user_timezone')))); $this->setFieldMode(self::FIELD); } else { $this->setValueTransformer(new DateTimeToArrayTransformer(array('input_timezone' => $this->getOption('data_timezone'), 'output_timezone' => $this->getOption('user_timezone')))); $this->setFieldMode(self::FORM); $this->addChoiceFields(); } }
/** * Returns the pattern for this locale * * The pattern contains the placeholder "{{ widget }}" where the HTML tag should * be inserted */ public function getPattern() { if (!$this->getOption('currency')) { return '{{ widget }}'; } if (!isset(self::$patterns[\Locale::getDefault()])) { self::$patterns[\Locale::getDefault()] = array(); } if (!isset(self::$patterns[\Locale::getDefault()][$this->getOption('currency')])) { $format = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::CURRENCY); $pattern = $format->formatCurrency('123', $this->getOption('currency')); // the spacings between currency symbol and number are ignored, because // a single space leads to better readability in combination with input // fields // the regex also considers non-break spaces (0xC2 or 0xA0 in UTF-8) preg_match('/^([^\\s\\xc2\\xa0]*)[\\s\\xc2\\xa0]*123[,.]00[\\s\\xc2\\xa0]*([^\\s\\xc2\\xa0]*)$/', $pattern, $matches); if (!empty($matches[1])) { self::$patterns[\Locale::getDefault()] = $matches[1] . ' {{ widget }}'; } else { if (!empty($matches[2])) { self::$patterns[\Locale::getDefault()] = '{{ widget }} ' . $matches[2]; } else { self::$patterns[\Locale::getDefault()] = '{{ widget }}'; } } } return self::$patterns[\Locale::getDefault()]; }
/** * Returns the pattern for this locale. * * The pattern contains the placeholder "{{ widget }}" where the HTML tag should * be inserted */ protected static function getPattern($currency) { if (!$currency) { return '{{ widget }}'; } $locale = \Locale::getDefault(); if (!isset(self::$patterns[$locale])) { self::$patterns[$locale] = array(); } if (!isset(self::$patterns[$locale][$currency])) { $format = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); $pattern = $format->formatCurrency('123', $currency); // the spacings between currency symbol and number are ignored, because // a single space leads to better readability in combination with input // fields // the regex also considers non-break spaces (0xC2 or 0xA0 in UTF-8) preg_match('/^([^\\s\\xc2\\xa0]*)[\\s\\xc2\\xa0]*123(?:[,.]0+)?[\\s\\xc2\\xa0]*([^\\s\\xc2\\xa0]*)$/u', $pattern, $matches); if (!empty($matches[1])) { self::$patterns[$locale][$currency] = $matches[1] . ' {{ widget }}'; } elseif (!empty($matches[2])) { self::$patterns[$locale][$currency] = '{{ widget }} ' . $matches[2]; } else { self::$patterns[$locale][$currency] = '{{ widget }}'; } } return self::$patterns[$locale][$currency]; }
/** * @param null|string $locale */ public function __construct($locale = null) { $this->locale = $locale; if (is_null($this->locale)) { $this->locale = \Locale::getDefault(); } }
function it_should_configure_the_resolver(OptionsResolverInterface $resolver) { $resolver->setDefaults(array('format' => Argument::any(), 'language' => \Locale::getDefault(), 'leading_zero' => false))->shouldBeCalled(); $resolver->setOptional(array('placeholder', 'language', 'leading_zero'))->shouldBeCalled(); $resolver->setAllowedTypes(array('placeholder' => array('string'), 'language' => array('string'), 'leading_zero' => array('bool')))->shouldBeCalled(); $this->setDefaultOptions($resolver); }
public function createService(ServiceLocatorInterface $serviceLocator) { $adapter = null; $locale = null; $config = $serviceLocator->has('Config') ? $serviceLocator->get('Config') : []; $config = isset($config['locale_manager']) ? $config['locale_manager'] : []; if ($serviceLocator->has('LocaleManager\\Adapter\\AdapterInterface')) { $adapter = $serviceLocator->get('LocaleManager\\Adapter\\AdapterInterface'); if (!$adapter instanceof AdapterInterface) { throw new ServiceNotCreatedException(sprintf('LocaleManager requires that the %s service implement %s; received "%s"', 'LocaleManager\\Adapter\\AdapterInterface', 'LocaleManager\\Adapter\\AdapterInterface', is_object($adapter) ? get_class($adapter) : gettype($adapter))); } } $manager = new LocaleManager($adapter); // Set the current locale if (isset($config['locale'])) { $locale = $config['locale']; } elseif ($serviceLocator->has('Translator')) { $translator = $serviceLocator->get('Translator'); if ($translator instanceof \Zend\Mvc\I18n\Translator) { $translator = $translator->getTranslator(); } if (method_exists($translator, 'getLocale')) { $locale = $translator->getLocale(); } } if ($locale === null) { $locale = \Locale::getDefault(); } $manager->setLocale($locale); // Return the manager return $manager; }