/** * Get name format based on locale, if locale is not passed locale from system configuration will be used. * * @param string|null $locale * @throws \RuntimeException */ public function getNameFormat($locale = null) { if (!$locale) { $locale = $this->localeSettings->getLocale(); } $nameFormats = $this->localeSettings->getNameFormats(); // match by locale (for example - "fr_CA") if (isset($nameFormats[$locale])) { return $nameFormats[$locale]; } // match by locale language (for example - "fr") $localeParts = \Locale::parseLocale($locale); if (isset($localeParts[\Locale::LANG_TAG])) { $match = $localeParts[\Locale::LANG_TAG]; if (isset($match, $nameFormats[$match])) { return $nameFormats[$match]; } } // match by default locale in system configuration settings $match = $this->localeSettings->getLocale(); if ($match !== $locale && isset($nameFormats[$match])) { return $nameFormats[$match]; } // fallback to default constant locale $match = LocaleConfiguration::DEFAULT_LOCALE; if (isset($nameFormats[$match])) { return $nameFormats[$match]; } throw new \RuntimeException(sprintf('Cannot get name format for "%s"', $locale)); }
/** * @param Request $request */ public function setRequest(Request $request = null) { if (!$request) { return; } if (!$request->attributes->get('_locale')) { $request->setLocale($this->localeSettings->getLanguage()); } $this->setPhpDefaultLocale($this->localeSettings->getLocale()); }
/** * Get the pattern used for the IntlDateFormatter * * @param int|string $dateType Constant of IntlDateFormatter (NONE, FULL, LONG, MEDIUM, SHORT) or it's string name * @param int|string $timeType Constant IntlDateFormatter (NONE, FULL, LONG, MEDIUM, SHORT) or it's string name * @param string|null $locale * @return string */ public function getPattern($dateType, $timeType, $locale = null) { if (!$locale) { $locale = $this->localeSettings->getLocale(); } if (null === $dateType) { $dateType = static::DEFAULT_DATE_TYPE; } if (null === $timeType) { $timeType = static::DEFAULT_TIME_TYPE; } $dateType = $this->parseDateType($dateType); $timeType = $this->parseDateType($timeType); $localeFormatter = new \IntlDateFormatter($locale, $dateType, $timeType, null, \IntlDateFormatter::GREGORIAN); return $localeFormatter->getPattern(); }
/** * Get address format based on locale or region, if argument is not passed locale from * system configuration will be used. * * @param string|null $localeOrRegion * @throws \RuntimeException */ public function getAddressFormat($localeOrRegion = null) { if (!$localeOrRegion) { $localeOrRegion = $this->localeSettings->getLocale(); } $addressFormats = $this->localeSettings->getAddressFormats(); // matched by country (for example - "RU") if (isset($addressFormats[$localeOrRegion][LocaleSettings::ADDRESS_FORMAT_KEY])) { return $addressFormats[$localeOrRegion][LocaleSettings::ADDRESS_FORMAT_KEY]; } // matched by locale region - "CA" $localeParts = \Locale::parseLocale($localeOrRegion); if (isset($localeParts[\Locale::REGION_TAG])) { $match = $localeParts[\Locale::REGION_TAG]; if (isset($match, $addressFormats[$match][LocaleSettings::ADDRESS_FORMAT_KEY])) { return $addressFormats[$match][LocaleSettings::ADDRESS_FORMAT_KEY]; } } // match by default country in system configuration settings $match = $this->localeSettings->getCountry(); if ($match !== $localeOrRegion && isset($addressFormats[$match][LocaleSettings::ADDRESS_FORMAT_KEY])) { return $addressFormats[$match][LocaleSettings::ADDRESS_FORMAT_KEY]; } // fallback to default country $match = LocaleConfiguration::DEFAULT_COUNTRY; if (isset($addressFormats[$match][LocaleSettings::ADDRESS_FORMAT_KEY])) { return $addressFormats[$match][LocaleSettings::ADDRESS_FORMAT_KEY]; } throw new \RuntimeException(sprintf('Cannot get address format for "%s"', $localeOrRegion)); }
/** * @param ConsoleCommandEvent $event */ public function onConsoleCommand(ConsoleCommandEvent $event) { $isForced = $event->getInput()->hasParameterOption('--force'); if ($isForced) { $this->isInstalled = false; return; } if ($this->isInstalled) { try { $locale = $this->localeSettings->getLocale(); $language = $this->localeSettings->getLanguage(); } catch (DBALException $exception) { // application is not installed return; } $this->setPhpDefaultLocale($locale); $this->translatableListener->setTranslatableLocale($language); } }
/** * @param array $currencies * @throws LogicException */ protected function checkCurrencies(array $currencies) { $invalidCurrencies = []; foreach ($currencies as $currency) { $name = Intl::getCurrencyBundle()->getCurrencyName($currency, $this->localeSettings->getLocale()); if (!$name) { $invalidCurrencies[] = $currency; } } if ($invalidCurrencies) { throw new LogicException(sprintf('Found unknown currencies: %s.', implode(', ', $invalidCurrencies))); } }
/** * @param string $currency * @param string|null $locale * @return bool|null Null means that there are no currency symbol in string */ public function isCurrencySymbolPrepend($currency, $locale = null) { if (!$locale) { $locale = $this->localeSettings->getLocale(); } if (empty($this->currencySymbolPrepend[$locale]) || !array_key_exists($currency, $this->currencySymbolPrepend)) { $formatter = $this->getFormatter($locale, \NumberFormatter::CURRENCY); $pattern = $formatter->formatCurrency('123', $currency); preg_match('/^([^\\s\\xc2\\xa0]*)[\\s\\xc2\\xa0]*123(?:[,.]0+)?[\\s\\xc2\\xa0]*([^\\s\\xc2\\xa0]*)$/u', $pattern, $matches); if (!empty($matches[1])) { $this->currencySymbolPrepend[$locale][$currency] = true; } elseif (!empty($matches[2])) { $this->currencySymbolPrepend[$locale][$currency] = false; } else { $this->currencySymbolPrepend[$locale][$currency] = null; } } return $this->currencySymbolPrepend[$locale][$currency]; }
/** * @param string $expectedValue * @param string $configurationValue * @dataProvider getLocaleDataProvider */ public function testGetLocale($expectedValue, $configurationValue) { $this->configManager->expects($this->once())->method('get')->with('oro_locale.locale')->will($this->returnValue($configurationValue)); $this->assertEquals($expectedValue, $this->localeSettings->getLocale()); $this->assertEquals($expectedValue, $this->localeSettings->getLocale()); }