public function __construct()
 {
     $i18n = Strata::i18n();
     $this->currentLocale = $i18n->getCurrentLocale();
     $this->defaultLocale = $i18n->getDefaultLocale();
     $this->textdomain = $i18n->getTextdomain();
 }
 public function __construct()
 {
     $i18n = Strata::i18n();
     $this->currentLocale = $i18n->getCurrentLocale();
     $this->defaultLocale = $i18n->getDefaultLocale();
     $this->shouldLocalizeByFallback = !$i18n->currentLocaleIsDefault() && $i18n->shouldFallbackToDefaultLocale();
 }
 public function filter_onInit()
 {
     $strataRewriter = Strata::rewriter();
     $i18n = Strata::i18n();
     $configuration = $i18n->getConfiguration();
     // Taxonomies
     $rewriter = new TaxonomyRewriter($i18n, $strataRewriter);
     $rewriter->setConfiguration($configuration);
     $rewriter->rewrite();
     // Custom Post Types
     $rewriter = new CustomPostTypeRewriter($i18n, $strataRewriter);
     $rewriter->setConfiguration($configuration);
     $rewriter->rewrite();
     // Translate homepages
     $rewriter = new HomepageRewriter($i18n, $strataRewriter);
     $rewriter->setDefaultHomepageId($i18n->query()->getDefaultHomepageId());
     $rewriter->rewrite();
     // Translate the default slugs
     $rewriter = new DefaultWordpressRewriter($i18n, $strataRewriter);
     $rewriter->setConfiguration($configuration);
     $rewriter->rewrite();
     $postPermalink = new PostPermalinkManager();
     add_filter('post_link', array($postPermalink, "filter_onCptLink"), 5, 2);
     add_filter('post_type_link', array($postPermalink, "filter_onCptLink"), 5, 2);
     add_filter('page_link', array($postPermalink, "filter_onPostLink"), 5, 2);
     $termPermalink = new TermPermalinkManager();
     add_filter('term_link', array($termPermalink, 'filter_onTermLink'), 5, 3);
     $metaManager = new PostMetaManager();
     add_action('save_post', array($metaManager, 'filter_onSavePost'), 100);
     $metaManager = new TermMetaManager();
     add_action('create_term', array($metaManager, 'filter_onCreateTerm'), 100);
     add_action('edit_term', array($metaManager, 'filter_onEditTerm'), 100);
 }
