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();
 }
 private function replaceLocaleHomeUrl($permalink)
 {
     if (preg_match('#' . Utility::getLocaleUrlsRegex() . '#', $permalink)) {
         $permalink = preg_replace('#(/(' . Utility::getLocaleUrlsRegex() . ')/)#', '/', $permalink);
     }
     if ($this->currentLocale->hasACustomUrl()) {
         return Utility::replaceFirstOccurence(get_home_url() . '/', $this->currentLocale->getHomeUrl(), $permalink);
     }
 }
 /**
  * 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]);
     }
 }