private function replaceDefaultTaxonomySlug($url, $taxonomyDetails)
 {
     $localeCode = $this->currentLocale->getCode();
     if (Hash::check((array) $taxonomyDetails->i18n, "{$localeCode}.rewrite.slug")) {
         return Utility::replaceFirstOccurence(Hash::get($taxonomyDetails->i18n, "{$localeCode}.rewrite.slug"), $taxonomyDetails->rewrite['slug'], $url);
     }
     return $url;
 }
 public function __construct(I18n $i18n, Rewriter $rewriter)
 {
     $this->rewriter = $rewriter;
     $this->i18n = $i18n;
     $this->currentLocale = $i18n->getCurrentLocale();
     $this->defaultLocale = $i18n->getDefaultLocale();
     $this->urlRegex = Utility::getLocaleUrlsRegex();
 }
 protected function removeLocalizedRoutedSlugs($route, $model)
 {
     if (!$this->currentLocale->isDefault() && is_array($model->routed)) {
         $key = "i18n." . $this->currentLocale->getCode() . ".rewrite";
         if (Hash::check($model->routed, $key)) {
             foreach (Hash::get($model->routed, $key) as $rewriteKey => $rewriteUrl) {
                 if (Hash::check($model->routed, "rewrite.{$rewriteKey}")) {
                     $defaultValue = Hash::get($model->routed, "rewrite.{$rewriteKey}");
                     $route = Utility::replaceFirstOccurence($rewriteUrl, $defaultValue, $route);
                 }
             }
         }
     }
     return $route;
 }
 /**
  * Triggered in the front end to learn the locale based on the type
  * of object the user is browsing. Prevents a user from seeing
  * a object in another locale than the one the object is supposed to be in.
  * @return \Strata\i18n\Locale
  */
 public function getByFrontContext()
 {
     $i18n = Strata::i18n();
     $defaultLocale = $i18n->getDefaultLocale();
     global $wp_query;
     // Prevents 'xyz was called incorrectly' messages.
     if (!is_null($wp_query) && !Strata::isCommandLineInterface() && !is_search() && !is_404()) {
         // By Post
         $postId = get_the_ID();
         if ($postId) {
             $suspectedLocale = $this->getLocaleByPostId($postId);
             if ($suspectedLocale && !$suspectedLocale->isDefault() && $i18n->shouldFallbackToDefaultLocale()) {
                 $defaultPost = $defaultLocale->getTranslatedPost($postId);
                 $localizedPost = $suspectedLocale->getTranslatedPost($postId);
                 if ($localizedPost && $defaultPost) {
                     if ((int) $defaultPost->ID !== (int) $localizedPost->ID) {
                         return $suspectedLocale;
                     }
                 }
             }
         }
         // By Taxonomy
         global $wp_query;
         if ($wp_query) {
             $taxonomy = $wp_query->queried_object;
             if (is_a($taxonomy, "WP_Term")) {
                 $suspectedLocale = $this->getLocaleByTaxonomyId($taxonomy->term_id, $taxonomy->taxonomy);
                 if ($suspectedLocale && !$suspectedLocale->isDefault() && $i18n->shouldFallbackToDefaultLocale()) {
                     $defaultPost = $defaultLocale->getTranslatedTerm($taxonomy->term_id, $taxonomy->taxonomy);
                     $localizedPost = $suspectedLocale->getTranslatedTerm($taxonomy->term_id, $taxonomy->taxonomy);
                     if ($localizedPost && $defaultPost) {
                         if ((int) $defaultPost->term_id !== (int) $localizedPost->term_id) {
                             return $suspectedLocale;
                         }
                     }
                 }
             }
         }
     }
     if (preg_match('#/(' . Utility::getLocaleUrlsRegex() . ')/#', $_SERVER['REQUEST_URI'], $matches)) {
         return Strata::i18n()->getLocaleByUrl($matches[1]);
     }
 }
 protected function localizeStaticSlugs($localizedPost, $routedUrl, $originalUrl)
 {
     // Localize back the parameters in the default language
     if (!$this->currentLocale->isDefault()) {
         if (preg_match('/' . preg_quote($localizedPost->post_name) . '\\/(.+?)$/', $routedUrl, $matches)) {
             $cpt = CustomPostType::factoryFromKey($localizedPost->post_type);
             $key = "i18n." . $this->currentLocale->getCode() . ".rewrite.slug";
             if ($cpt->hasConfig($key)) {
                 $additionalParameters = Utility::replaceFirstOccurence($cpt->getConfig($key), $cpt->getConfig("rewrite.slug"), $additionalParameters);
             }
         }
     }
     return trailingslashit($originalUrl);
 }
 private function localizeDefaultSlug($model, $permalink)
 {
     $defaultSlug = $model->getConfig("rewrite.slug");
     if ($defaultSlug) {
         $slugs = array();
         foreach ($model->extractConfig("i18n.{s}.rewrite.slug") as $slug) {
             $slugs[] = $slug;
         }
         if (count($slugs)) {
             $permalink = preg_replace('#/(' . implode("|", $slugs) . ')/#', '/' . $defaultSlug . '/', $permalink);
         }
         if (!$this->currentLocale->isDefault() && $model->hasConfig("i18n." . $this->currentLocale->getCode() . ".rewrite.slug")) {
             return Utility::replaceFirstOccurence($defaultSlug, $model->getConfig("i18n." . $this->currentLocale->getCode() . ".rewrite.slug"), $permalink);
         }
     }
     return $permalink;
 }