Skip to content

Commit

Permalink
Merge branch 'release/3.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Oct 5, 2022
2 parents d504a91 + 4f9c6c1 commit 9cc3e33
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# v3.0.2
## 10/05/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

Expand Down
6 changes: 3 additions & 3 deletions blueprints.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
46 changes: 31 additions & 15 deletions langswitcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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();
Expand Down

0 comments on commit 9cc3e33

Please sign in to comment.