public function getStrictDomainDataProvider() { ini_set('memory_limit', '6G'); // All available locales $locales = \ResourceBundle::getLocales(''); // Fake locales $locales[] = 'xx_XX_XXXX'; $locales[] = 'en_XX'; $locales[] = 'en_US_XXXX'; $locales[] = 'xx_Cyrl'; // Invalid locales $locales[] = 'foobarfoobarfoobar'; $locales[] = 'foo bar'; // All available currencies $currencies = []; $currencyResources = \ResourceBundle::create('en', 'ICUDATA-curr', true); $currencySymbols = $currencyResources->get('Currencies'); foreach ($currencySymbols as $currencyCode => $bundle) { $currencies[] = $currencyCode; } $data = []; foreach ($locales as $locale) { foreach ($currencies as $currencyCode) { $data[] = [$locale, $currencyCode]; } } return $data; }
function load_resource_bundle($locale, $directory) { $bundle = \ResourceBundle::create($locale, $directory); if (null === $bundle) { bailout('The resource bundle for locale ' . $locale . ' could not be loaded from directory ' . $directory); } return $bundle; }
/** * @param string $locale * * @throws \IntlException * @return \ResourceBundle */ private function getResourceBundle($locale) { if (!isset($this->resourceBundles[$locale])) { $resourceBundle = \ResourceBundle::create($locale, $this->resourcesDirectory, true); if ($resourceBundle === null) { throw new \IntlException('Could not create resource bundle'); } $this->resourceBundles[$locale] = $resourceBundle; } return $this->resourceBundles[$locale]; }
/** * Returns the locale names for a locale * * @param string $locale The locale to use for the locale names * * @return array The locale names with their codes as keys * * @throws \RuntimeException When the resource bundles cannot be loaded */ public static function getDisplayLocales($locale) { if (!isset(self::$locales[$locale])) { $bundle = \ResourceBundle::create($locale, self::getIcuDataDirectory() . '/names'); if (null === $bundle) { throw new \RuntimeException(sprintf('The locale resource bundle could not be loaded for locale "%s"', $locale)); } $collator = new \Collator($locale); $locales = array(); $bundleLocales = $bundle->get('Locales') ?: array(); foreach ($bundleLocales as $code => $name) { $locales[$code] = $name; } $fallbackLocale = self::getFallbackLocale($locale); if (null !== $fallbackLocale) { $locales = array_merge(self::getDisplayLocales($fallbackLocale), $locales); } $collator->asort($locales); self::$locales[$locale] = $locales; } return self::$locales[$locale]; }
/** * Retrieve the currency symbol of the given locale anche currency code. * * @param $locale * @param $currencyCode * @return string */ protected function getFirstCurrencySymbol($locale, $currencyCode) { $currencySymbol = null; $parent = null; // Check first in passed locale $currencyResources = \ResourceBundle::create($locale, 'ICUDATA-curr', false); if ($currencyResources instanceof \ResourceBundle) { $currencySymbols = $currencyResources->get('Currencies'); $parent = $currencyResources->get('%%Parent'); if ($currencySymbols instanceof \ResourceBundle) { $currencyCodeSymbols = $currencySymbols->get($this->getCurrencyCode()); if ($currencyCodeSymbols instanceof \ResourceBundle) { if ($currencySymbol = $currencyCodeSymbols->get(0)) { return $currencySymbol; // found } } } } // If root, no other fallbacks are available. Return the ISO currency code as default. if ($locale === 'root') { return $currencyCode; } // If any, check in parent if ($parent) { $currencyResources = \ResourceBundle::create($parent, 'ICUDATA-curr', false); if ($currencyResources instanceof \ResourceBundle) { $currencySymbols = $currencyResources->get('Currencies'); if ($currencySymbols instanceof \ResourceBundle) { $currencyCodeSymbols = $currencySymbols->get($this->getCurrencyCode()); if ($currencyCodeSymbols instanceof \ResourceBundle) { if ($currencySymbol = $currencyCodeSymbols->get(0)) { return $currencySymbol; // Found } } } } // If root, no other fallbacks are available. Return the ISO currency code as default. if ($parent === 'root') { return $currencyCode; } } // Fallback locale up to root if (strpos($locale, '_') !== false) { $locale = explode('_', $locale); array_pop($locale); $locale = implode('_', $locale); } else { $locale = 'root'; } return $this->getFirstCurrencySymbol($locale, $currencyCode); }
/** * Returns the locale names for a locale * * @param string $locale The locale to use for the locale names * @return array The locale names with their codes as keys * @throws RuntimeException When the resource bundles cannot be loaded */ static public function getDisplayLocales($locale) { if (!isset(self::$locales[$locale])) { $bundle = \ResourceBundle::create($locale, __DIR__.'/Resources/data/names'); if (null === $bundle) { throw new \RuntimeException('The locale resource bundle could not be loaded'); } $collator = new \Collator($locale); $locales = array(); foreach ($bundle->get('Locales') as $code => $name) { $locales[$code] = $name; } $collator->asort($locales); self::$locales[$locale] = $locales; } return self::$locales[$locale]; }