Example #1
0
 /**
  * Check if the locale is supported.
  *
  * @param  string  $locale
  *
  * @return bool
  */
 private function isSupported($locale)
 {
     return $this->supportedLocales->has($locale);
 }
Example #2
0
 /**
  * Get the translated route.
  *
  * @param  string                                             $baseUrl
  * @param  array|false                                        $parsedUrl
  * @param  string                                             $defaultLocale
  * @param  \Arcanedev\Localization\Entities\LocaleCollection  $supportedLocales
  *
  * @return string|false
  */
 public function getTranslatedRoute($baseUrl, &$parsedUrl, $defaultLocale, LocaleCollection $supportedLocales)
 {
     if (empty($parsedUrl) || !isset($parsedUrl['path'])) {
         $parsedUrl['path'] = '';
     } else {
         $path = $parsedUrl['path'] = str_replace($baseUrl, '', '/' . ltrim($parsedUrl['path'], '/'));
         foreach ($supportedLocales->keys() as $locale) {
             foreach (["%^/?{$locale}/%", "%^/?{$locale}\$%"] as $pattern) {
                 $parsedUrl['path'] = preg_replace($pattern, '$1', $parsedUrl['path']);
                 if ($parsedUrl['path'] !== $path) {
                     $defaultLocale = $locale;
                     break 2;
                 }
             }
         }
     }
     $parsedUrl['path'] = ltrim($parsedUrl['path'], '/');
     return $this->findTranslatedRouteByPath($parsedUrl['path'], $defaultLocale);
 }
 /**
  * Filter locale collection.
  *
  * @param  array  $supportedLocales
  *
  * @return \Arcanedev\Localization\Entities\LocaleCollection
  */
 private function filterLocales(array $supportedLocales)
 {
     return $this->locales->filter(function (Locale $locale) use($supportedLocales) {
         return in_array($locale->key(), $supportedLocales);
     });
 }