本文整理汇总了PHP中Symfony\Component\Intl\Intl类的典型用法代码示例。如果您正苦于以下问题:PHP Intl类的具体用法?PHP Intl怎么用?PHP Intl使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Intl类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: guard
/**
* @param mixed $value
*
* @throws InvalidLocaleException
*/
protected function guard($value)
{
$localeName = Intl::getLocaleBundle()->getLocaleName($value);
if (null === $localeName) {
throw new InvalidLocaleException($value);
}
}
示例2: __construct
/**
* Constructor
*/
public function __construct(ProfileService $profileService, DocumentManager $documentManager, TranslatorInterface $translator)
{
$this->dm = $documentManager;
$this->languages = Intl::getLanguageBundle()->getLanguageNames();
$this->profileService = $profileService;
$this->translator = $translator;
}
示例3: validateUPU_S10
public static function validateUPU_S10($track)
{
if (!preg_match("/^([A-Z]{2})(\\d{8})(\\d{1})([A-Z]{2})\$/", $track, $matches)) {
return false;
}
list($track, $serviceIndicator, $serialNumber, $checkDigit, $countryCode) = $matches;
// Check $serviceIndicator
if (!preg_match("/([ELMRUVCHABDNPZ][A-Z])|(Q[A-M])|(G[AD])/", $serviceIndicator)) {
return false;
}
// Check $checkDigit
$serialNumberDigits = str_split($serialNumber);
$weightFactors = array(8, 6, 4, 2, 3, 5, 9, 7);
$weightedSum = array_reduce(range(0, 7), function ($sum, $i) use($serialNumberDigits, $weightFactors) {
return $sum + $serialNumberDigits[$i] * $weightFactors[$i];
});
$res = 11 - $weightedSum % 11;
$appropriateCheckDigit = $res >= 1 && $res <= 9 ? $res : ($res == 11 ? 5 : 0);
if ($appropriateCheckDigit != $checkDigit) {
return false;
}
// Check $countryCode
$countryCodes = array_keys(Intl::getRegionBundle()->getCountryNames());
if (!in_array($countryCode, $countryCodes)) {
return false;
}
return true;
}
示例4: requireFullIntl
/**
* Should be called before tests that require a feature-complete intl
* implementation.
*
* @param \PhpUnit_Framework_TestCase $testCase
*/
public static function requireFullIntl(\PhpUnit_Framework_TestCase $testCase)
{
// We only run tests if the intl extension is loaded...
if (!Intl::isExtensionLoaded()) {
$testCase->markTestSkipped('The intl extension is not available.');
}
// ... and only if the version is *one specific version* ...
if (IcuVersion::compare(Intl::getIcuVersion(), Intl::getIcuStubVersion(), '!=', $precision = 1)) {
$testCase->markTestSkipped('Please change ICU version to ' . Intl::getIcuStubVersion());
}
// ... and only if the data in the Icu component matches that version.
if (IcuVersion::compare(Intl::getIcuDataVersion(), Intl::getIcuStubVersion(), '!=', $precision = 1)) {
$testCase->markTestSkipped('Please change the Icu component to version 1.0.x or 1.' . IcuVersion::normalize(Intl::getIcuStubVersion(), 1) . '.x');
}
// Normalize the default locale in case this is not done explicitly
// in the test
\Locale::setDefault('en');
// Consequently, tests will
//
// * run only for one ICU version (see Intl::getIcuStubVersion())
// there is no need to add control structures to your tests that
// change the test depending on the ICU version.
// * always use the C intl classes
// * always use the binary resource bundles (any locale is allowed)
}
示例5: loadChoiceList
/**
* {@inheritdoc}
*/
public function loadChoiceList($value = null)
{
if (null !== $this->choiceList) {
return $this->choiceList;
}
return $this->choiceList = new ArrayChoiceList(array_flip(Intl::getLanguageBundle()->getLanguageNames()), $value);
}
示例6: execute
protected function execute(InputInterface $input, OutputInterface $output)
{
$textWriter = new TextBundleWriter();
$textWriter->write(CACHE_PATH . 'intl/text_bundle', 'en', ['data' => ['Ok']]);
$textWriter->write(CACHE_PATH . 'intl/text_bundle', 'ru', ['data' => ['Хорошо']]);
$phpWriter = new PhpBundleWriter();
$phpWriter->write(CACHE_PATH . 'intl/php_bundle', 'en', ['data' => 'php bundle: Ok', 'nested' => ['message' => 'Hi!']]);
$phpWriter = new PhpBundleWriter();
$phpWriter->write(CACHE_PATH . 'intl/php_bundle', 'ru', ['data' => 'php bundle: Хорошо', 'nested' => ['message' => 'Привет!']]);
$compiler = new GenrbCompiler();
$compiler->compile(CACHE_PATH . 'intl/text_bundle', CACHE_PATH . 'intl/compiled');
$phpReader = new PhpBundleReader();
$data = $phpReader->read(CACHE_PATH . 'intl/php_bundle', 'en');
$output->writeln($data['data']);
$data = $phpReader->read(CACHE_PATH . 'intl/php_bundle', 'ru');
$output->writeln($data['data']);
$reader = new BundleEntryReader($phpReader);
$data = $reader->readEntry(CACHE_PATH . 'intl/php_bundle', 'ru', ['nested', 'message']);
$output->writeln($data);
$language = Intl::getLanguageBundle()->getLanguageName('ru', 'RU', 'en');
$output->writeln($language);
$language = Intl::getLanguageBundle()->getLanguageName('ru', 'RU', 'de');
$output->writeln($language);
$language = Intl::getLanguageBundle()->getLanguageName('ru', 'RU', 'ru');
$output->writeln($language);
$currencyName = Intl::getCurrencyBundle()->getCurrencyName('RUB', 'en');
$output->writeln($currencyName);
$currencyName = Intl::getCurrencyBundle()->getCurrencySymbol('USD', 'en');
$output->writeln($currencyName);
$output->writeln('<comment>Ok</comment>');
}
示例7: getCountryName
/**
* Return the country name using the Locale class.
*
* @param string $isoCode Country ISO 3166-1 alpha 2 code
* @param null|string $locale Locale code
*
* @return null|string Country name
*/
public function getCountryName($isoCode, $locale = null)
{
if ($isoCode === null) {
return;
}
return $locale === null ? Intl::getRegionBundle()->getCountryName($isoCode) : Intl::getRegionBundle()->getCountryName($isoCode, $locale);
}
示例8: getName
/**
* @return string
*/
public function getName($locale = null)
{
$enc = 'utf-8';
$names = Intl::getLocaleBundle()->getLocaleNames($locale ?: $this->locale);
$str = isset($names[$this->locale]) ? $names[$this->locale] : $this->locale;
return mb_strtoupper(mb_substr($str, 0, 1, $enc), $enc) . mb_substr($str, 1, mb_strlen($str, $enc), $enc);
}
示例9: getGlobals
public function getGlobals()
{
$locales = array('current' => null, 'other' => null);
$enabledLocales = $this->localeRepository->findBy(array('isEnabled' => true));
$otherLocales = $enabledLocales;
if (count($enabledLocales)) {
/** @var Locale $locale */
foreach ($enabledLocales as $localeKey => $locale) {
if ($this->currentLocale == $locale->getCode()) {
$locales['current'] = $locale;
unset($otherLocales[$localeKey]);
break;
}
}
}
if (!count($enabledLocales) || empty($locales['current'])) {
$defaultLocaleCode = $this->container->getParameter('locale');
$locale = new Locale();
$locale->setCode($defaultLocaleCode);
$locale->setTitle(Intl::getLocaleBundle()->getLocaleName($defaultLocaleCode, $defaultLocaleCode));
$locales['current'] = $locale;
}
$locales['other'] = $otherLocales;
return array('locales' => $locales);
}
示例10: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
if (self::WIDGET_COUNTRY_CHOICE === $options['widget']) {
$util = PhoneNumberUtil::getInstance();
$countries = array();
if (is_array($options['country_choices'])) {
foreach ($options['country_choices'] as $country) {
$code = $util->getCountryCodeForRegion($country);
if ($code) {
$countries[$country] = $code;
}
}
}
if (empty($countries)) {
foreach ($util->getSupportedRegions() as $country) {
$countries[$country] = $util->getCountryCodeForRegion($country);
}
}
$countryChoices = array();
foreach (Intl::getRegionBundle()->getCountryNames() as $region => $name) {
if (false === isset($countries[$region])) {
continue;
}
$countryChoices[$region] = sprintf('%s (+%s)', $name, $countries[$region]);
}
$countryOptions = $numberOptions = array('error_bubbling' => true, 'required' => $options['required'], 'disabled' => $options['disabled'], 'translation_domain' => $options['translation_domain']);
$countryOptions['required'] = true;
$countryOptions['choices'] = $countryChoices;
$countryOptions['preferred_choices'] = $options['preferred_country_choices'];
$countryOptions['choice_translation_domain'] = false;
$builder->add('country', 'choice', $countryOptions)->add('number', 'text', $numberOptions)->addViewTransformer(new PhoneNumberToArrayTransformer(array_keys($countryChoices)));
} else {
$builder->addViewTransformer(new PhoneNumberToStringTransformer($options['default_region'], $options['format']));
}
}
示例11: guard
/**
* @param mixed $value
*
* @throws InvalidLanguageCodeException
*/
protected function guard($value)
{
$languageName = Intl::getLanguageBundle()->getLanguageName($value);
if (null === $languageName) {
throw new InvalidLanguageCodeException($value);
}
}
示例12: translateCountryIsoCode
/**
* @param mixed $country
* @param string $locale
*
* @return string
*/
public function translateCountryIsoCode($country, $locale = null)
{
if ($country instanceof CountryInterface) {
return Intl::getRegionBundle()->getCountryName($country->getCode(), $locale);
}
return Intl::getRegionBundle()->getCountryName($country, $locale);
}
示例13: testToString
public function testToString($value, $currencyCode, $expectedOutput)
{
if (Intl::isExtensionLoaded()) {
\Locale::setDefault('en');
}
$this->if($price = new TestedPrice($value, $currencyCode))->string((string) $price)->isIdenticalTo($expectedOutput);
}
示例14: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$intlBundle = Intl::getLanguageBundle();
$builder->add('locale', ChoiceType::class, ['choices' => $options['lang_code'], 'choices_as_values' => true, 'choice_label' => function ($lang) use($intlBundle) {
return $intlBundle->getLanguageName($lang);
}]);
}
示例15: validate
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Country) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Country');
}
if (null === $value || '' === $value) {
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
$value = (string) $value;
$countries = Intl::getRegionBundle()->getCountryNames();
if (!isset($countries[$value])) {
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Country::NO_SUCH_COUNTRY_ERROR)
->addViolation();
} else {
$this->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Country::NO_SUCH_COUNTRY_ERROR)
->addViolation();
}
}
}