first commit
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
# v1.4.0
|
||||
## 06/29/2017
|
||||
|
||||
1. [](#new)
|
||||
* Added the `untranslated_pages_behavior` option to determine what to do with a language link when the current page doesn't exist in that language or it exists but it's not published
|
||||
1. [](#bugfix)
|
||||
* Fixed generated URLs when `append_url_extension` is set, via PR [#22](https://github.com/getgrav/grav-plugin-langswitcher/pull/22)
|
||||
|
||||
# v1.3.0
|
||||
## 02/17/2017
|
||||
|
||||
1. [](#new)
|
||||
* Added support for `hreflang` annotations via PR [#19](https://github.com/getgrav/grav-plugin-langswitcher/pull/19)
|
||||
|
||||
# v1.2.1
|
||||
## 05/28/2016
|
||||
|
||||
1. [](#bugfix)
|
||||
* Display all language names, even those with non supported locales
|
||||
|
||||
# v1.2.0
|
||||
## 05/03/2016
|
||||
|
||||
1. [](#improved)
|
||||
* Take URI parameters into account when switching languages
|
||||
* Add `external` class to avoid problems on modular pages when `jquery.singlePageNav` is loaded
|
||||
|
||||
# v1.1.0
|
||||
## 10/15/2015
|
||||
|
||||
1. [](#improved)
|
||||
* Added active class to language links
|
||||
|
||||
# v1.0.2
|
||||
## 07/13/2015
|
||||
|
||||
1. [](#improved)
|
||||
* Improved homepage routing
|
||||
|
||||
# v1.0.1
|
||||
## 07/08/2015
|
||||
|
||||
1. [](#improved)
|
||||
* Updated blueprints with some typo fixes
|
||||
|
||||
# v1.0.0
|
||||
## 07/08/2015
|
||||
|
||||
1. [](#new)
|
||||
* ChangeLog started...
|
||||
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Grav
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,92 @@
|
||||
# Grav LangSwitcher Plugin
|
||||
|
||||

|
||||
|
||||
`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 **0.9.30** or greater.
|
||||
|
||||
# Installation
|
||||
|
||||
Installing the LangSwitcher plugin can be done in one of two ways. Our GPM (Grav Package Manager) installation method enables you to quickly and easily install the plugin with a simple terminal command, while the manual method enables you to do so via a zip file.
|
||||
|
||||
## GPM Installation (Preferred)
|
||||
|
||||
The simplest way to install this plugin is via the [Grav Package Manager (GPM)](http://learn.getgrav.org/advanced/grav-gpm) through your system's Terminal (also called the command line). From the root of your Grav install type:
|
||||
|
||||
bin/gpm install langswitcher
|
||||
|
||||
This will install the LangSwitcher plugin into your `/user/plugins` directory within Grav. Its files can be found under `/your/site/grav/user/plugins/langswitcher`.
|
||||
|
||||
## Manual Installation
|
||||
|
||||
To install this plugin, just download the zip version of this repository and unzip it under `/your/site/grav/user/plugins`. Then, rename the folder to `langswitcher`. You can find these files either on [GitHub](https://github.com/getgrav/grav-plugin-langswitcher) or via [GetGrav.org](http://getgrav.org/downloads/plugins#extras).
|
||||
|
||||
You should now have all the plugin files under
|
||||
|
||||
/your/site/grav/user/plugins/langswitcher
|
||||
|
||||
# Usage
|
||||
|
||||
The `langswitcher` plugin doesn't require any configuration. You do however need to add the included Twig partials template into your own theme somewhere you want the available languages to be displayed.
|
||||
|
||||
```
|
||||
{% include 'partials/langswitcher.html.twig' %}
|
||||
```
|
||||
|
||||
Something you might want to do is to override the look and feel of the langswitcher, and with Grav it is super easy.
|
||||
|
||||
Copy the template file [langswitcher.html.twig](templates/partials/langswitcher.html.twig) into the `templates` folder of your custom theme:
|
||||
|
||||
```
|
||||
/your/site/grav/user/themes/custom-theme/templates/partials/langswitcher.html.twig
|
||||
```
|
||||
|
||||
You can now edit the override and tweak it however you prefer.
|
||||
|
||||
## Usage of the `hreflang` partial
|
||||
|
||||
A second template is available for `hreflang` annotations in the header of the page. In order to emit language annotations for the available languages of a page you need to add the corrsponding Twig partial template into the `<head>` section of your page, which can typically be found in `base.html.twig`:
|
||||
|
||||
```
|
||||
{% include 'partials/langswitcher.hreflang.html.twig' %}
|
||||
```
|
||||
|
||||
This will generate something like:
|
||||
|
||||
```
|
||||
<link rel="alternate" href="http://example.com/en" hreflang="en" />
|
||||
<link rel="alternate" href="http://example.com/de" hreflang="de" />
|
||||
<link rel="alternate" href="http://example.com/zh-cn" hreflang="zh-cn" />
|
||||
```
|
||||
|
||||
# Updating
|
||||
|
||||
As development for the LangSwitcher plugin continues, new versions may become available that add additional features and functionality, improve compatibility with newer Grav releases, and generally provide a better user experience. Updating LangSwitcher is easy, and can be done through Grav's GPM system, as well as manually.
|
||||
|
||||
## GPM Update (Preferred)
|
||||
|
||||
The simplest way to update this plugin is via the [Grav Package Manager (GPM)](http://learn.getgrav.org/advanced/grav-gpm). You can do this with this by navigating to the root directory of your Grav install using your system's Terminal (also called command line) and typing the following:
|
||||
|
||||
bin/gpm update langswitcher
|
||||
|
||||
This command will check your Grav install to see if your LangSwitcher plugin is due for an update. If a newer release is found, you will be asked whether or not you wish to update. To continue, type `y` and hit enter. The plugin will automatically update and clear Grav's cache.
|
||||
|
||||
> Note: Any changes you have made to any of the files listed under this directory will also be removed and replaced by the new set. Any files located elsewhere (for example a YAML settings file placed in `user/config/plugins`) will remain intact.
|
||||
|
||||
## Configuration
|
||||
|
||||
Simply copy the `user/plugins/langswitcher/langswitcher.yaml` into `user/config/plugins/langswitcher.yaml` and make your modifications.
|
||||
|
||||
```
|
||||
enabled: true
|
||||
built_in_css: true
|
||||
```
|
||||
|
||||
Options are pretty self explanatory.
|
||||
|
||||
## Redirecting after switching language
|
||||
|
||||
To have Grav redirect to the default page route after switching language, you must add the following configuration to `user/config/system.yaml`
|
||||
```
|
||||
pages:
|
||||
redirect_default_route: true
|
||||
```
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 38 KiB |
@@ -0,0 +1,47 @@
|
||||
name: LangSwitcher
|
||||
version: 1.4.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:
|
||||
name: Team Grav
|
||||
email: devs@getgrav.org
|
||||
url: http://getgrav.org
|
||||
homepage: https://github.com/getgrav/grav-plugin-langswitcher
|
||||
keywords: mulitlang, multilanguage, translation, switcher
|
||||
bugs: https://github.com/getgrav/grav-plugin-langswitcher/issues
|
||||
license: MIT
|
||||
|
||||
form:
|
||||
validation: strict
|
||||
fields:
|
||||
enabled:
|
||||
type: toggle
|
||||
label: Plugin status
|
||||
highlight: 1
|
||||
default: 1
|
||||
options:
|
||||
1: Enabled
|
||||
0: Disabled
|
||||
validate:
|
||||
type: bool
|
||||
|
||||
built_in_css:
|
||||
type: toggle
|
||||
label: Use built in CSS
|
||||
highlight: 1
|
||||
default: 1
|
||||
options:
|
||||
1: Enabled
|
||||
0: Disabled
|
||||
validate:
|
||||
type: bool
|
||||
|
||||
untranslated_pages_behavior:
|
||||
type: select
|
||||
label: Untranslated pages behavior
|
||||
help: Determine what to do with a language link when the current page doesn't exist in that language or it exists but it's not published.
|
||||
default: none
|
||||
options:
|
||||
none: Show language (default)
|
||||
redirect: Show language, link to home route
|
||||
hide: Hide language
|
||||
@@ -0,0 +1,22 @@
|
||||
.langswitcher {
|
||||
position: relative;
|
||||
top: 50%;
|
||||
-webkit-transform: translateY(-50%);
|
||||
-moz-transform: translateY(-50%);
|
||||
-o-transform: translateY(-50%);
|
||||
-ms-transform: translateY(-50%);
|
||||
transform: translateY(-50%);
|
||||
margin-left: 1rem !important;
|
||||
display: inline-block;
|
||||
}
|
||||
|
||||
.langswitcher li {
|
||||
display: inline-block;
|
||||
margin-left: 0.5rem;
|
||||
line-height: 1rem;
|
||||
}
|
||||
|
||||
.langswitcher .active {
|
||||
font-weight: bold;
|
||||
text-decoration: underline;
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
namespace Grav\Plugin;
|
||||
|
||||
use Grav\Common\Language\LanguageCodes;
|
||||
use Grav\Common\Page\Page;
|
||||
use \Grav\Common\Plugin;
|
||||
|
||||
class LangSwitcherPlugin extends Plugin
|
||||
{
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return [
|
||||
'onPluginsInitialized' => ['onPluginsInitialized', 0]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize configuration
|
||||
*/
|
||||
public function onPluginsInitialized()
|
||||
{
|
||||
if ($this->isAdmin()) {
|
||||
$this->active = false;
|
||||
return;
|
||||
}
|
||||
|
||||
$this->enable([
|
||||
'onTwigInitialized' => ['onTwigInitialized', 0],
|
||||
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
|
||||
'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
|
||||
]);
|
||||
}
|
||||
|
||||
/** Add the native_name function */
|
||||
public function onTwigInitialized()
|
||||
{
|
||||
$this->grav['twig']->twig()->addFunction(
|
||||
new \Twig_SimpleFunction('native_name', function($key) {
|
||||
return LanguageCodes::getNativeName($key);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add current directory to twig lookup paths.
|
||||
*/
|
||||
public function onTwigTemplatePaths()
|
||||
{
|
||||
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
|
||||
}
|
||||
|
||||
/**
|
||||
* Set needed variables to display Langswitcher.
|
||||
*/
|
||||
public function onTwigSiteVariables()
|
||||
{
|
||||
$data = new \stdClass;
|
||||
|
||||
$page = $this->grav['page'];
|
||||
$data->page_route = $page->rawRoute();
|
||||
if ($page->home()) {
|
||||
$data->page_route = '/';
|
||||
}
|
||||
|
||||
$languages = $this->grav['language']->getLanguages();
|
||||
$data->languages = $languages;
|
||||
|
||||
if ($this->config->get('plugins.langswitcher.untranslated_pages_behavior') !== 'none') {
|
||||
$translated_pages = [];
|
||||
foreach ($languages as $language) {
|
||||
$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)) {
|
||||
$translated_page = new Page();
|
||||
$translated_page->init(new \SplFileInfo($translated_page_path), $language . '.md');
|
||||
$translated_pages[$language] = $translated_page;
|
||||
}
|
||||
}
|
||||
$data->translated_pages = $translated_pages;
|
||||
}
|
||||
|
||||
$data->current = $this->grav['language']->getLanguage();
|
||||
|
||||
$this->grav['twig']->twig_vars['langswitcher'] = $data;
|
||||
|
||||
if ($this->config->get('plugins.langswitcher.built_in_css')) {
|
||||
$this->grav['assets']->add('plugin://langswitcher/css/langswitcher.css');
|
||||
}
|
||||
}
|
||||
|
||||
public function getNativeName($code) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
enabled: true
|
||||
built_in_css: true
|
||||
untranslated_pages_behavior: none
|
||||
@@ -0,0 +1,9 @@
|
||||
{% set langobj = grav['language'] %}
|
||||
{% for key in langswitcher.languages %}
|
||||
{% if key == langswitcher.current %}
|
||||
{% set lang_url = page.url %}
|
||||
{% else %}
|
||||
{% set lang_url = base_url_simple ~ langobj.getLanguageURLPrefix(key) ~ langswitcher.page_route ~ page.urlExtension ?: '/' %}
|
||||
{% endif %}
|
||||
<link rel="alternate" hreflang="{{ key }}" href="{{ lang_url ~ uri.params }}" />
|
||||
{% endfor %}
|
||||
@@ -0,0 +1,30 @@
|
||||
<ul class="langswitcher">
|
||||
{% for language in langswitcher.languages %}
|
||||
|
||||
{% 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.page_route ~ page.urlExtension %}
|
||||
{% 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 ~ '/' %}
|
||||
{% 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 }}">{{ native_name(language)|capitalize }}</a></li>
|
||||
{% endif %}
|
||||
|
||||
{% endfor %}
|
||||
</ul>
|
||||
Reference in New Issue
Block a user