Skip to content

Commit

Permalink
Merge branch 'release/3.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Aug 19, 2022
2 parents a9c5d1a + 50b3d1c commit e28c286
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 58 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# v3.0.0
## 08/19/2022

1. [](#new)
* Completely rewrote the logic for translated URLs to be more robust.
* Added configuration option to use **Translated URLs** or use previous **Raw-Route** approach
1. [](#improved)
* Updated `hreflang` Twig template to use new translated URLs logic
* Added an `x-default` entry for `hreflang` template when default language has `include_default_lang` set to false
* Support `params` and `query` string parameters in URLs
* Full domain URLs for `hreflang` entries

# v2.0.1
## 08/04/2022

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ Simply copy the `user/plugins/langswitcher/langswitcher.yaml` into `user/config/
```yaml
enabled: true
built_in_css: true
translated_urls: true
untranslated_pages_behavior: none
language_display: long
```
Options are pretty self explanatory.
Expand Down
14 changes: 13 additions & 1 deletion blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: LangSwitcher
version: 2.0.1
version: 3.0.0
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.
icon: globe
author:
Expand Down Expand Up @@ -39,6 +39,18 @@ form:
validate:
type: bool

translated_urls:
type: toggle
label: PLUGIN_LANGSWITCHER.TRANSLATED_URLS
help: PLUGIN_LANGSWITCHER.TRANSLATED_URLS_HELP
highlight: 1
default: 1
options:
1: PLUGIN_ADMIN.ENABLED
0: PLUGIN_ADMIN.DISABLED
validate:
type: bool

language_display:
type: select
size: small
Expand Down
72 changes: 39 additions & 33 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\Language\Language;
use Grav\Common\Language\LanguageCodes;
use Grav\Common\Page\Page;
use Grav\Common\Page\Pages;
use \Grav\Common\Plugin;

class LangSwitcherPlugin extends Plugin
Expand Down Expand Up @@ -72,36 +74,21 @@ public function onTwigTemplatePaths()
*/
protected function getTranslatedUrl($lang, $path)
{
$translated_url_parts = array();
/** @var Language $language */
$url = null;
/** @var Pages $pages */
$pages = $this->grav['pages'];
/** @var Language $language */
$language = $this->grav['language'];

$language->init();
$language->setActive($lang);
$pages->reset();
$page = $pages->get($path);
$current_node = $page;
$max_recursions = 10;
while ($max_recursions > 0 && $current_node !== null && $current_node->slug() != 'pages' && $path != 'pages') {
$translated_md_filepath = "{$path}/{$current_node->template()}.{$lang}.md";
if (file_exists($translated_md_filepath)) {
$translated_page = new Page();
$translated_page->init(new \SplFileInfo($translated_md_filepath));
$translated_slug = $translated_page->slug();
if (!empty($translated_slug)) {
array_unshift($translated_url_parts, $translated_slug);
} else {
$untranslated_slug = $current_node->slug();
if (!empty($untranslated_slug)) {
array_unshift($translated_url_parts, $untranslated_slug);
}
}
$current_node = $current_node->parent();
$path = dirname($path);
}
$max_recursions--;
}
if (!empty($translated_url_parts)) {
array_unshift($translated_url_parts, '');
return implode('/', $translated_url_parts);
} else {
return '';
if ($page) {
$url = $page->url();
}
return $url;
}

/**
Expand All @@ -126,6 +113,9 @@ public function onTwigSiteVariables()
$translated_pages[$language] = null;
$page_name_without_ext = substr($page->name(), 0, -(strlen($page->extension())));
$translated_page_path = $page->path() . DS . $page_name_without_ext . '.' . $language . '.md';
if (!file_exists($translated_page_path) and $language == $this->grav['language']->getDefault()) {
$translated_page_path = $page->path() . DS . $page_name_without_ext . '.md';
}
if (file_exists($translated_page_path)) {
$translated_page = new Page();
$translated_page->init(new \SplFileInfo($translated_page_path), $language . '.md');
Expand All @@ -135,15 +125,31 @@ public function onTwigSiteVariables()
$data->translated_pages = $translated_pages;
}

$data->translated_routes = array();
foreach ($data->languages as $language) {
$data->translated_routes[$language] = $this->getTranslatedUrl($language, $page->path());
if (empty($data->translated_routes[$language])) {
$data->translated_routes[$language] = $data->page_route;
$language = $this->grav['language'];
$active = $language->getActive() ?? $language->getDefault();

if ($this->config->get('plugins.langswitcher.translated_urls', true)) {
$data->translated_routes = array();
$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;
}
}
// Reset pages to current active language
$language->init();
$language->setActive($active);
$this->grav['pages']->reset();
}

$data->current = $this->grav['language']->getLanguage();
$data->current = $language->getLanguage();

$this->grav['twig']->twig_vars['langswitcher'] = $this->grav['langswitcher'] = $data;

Expand Down
1 change: 1 addition & 0 deletions langswitcher.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
enabled: true
built_in_css: true
translated_urls: true
untranslated_pages_behavior: none
language_display: long
2 changes: 2 additions & 0 deletions languages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ en:
LANGUAGE_DISPLAY_HELP: 'The format of the language display, either "long" (e.g. English), or "short" (e.g. EN)'
LANGUAGE_DISPLAY_LONG: 'Long'
LANGUAGE_DISPLAY_SHORT: 'Short'
TRANSLATED_URLS: 'Translated URLs'
TRANSLATED_URLS_HELP: 'Use the actual translated page URL rather then the raw-route'

ru:
PLUGIN_LANGSWITCHER:
Expand Down
23 changes: 15 additions & 8 deletions templates/partials/langswitcher.hreflang.html.twig
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
{% set langobj = grav['language'] %}
{% for key in langswitcher.languages %}
{% if key == langswitcher.current %}
{% set lang_url = page.url is same as('/') ? '' : page.url %}
{% else %}
{% set lang_url = (langobj.getLanguageURLPrefix(key) ~ langswitcher.page_route ~ page.urlExtension ?: '')|rtrim('/') %}
{% endif %}
<link rel="alternate" hreflang="{{ key }}" href="{{ uri.base ~ lang_url ~ uri.params }}" />
{% set language_obj = grav.language %}
{% for language in langswitcher.languages %}
{% if langswitcher.translated_routes[language] %}
{% set lang_url = langswitcher.translated_routes[language] ~ page.urlExtension %}
{% else %}
{% set base_lang_url = base_url_simple ~ grav.language.getLanguageURLPrefix(language) %}
{% set lang_url = base_lang_url ~ langswitcher.page_route ~ page.urlExtension %}
{% endif %}

{% set href_url = (url('/', true)|trim('/','right') ~ lang_url ~ uri.params ~ (uri.query|length > 1 ? '?' ~ uri.query)) %}

{% if (language_obj.default == language and config.languages.include_default_lang == false) %}
<link rel="alternate" hreflang="x-default" href="{{ href_url }}" />
{% endif %}
<link rel="alternate" hreflang="{{ language }}" href="{{ href_url }}" />
{% endfor %}
34 changes: 18 additions & 16 deletions templates/partials/langswitcher.html.twig
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
<ul class="langswitcher">
{% set display_format = display_format ?? config.get('plugins.langswitcher.language_display', 'long') %}
{% for language in langswitcher.languages %}
{% set display_format = display_format ?? config.get('plugins.langswitcher.language_display', 'long') %}

{% set show_language = true %}
{% if language == langswitcher.current %}
{% set lang_url = page.url %}
{% set active_class = ' active' %}
{% else %}
{% set base_lang_url = base_url_simple ~ grav.language.getLanguageURLPrefix(language) %}
{% set lang_url = base_lang_url ~ langswitcher.translated_routes[language] ~ page.urlExtension %}
{% for language in langswitcher.languages %}
{% set active_class = '' %}
{% set show_language = true %}
{% if language == langswitcher.current %}
{% set active_class = 'active' %}
{% endif %}
{% if langswitcher.translated_routes[language] %}
{% set lang_url = langswitcher.translated_routes[language] ~ page.urlExtension %}
{% else %}
{% set base_lang_url = base_url_simple ~ grav.language.getLanguageURLPrefix(language) %}
{% set lang_url = base_lang_url ~ langswitcher.page_route ~ page.urlExtension %}
{% endif %}
{% set untranslated_pages_behavior = grav.config.plugins.langswitcher.untranslated_pages_behavior %}
{% if untranslated_pages_behavior != 'none' %}
{% set translated_page = langswitcher.translated_pages[language] %}
{% if (not translated_page) or (not translated_page.published) %}
{% if untranslated_pages_behavior == 'redirect' %}
{% set lang_url = base_lang_url ~ '/' %}
{% set lang_url = url('/') %}
{% elseif untranslated_pages_behavior == 'hide' %}
{% set show_language = false %}
{% endif %}
{% endif %}
{% endif %}
{% set active_class = '' %}
{% endif %}

{% if show_language %}
<li><a href="{{ lang_url ~ uri.params }}" class="external{{ active_class }}">{% include 'partials/langswitcher-' ~ display_format ~ '.html.twig' %}</a></li>
{% endif %}
{% if show_language %}
<li><a href="{{ lang_url ~ uri.params ~ (uri.query|length > 1 ? '?' ~ uri.query) }}" class="external {{ active_class }}">{% include 'partials/langswitcher-' ~ display_format ~ '.html.twig' %}</a></li>
{% endif %}

{% endfor %}
{% endfor %}
</ul>

0 comments on commit e28c286

Please sign in to comment.