From 1452d9dbee6fd2179fa057254ebdb1599282059b Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Wed, 5 Oct 2022 06:28:39 -0600 Subject: [PATCH 1/2] added untranslated url caching per-page-route --- CHANGELOG.md | 7 +++++++ blueprints.yaml | 2 +- langswitcher.php | 46 +++++++++++++++++++++++++++++++--------------- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 12e530f..bcee11e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# v3.0.2 +## mm/dd/2022 + +1. [](#new) + * Require Grav `1.7.37` to make use of the new `Pages::getSimplePagesHash()` method + * Added caching to `translated_routes` so translation work is only performed on the first load, resulting in faster subsequent page loads + # v3.0.1 ## 08/19/2022 diff --git a/blueprints.yaml b/blueprints.yaml index b440a2a..28af80e 100644 --- a/blueprints.yaml +++ b/blueprints.yaml @@ -11,7 +11,7 @@ keywords: mulitlang, multilanguage, translation, switcher bugs: https://github.com/getgrav/grav-plugin-langswitcher/issues license: MIT dependencies: - - { name: grav, version: '>=1.7.0' } + - { name: grav, version: '>=1.7.37' } form: validation: strict diff --git a/langswitcher.php b/langswitcher.php index baa91a7..82f22eb 100644 --- a/langswitcher.php +++ b/langswitcher.php @@ -2,8 +2,10 @@ namespace Grav\Plugin; use Composer\Autoload\ClassLoader; +use Grav\Common\Cache; use Grav\Common\Language\Language; use Grav\Common\Language\LanguageCodes; +use Grav\Common\Page\Interfaces\PageInterface; use Grav\Common\Page\Page; use Grav\Common\Page\Pages; use \Grav\Common\Plugin; @@ -96,14 +98,24 @@ protected function getTranslatedUrl($lang, $path) */ public function onTwigSiteVariables() { - $data = new \stdClass; + /** @var PageInterface $page */ $page = $this->grav['page']; + + /** @var Pages $pages */ + $pages = $this->grav['pages']; + + /** @var Cache $cache */ + $cache = $this->grav['cache']; + + $data = new \stdClass; $data->page_route = $page->rawRoute(); if ($page->home()) { $data->page_route = '/'; } + $translated_cache_key = md5('translated_cache_key'.$data->page_route.$pages->getSimplePagesHash()); + $languages = $this->grav['language']->getLanguages(); $data->languages = $languages; @@ -129,24 +141,28 @@ public function onTwigSiteVariables() $active = $language->getActive() ?? $language->getDefault(); if ($this->config->get('plugins.langswitcher.translated_urls', true)) { - $data->translated_routes = array(); - $translate_langs = $data->languages; + $data->translated_routes = $cache->fetch($translated_cache_key); - if (($key = array_search($active, $translate_langs)) !== false) { - $data->translated_routes[$active] = $page->url(); - unset($translate_langs[$key]); - } + if ($data->translated_routes === false) { + $translate_langs = $data->languages; + + if (($key = array_search($active, $translate_langs)) !== false) { + $data->translated_routes[$active] = $page->url(); + unset($translate_langs[$key]); + } - foreach ($translate_langs as $lang) { - $data->translated_routes[$lang] = $this->getTranslatedUrl($lang, $page->path()); - if (is_null($data->translated_routes[$lang])) { - $data->translated_routes[$lang] = $data->page_route; + foreach ($translate_langs as $lang) { + $data->translated_routes[$lang] = $this->getTranslatedUrl($lang, $page->path()); + if (is_null($data->translated_routes[$lang])) { + $data->translated_routes[$lang] = $data->page_route; + } } + // Reset pages to current active language + $language->init(); + $language->setActive($active); + $this->grav['pages']->reset(); + $cache->save($translated_cache_key, $data->translated_routes); } - // Reset pages to current active language - $language->init(); - $language->setActive($active); - $this->grav['pages']->reset(); } $data->current = $language->getLanguage(); From 4f9c6c134272a3e7a080150c67ac6c4f374837f7 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Wed, 5 Oct 2022 11:26:39 -0600 Subject: [PATCH 2/2] prepare for release --- CHANGELOG.md | 2 +- blueprints.yaml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bcee11e..165780c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ # v3.0.2 -## mm/dd/2022 +## 10/05/2022 1. [](#new) * Require Grav `1.7.37` to make use of the new `Pages::getSimplePagesHash()` method diff --git a/blueprints.yaml b/blueprints.yaml index 28af80e..910baee 100644 --- a/blueprints.yaml +++ b/blueprints.yaml @@ -1,6 +1,6 @@ name: LangSwitcher -version: 3.0.1 -description: LangSwitcher is a [Grav](http://github.com/getgrav/grav) plugin that provides native language text links to switch between [multiple languages](http://learn.getgrav.org/content/multi-language) in Grav **v0.9.30** or greater. +version: 3.0.2 +description: LangSwitcher is a [Grav](https://github.com/getgrav/grav) plugin that provides native language text links to switch between [multiple languages](http://learn.getgrav.org/content/multi-language) in Grav **v0.9.30** or greater. icon: globe author: name: Team Grav