Пример #4
0
 /**
  * Adds a resourced based route possibility based on a custom
  * post type or taxonomy.
  * @param WordpressEntity $model
  */
 public function addResourcePossibility(WordpressEntity $model)
 {
     $slug = null;
     if (property_exists($model, "routed") && is_array($model->routed) && array_key_exists("controller", $model->routed)) {
         $controllerName = $model->routed['controller'];
         $controllerObject = new $controllerName();
         $controller = $controllerObject->getShortName();
     } else {
         $controller = Controller::generateClassName($model->getShortName());
     }
     $i18n = Strata::i18n();
     if ($i18n->isLocalized()) {
         $currentLocale = $i18n->getCurrentLocale();
         if ($currentLocale && !$currentLocale->isDefault()) {
             $slugInfo = $model->extractConfig("i18n." . $currentLocale->getCode() . ".rewrite.slug");
             $slug = array_pop($slugInfo);
             if (!is_null($slug)) {
                 $this->automatedRoutes[] = array('*', "/{$slug}/page/[i:pageNumber]/?", "{$controller}#index");
                 $this->automatedRoutes[] = array('*', "/{$slug}/[:slug]/?", "{$controller}#show");
                 $this->automatedRoutes[] = array('*', "/{$slug}/?", "{$controller}#index");
             }
         }
     }
     $slugInfo = $model->extractConfig("rewrite.slug");
     $slug = array_pop($slugInfo);
     if (is_null($slug)) {
         $slug = $model->getWordpressKey();
     }
     $this->automatedRoutes[] = array('*', "/{$slug}/page/[i:pageNumber]/?", "{$controller}#index");
     $this->automatedRoutes[] = array('*', "/{$slug}/[:slug]/?", "{$controller}#show");
     $this->automatedRoutes[] = array('*', "/{$slug}/?", "{$controller}#index");
 }
 /**
  * Returns the default term link to add the current locale prefix
  * to the generated link, if applicable.
  * @param  string $url
  * @param  WP_Term $term
  * @param  string $taxonomy
  * @return string
  */
 public function filter_onTermLink($url, WP_Term $term, $taxonomy)
 {
     $configuration = Strata::i18n()->getConfiguration();
     if ($configuration->isTaxonomyEnabled($taxonomy)) {
         return $this->generatePermalink($url, $term, $taxonomy);
     }
     return $url;
 }
 private function shouldRewriteModelSlug($model)
 {
     if (!$this->currentLocale->isDefault() || Strata::i18n()->shouldFallbackToDefaultLocale()) {
         if (is_a($model, "Strata\\Model\\WordpressEntity")) {
             return $model->hasConfig("i18n." . $this->currentLocale->getCode() . ".rewrite.slug");
         }
     }
     return false;
 }
 public static function localizeRouteByQuery($wp_query, $route = null)
 {
     if (is_null($route)) {
         $route = $_SERVER['REQUEST_URI'];
     }
     if ($wp_query->is_tax()) {
         $router = new TaxonomyRouter(Strata::i18n(), $wp_query);
     } else {
         $router = new PostRouter(Strata::i18n(), $wp_query);
     }
     return $router->localizeRoute($route);
 }
 private function translationExists()
 {
     $i18n = Strata::i18n();
     $translations = $i18n->query()->findTranlationsOfId($this->originalId, $this->originalKind);
     $targetLocale = $this->getTranslationLocale();
     foreach ($translations as $translation) {
         if ($targetLocale->getCode() === $translation->getTranslationLocaleCode()) {
             return true;
         }
     }
     return false;
 }
 protected function distributeTemplate()
 {
     foreach (Strata::i18n()->getLocales() as $locale) {
         if (!$locale->isDefault() && $locale->hasPostTranslation($this->post->ID)) {
             $originalTemplate = get_post_meta($this->post->ID, '_wp_page_template', true);
             if (!empty($originalTemplate)) {
                 $localization = $locale->getTranslatedPost($this->post->ID);
                 if ((int) $localization->ID !== (int) $this->post->ID) {
                     update_post_meta($localization->ID, '_wp_page_template', $originalTemplate);
                     if ($this->logger) {
                         $this->logger->log(sprintf("Synced post #%s with #%s's template.", $localization->ID, $this->post->ID), "<magenta>Polyglot</magenta>");
                     }
                 }
             }
         }
     }
 }
 public function onFilter_DropdownPages($output, $r, $pages)
 {
     if (!StrataRouter::isFrontendAjax()) {
         $currentLocale = Strata::i18n()->getCurrentLocale();
         if ($currentLocale->isDefault()) {
             $selectAsArray = explode("\n", $output);
             foreach ($pages as $idx => $page) {
                 $translation = $currentLocale->getTranslatedPost($page->ID);
                 if (is_null($translation) || (int) $translation->ID !== (int) $page->ID) {
                     $output = preg_replace('/<option.+?value="' . $page->ID . '".+?<\\/option>/', '', $output);
                 }
             }
             return $output;
         }
         return __("Parent page is determined by the default locale.", "polyglot");
     }
     return $output;
 }
 public function generatePermalink($permalink, $postId)
 {
     $postId = is_object($postId) ? $mixed->ID : $postId;
     if (wp_is_post_revision($postId)) {
         return $this->addLocaleHomeUrl($permalink);
     }
     $postAttempingToTranslate = get_post($postId);
     if (!$postAttempingToTranslate) {
         return $this->addLocaleHomeUrl($permalink);
     }
     // Translate the current post_name.
     $tree = Tree::grow($postAttempingToTranslate->ID, "WP_Post");
     if ($tree->isLocalized()) {
         $permalink = $this->translatePostName($postAttempingToTranslate, $permalink);
     }
     if ($this->currentLocale->hasPostTranslation($postAttempingToTranslate->ID)) {
         // Translate up the tree should the post have parents.
         $generationPointer = $postAttempingToTranslate;
         while ($generationPointer && (int) $generationPointer->post_parent > 0) {
             $parent = get_post($generationPointer->post_parent);
             $parentTree = Tree::grow($parent->ID, "WP_Post");
             if ($parentTree->isLocalized($parent->ID)) {
                 $permalink = $this->translatePostName($parent, $permalink);
                 $generationPointer = $parent;
             }
         }
         // Home urls should not display the post_name slug on translated versions.
         if (!$this->currentLocale->isDefault()) {
             $homepageId = Strata::i18n()->query()->getDefaultHomepageId();
             $tree = Tree::grow($homepageId, "WP_Post");
             $localizedHomePage = $tree->getLocalizationIn($this->currentLocale);
             if ($localizedHomePage && (int) $postAttempingToTranslate->ID === (int) $localizedHomePage->getObjectId()) {
                 $permalink = Utility::replaceFirstOccurence($localizedHomePage->getWordpressObject()->post_name . "/", "", $permalink);
             }
         }
     }
     // Translate the default Wordpress custom post type slug
     $model = $this->getStrataModel($postAttempingToTranslate);
     if (!is_null($model)) {
         $permalink = $this->localizeDefaultSlug($model, $permalink);
     }
     return $this->addLocaleHomeUrl($permalink);
 }
 protected function distributeFields()
 {
     $defaultLocale = Strata::i18n()->getDefaultLocale();
     $original = $defaultLocale->getTranslatedTerm($this->term->term_id, $this->term->taxonomy);
     $this->isWorking = true;
     if ($original) {
         foreach (Strata::i18n()->getLocales() as $locale) {
             if (!$locale->isDefault() && $locale->hasTermTranslation($original->term_id)) {
                 $localization = $locale->getTranslatedTerm($original->term_id, $original->taxonomy);
                 // Ensure we aren't getting a fallback post
                 if ((int) $localization->term_id !== (int) $original->term_id) {
                     wp_update_term($localization->term_id, $localization->taxonomy, array("parent" => $original->parent));
                     if ($this->logger) {
                         $this->logger->log(sprintf("Synced term #%s with #%s's parent information.", $localization->term_id, $original->term_id), "<magenta>Polyglot</magenta>");
                     }
                 }
             }
         }
     }
     $this->isWorking = false;
 }
