示例#1
0
 /**
  * Returns all active locales with short and long names
  *
  * @param string|null $locale
  * @return array
  */
 public function getActiveLocales($locale = null)
 {
     $registeredLocales = $this->registry->getRegisteredLocales();
     if (empty($registeredLocales)) {
         return [];
     } elseif ($locale === null) {
         $translatedLocales = $registeredLocales;
     } else {
         if (strpos($locale, '_') > 0) {
             $displayCountryLocales = Intl::getLocaleBundle()->getLocaleNames($locale);
             $displayLgLocales = Intl::getLocaleBundle()->getLocaleNames(substr($locale, 0, strpos($locale, '_')));
             $displayLocales = array_merge($displayLgLocales, $displayCountryLocales);
         } else {
             $displayLocales = Intl::getLocaleBundle()->getLocaleNames($locale);
         }
         // time to return a translated locale
         $translatedLocales = array_map(function ($element) use($displayLocales, $locale) {
             if (array_key_exists($element, $displayLocales)) {
                 // perfect, we have the full translated locale
                 return $displayLocales[$element];
             } elseif (strpos($element, '_') > 0) {
                 // ow we don't, let's see if it's a locale containing the country
                 list($lg, $country) = explode('_', $element);
                 if (strpos($locale, '_') > 0) {
                     // Yep! it's a full locale. Let's merge the translated countries for the full locale + his parent, the lg
                     $displayCountriesChild = Intl::getRegionBundle()->getCountryNames($locale);
                     $displayCountriesParent = Intl::getRegionBundle()->getCountryNames(substr($locale, 0, strpos($locale, '_')));
                     $displayCountries = array_merge($displayCountriesParent, $displayCountriesChild);
                 } else {
                     // it's just a lg
                     $displayCountries = Intl::getRegionBundle()->getCountryNames($locale);
                 }
                 if (array_key_exists($country, $displayCountries)) {
                     // ok we do have a country translation, let's manually build the full translation string
                     $displayCountry = $displayCountries[$country];
                     $displayLg = Intl::getLanguageBundle()->getLanguageName($lg, null, $locale);
                     return $displayLg . ' (' . $displayCountry . ')';
                 } else {
                     // I give up. I just return the received locale.
                     return $element;
                 }
             } else {
                 return $element;
             }
         }, $registeredLocales);
     }
     return array_combine($registeredLocales, $translatedLocales);
 }
示例#2
0
 /**
  * @param string $name
  * @param array $parameters
  * @param bool|string $referenceType
  * @throws \UnexpectedValueException
  * @return string
  */
 public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
 {
     $originalParameters = $parameters;
     try {
         if (isset($parameters['_locale'])) {
             $locale = $parameters['_locale'];
         } else {
             $locale = $this->getContext()->getParameter('_locale');
         }
         if (!in_array($locale, $this->registry->getRegisteredLocales())) {
             throw new \UnexpectedValueException(sprintf('The locale %s has not been registered in the leapt_i18n config', $locale));
         }
         $i18nName = $name . '.' . $locale;
         unset($parameters['_locale']);
         return parent::generate($i18nName, $parameters, $referenceType);
     } catch (RouteNotFoundException $e) {
         return parent::generate($name, $originalParameters, $referenceType);
     }
 }
 /**
  * @param RouteCollection $collection
  * @param $annot
  * @param $globals
  * @param \ReflectionClass $class
  * @param \ReflectionMethod $method
  */
 protected function addRoute(RouteCollection $collection, $annot, $globals, \ReflectionClass $class, \ReflectionMethod $method)
 {
     $i18n = isset($annot->data['i18n']) ? $annot->data['i18n'] : true;
     unset($annot->data['i18n']);
     foreach ($this->registry->getRegisteredLocales() as $locale) {
         $i18nAnnot = new Route($annot->data);
         $i18nGlobals = $globals;
         if ($i18n) {
             $i18nAnnot->setName($this->helper->alterName($i18nAnnot->getName(), $locale));
             $i18nAnnot->setPath($this->helper->alterPath($i18nAnnot->getPath(), $locale));
             $i18nAnnot->setDefaults($this->helper->alterDefaults($i18nAnnot->getDefaults(), $locale));
             if (isset($i18nGlobals['path']) && !empty($i18nGlobals['path'])) {
                 $i18nGlobals['path'] = '/' . $locale . '/' . ltrim($this->helper->alterPath($i18nGlobals['path'], $locale), '/');
             } else {
                 $i18nGlobals['path'] = '/' . $locale;
             }
         }
         parent::addRoute($collection, $i18nAnnot, $i18nGlobals, $class, $method);
     }
 }