Пример #13
0
 /**
  * Returns the original locale of a translated object.
  * @param  mixed $translatedObj
  * @return Locale
  */
 public function findObjectLocale($translatedObj)
 {
     $details = $this->findDetails($translatedObj);
     if (is_null($details)) {
         return Strata::i18n()->getDefaultLocale();
     }
     return Strata::i18n()->getLocaleByCode($details->getTranslationLocaleCode());
 }
 public function __construct($classes)
 {
     $this->classes = $classes;
     $this->currentLocale = Strata::i18n()->getCurrentLocale();
 }
 /**
  * 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]);
     }
 }
 private function generateTaxonomyTags($taxonomy)
 {
     $shouldFallback = (bool) Strata::config("i18n.default_locale_fallback");
     $defaultLocale = Strata::i18n()->getDefaultLocale();
     $currentLocale = Strata::i18n()->getCurrentLocale();
     $permalinkManager = new TermPermalinkManager();
     $currentPermalink = get_term_link($taxonomy, $taxonomy->taxonomy);
     // Keep the default url handy
     $defaultFallbackUrl = "";
     if ($shouldFallback) {
         $permalinkManager->enforceLocale($defaultLocale);
         $defaultFallbackUrl = $permalinkManager->generatePermalink($currentPermalink, $taxonomy, $taxonomy->taxonomy);
     }
     foreach (Strata::i18n()->getLocales() as $locale) {
         $permalinkManager->enforceLocale($locale);
         try {
             $localizedUrl = $permalinkManager->generatePermalink($currentPermalink, $taxonomy, $taxonomy->taxonomy);
             $destinationIsTheSame = $localizedUrl === $currentPermalink;
             $isNotDefaultButIsNotTheCurrent = !$locale->isDefault() && $locale->getCode() !== $currentLocale->getCode();
             if ($isNotDefaultButIsNotTheCurrent && $destinationIsTheSame && $shouldFallback) {
                 $this->alternates[] = sprintf('<link rel="alternate" hreflang="%s" href="%s">', $locale->getCode(), $defaultFallbackUrl);
                 $this->canonicals[] = sprintf('<link rel="canonical" href="%s">', $defaultFallbackUrl);
             } else {
                 $this->alternates[] = sprintf('<link rel="alternate" hreflang="%s" href="%s">', $locale->getCode(), $localizedUrl);
             }
         } catch (Exception $e) {
         }
     }
 }
 /**
  * Executed before each controller requests.
  */
 public function before()
 {
     $this->i18n = Strata::i18n();
     $this->view->set("i18n", $this->i18n);
 }
Пример #18
0
 public static function getLocaleUrls()
 {
     return array_map(function ($locale) {
         return $locale->getUrl();
     }, Strata::i18n()->getLocales());
 }
 public function getTranslationLocale()
 {
     return Strata::i18n()->getLocaleByCode($this->translation_locale);
 }
Пример #20
0
 /**
  * Generates the path of where the view localized in the current locale would
  * be located.
  * @return string
  */
 protected function generateLocalizedViewPath()
 {
     return $this->generateDefaultViewPath() . "." . Strata::i18n()->getCurrentLocaleCode();
 }
Пример #21
0
 public function generateTranslationFiles(Locale $locale, $activeTranslations = null)
 {
     $envPoFile = $locale->getPoFilePath(WP_ENV);
     $poFile = $locale->getPoFilePath();
     $moFile = $locale->getMoFilePath();
     if (is_null($activeTranslations)) {
         $activeTranslations = $locale->hasPoFile() ? Translations::fromPoFile($locale->getPoFilePath()) : new Translations();
     }
     // Add local modifications to the default set should there
     // be any.
     if ($locale->hasPoFile(WP_ENV)) {
         $this->hardTranslationSetMerge($locale, Translations::fromPoFile($envPoFile), $activeTranslations);
     }
     $textDomain = Strata::i18n()->getTextdomain();
     $activeTranslations->setDomain($textDomain);
     $activeTranslations->setHeader('Language', $locale->getCode());
     $activeTranslations->setHeader('Text Domain', $textDomain);
     $activeTranslations->setHeader('X-Domain', $textDomain);
     @unlink($poFile);
     @unlink($moFile);
     $activeTranslations->toPoFile($poFile);
     $activeTranslations->toMoFile($moFile);
 }
Пример #22
0
 protected function extractDefaultLocaleInformation()
 {
     $defaultLocale = null;
     if ($this->isLocalizedApp()) {
         $defaultLocale = Strata::i18n()->getDefaultLocale();
     }
     if ($this->modelSupportsRewrites()) {
         foreach ($this->model->routed['rewrite'] as $routeKey => $routeUrl) {
             $this->queueRewrite($routeUrl, $this->defaultSlug, $defaultLocale);
         }
     }
 }
Пример #23
0
 /**
  * Saves the list of $translation to the $locale.
  * @param  Locale       $locale
  * @param  Translations $translation
  */
 private function addGettextEntriesToLocale(Locale $locale, Translations $translations)
 {
     $defaultTranslations = $locale->hasPoFile() ? Translations::fromPoFile($locale->getPoFilePath()) : new Translations();
     $i18n = Strata::i18n();
     // it looks reversed to merge defaults into the found strings, but it's the
     // most efficient way of keeping existing translations
     $i18n->hardTranslationSetMerge($locale, $defaultTranslations, $translations);
     $i18n->generateTranslationFiles($locale, $translations);
 }
 public function getTermsArgs($args, $taxonomies)
 {
     if (!$this->taxonomyGroupIsSupported($taxonomies) || $this->wordpressIsCaching($args)) {
         return $args;
     }
     $notIn = array();
     $i18n = Strata::i18n();
     $locales = $i18n->getLocales();
     $shouldFallbackToDefault = $i18n->shouldFallbackToDefaultLocale();
     foreach ($taxonomies as $taxonomy) {
         // Remove references to the original object
         $currentTranslations = $this->query->findLocaleTranslations($this->currentLocale, "Term", $taxonomy);
         foreach ($currentTranslations as $translationEntity) {
             $notIn[] = $translationEntity->getOriginalObjectId();
         }
         // Remove translations in other locales
         $otherTranslations = array();
         foreach ($locales as $locale) {
             if ($locale->getCode() !== $this->currentLocale->getCode()) {
                 if (!$locale->isDefault() && !$this->currentLocale->isDefault()) {
                     $otherTranslations += $this->query->findLocaleTranslations($locale, "Term", $taxonomy);
                 }
             }
         }
         if (count($otherTranslations)) {
             foreach ($otherTranslations as $translationEntity) {
                 $notIn[] = $translationEntity->getObjectId();
             }
         }
     }
     if (count($notIn)) {
         $args['exclude'] = $notIn;
     }
     return $args;
 }