Git Product home page Git Product logo

grav-plugin-language-selector's Introduction

Grav Language Selector Plugin

Language Selector

Language Selector is a Grav plugin that provides native language selector with flags to switch between Multiple Languages.

Installation

Installing the Language Selector plugin can be done in one of two ways. 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 or GIT.

GPM Installation (Preferred)

The simplest way to install this plugin is via the Grav Package Manager (GPM) through your system's Terminal (also called the command line). From the root of your Grav install type:

bin/gpm install language-selector

This will install the Language Selector plugin into your /user/plugins directory within Grav. Its files can be found under /yoursite/user/plugins/language-selector.

Manual Installation (Download)

To install this plugin, just download the zip version of this repository and unzip it under user/plugins. Then, rename the folder to language-selector. You can find these files either on GitHub.

You should now have all the plugin files under

/yoursite/user/plugins/language-selector

Manual Installation (Using GIT)

In user/plugins folder, apply the following command:

git clone https://github.com/clemdesign/grav-plugin-language-selector language-selector

This will clone this repository into the language-selector folder.

Usage

1. Define the supported languages

In system.yaml, add the supported languages in languages.supported parameter.

Example:

languages:
  supported:
    - fr
    - en

2. Integration

You do need to add the included Twig partials template into your own theme somewhere you want the available languages to be displayed.

{% include 'partials/language-selector.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 language-selector.html.twig into the templates folder of your custom theme:

/yoursite/user/themes/custom-theme/templates/partials/language-selector.html.twig

You can now edit the override and tweak it however you prefer.

language-selector need jQuery (1.8 -> later) to display dropdown language menu.

3. 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/language-selector.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/fr" hreflang="fr" />
<link rel="alternate" href="http://example.com/zh-cn" hreflang="zh-cn" />

Configuration

Plugin

Simply copy the user/plugins/language-selector/language-selector.yaml into user/config/plugins/language-selector.yaml and make your modifications.

enabled: true
built_in_css: true
button_display: default
select_display: default

Options enabled and built_in_css are pretty self explanatory.

For button_display, this one define how button of selected language will be displayed. There are 3 possibilities:

  • default: Flag and Language name are displayed
  • flag: Only flag is displayed
  • name: Only language name is displayed

For select_display, this one define how language selector will be displayed. There are 3 possibilities:

  • default: Flag and Language name are displayed
  • flag: Only flag is displayed
  • name: Only language name is displayed

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

Contribute

Languages

For Administration panel, Language Selector is available in English and French. You could contribute for another languages.

For flags and language names, it is available for both match between flag images (content in language-selector/flags) and the list of language name in Grav/Common/Language/LanguageCodes.php.
You could contribute by adding new flags to match with list of language name, or the opposite.

Flag images are sized 24 x 12 pixels in PNG.

Credits

Language Selector is based on Lang Switcher plugin.

grav-plugin-language-selector's People

Contributors

clemdesign avatar pikant avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

grav-plugin-language-selector's Issues

Multi Site and Multi Language

Hi,

I am having issues with this plugin going to the correct link.

I have a grav site setup with multi-site. The subsites are what I am translating into multiple languages so it ends up like this:

Main site:
www.site.com
Subsite:
www.site.com/sub-site
Subsite Page(english):
www.site.com/sub-site/my-page
Subsite Page(french):
www.site.com/sub-site/my-page/fr

The translations work great. But when I use the plugin to switch language it directs me back to the parent site. So for example if I am viewing www.site.com/sub-site/my-page/fr and choose to change to English I get directed to: www.site.com/my-page/fr. Any tips on how I can get around this to make sure it gets the subsite included in the link it creates?

I tried editing the plugin template however I ended up with both languages directing to (fr).

Cheers,

Grav multilanguage site: Raw routes, redirects and slugs not translated: 2 Solutions for correct slugs

Hi there,

@clemdesign asked me to explain (and discuss) the problem with my multilanguage setup after I posted to closed topics regarding this problem.

First let me introduce the problem:

On a site with multilanguage setup and nested pages (help desk with collections of articles, nesting up to 5 levels) I tried to use the Grav langswitcher plugin as well clemdesigns language-selector. Both worked fine at a first glance. But then I realized that the URLs to the translated pages (as well as the href-lang-URLs) use the raw routes which are always built based on the default language. This in combination with 302 redirects is a base Grav functionality (which of course makes sense - but not always).

For example I configured

  • en
  • de

as languages of my Grav site. Then en is my default language. Every raw route is made up of this language.

Let's say we have a file structure like this (> indicates folder, slugs in brackets)

01.Home >
--home.de.md [start]
--home.en.md [home]
--01.Basic Knowledge >
----collection.de.md [grundlagen]
----collection.en.md [basics]
----01.Mail Fraud >
------article.de.md [mail_betrug]
------article.en.md [mail_fraud]

Ignore the different page types, it could all be article.

Assume an user on the Mail Fraud page.
Then the slugged URLs for that page would be:
en/home/basics/mail_fraud
de/start/grundlagen/mail_betrug

If you're using langswitcher or language-selector and since both they only retrieve the slug for the page requested (not the pages on the path between, instead the raw route is used there) then the routes used for switching the language would be:
(from en to de): de/home/basic%20knowledge/mail_betrug
(from de to en): en/home/basic%20knowledge/mail_fraud

As you can see they differ from their corresponding slugged routes (the routes Grav will redirect to). It works since home/basic%20knowledge is the raw route part and Grav redirects with a 302 to the slugged routes successfully.

  • Problem 1: User in different countries (and languages of course) always see these URLs written in the default language often with a lot of %20s in it. Yes they where redirected immediately. But it looks really ugly and such an URL is not self-explanatory.
  • More severe problem 2: Google doesn't like redirects.
  • Even more severe problem 3: I don't, either

Second let me tell you what I found around this topic:

  1. Grav knows about configured languages, of course, but during handling a page request Grav knows nothing about pages in languages different than the active page.
  2. It's not impossible but it involves a certain amount of work to let Grav have any idea of pages of all configured languages. And this would certainly have an impact on Grav's performance.
  3. There are solutions that handle this problem more ore less well. Rather less (with at least one exception).
  4. Nobody thinks about performance. Hello? Why not using WordPress then?

Third I have solutions:
Since I'm running out of time for now I will just outline the solutions to this problem and link to the corresponding solution. I will come back and give more detailed information here.

  1. Solution: Caching the slugged routes for each language. This is what I did. See getgrav/grav-plugin-langswitcher#39 (comment) for detailed information. This solution is really fast, especially on sites with high level of nesting and higher number of supported languages. But it has drawbacks, too. One drawback is that under certain circumstances the cache is out of date. But precaching fights against that drawback.

  2. Solution: As posted here by pamtbaau one could traverse up the path to retrieve all slugs. This is a good solution, too. Indeed this solution ensures the slugs are always up-to-date. But it has the drawback that on sites with a high level of nesting and a rather high number of languages this produces heavy page loading (not from cache but from disk) just to produce slugged URLs for the language menu.

  3. Solution (to be done): A smart combination of 1 + 2 ;)

dropdown menu invisible

The button displays in the upper right corner of the page. By selecting the button the dropdown is not opening. In HTML code all elements are available and by disabling the build-in CSS the button plus the dropdown menu is visible and functioning. An error message says 'user-select' declaration unknown and ignored. It looks that there is an issue with the JS-code.

Plugin name TWIG variable syntax problem

Hello,
First of all thank you very much for the developing this plugin.

In the real word use there is a problem with plugin name.
If you use the plugin name "language-selector" the PHP part of the plugin will work well, but the TWIG use of the plugin config variable will fail.

For example:

{% if config.plugins.language-selector.enabled %}
  {% include 'partials/language-selector.html.twig' %}
{% endif %}

This TWIG code will never include the plugin template because there is a syntax error in the TWIG array selector.

Only the 'config.plugins.languageselector.enabled' or the 'config.plugins.language_selector.enabled' variant will work from the TWIG side.

I have just tested it, and I had to rewrite the plugin without - in the plugin name to work properly with TWIG variables.

So, I suggest to change the name of this genius plugin for multi language GRAV sites.

Language switching is not always using the right slug

I have a page with subpages. The main page got a translated slug. Folder name is services and slug name (for NL) is 'diensten'

When I select that item when in the language NL it says /nl/diensten (which is fine)

When I am in English it says /en/services (which is fine too)

But when the item is the active item, and I switch from English to NL (Dutch) is says /nl/services (which is not fine). As I click the menu-item again, it says the url = /nl/diensten again (which is fine)

I don't know if this is a languageswitcher problem or a native Grav problem

does code for chinese traditional language exist ?

Hi,
I try to find the code to have traditional chinese in my choices of languages. But I can not find correct one.
I tried

  • ch
  • zh
  • cht
  • zh-ch
  • zh-cht
  • cmn

nothing works correctly for me. I mean by that some are simplified chinese and not traditional. zh-cht for example. cmn show just cmn and not Mandarin

Do you know where i can find the correct code please ?

ps: I don't need the flag as i see on the article

Grav Language selector and slug

I tried to set up a website with 2 languages using Grav/Gantry backend. One NL (default) and one EN. Only the EN page use slug. When opening pages using EN, I can see all the webpages with the right slug name as URL. I can easily switch to the NL pages. When trying to go back from NL to EN, it will not find the page as it will go to the naming of the page instead of the slug variant. Example:
Folder Name: matlessen
NL menu: Matlessen
EN menu: Mat Classes
EN Slug: matclass

When going from EN to NL, the url will go from ../en/matclass to ../nl/matlessen
When trying to move from NL to EN, the url try to find ../en/matlessen instead of the slug ../en/matclass

Is this an plugin issue ?
Thanks in advanced
Frank

language-selector.js:89 Uncaught TypeError: $(...).adddropdown is not a function

Hi,

I have a small problem with your plugins :

language-selector.js:89 Uncaught TypeError: $(...).adddropdown is not a function
at HTMLButtonElement. (language-selector.js:89)
at Function.each (jquery-2.x.min.js:2)
at n.fn.init.each (jquery-2.x.min.js:2)
at HTMLDocument. (language-selector.js:88)
at i (jquery-2.x.min.js:2)
at Object.fireWith [as resolveWith] (jquery-2.x.min.js:2)
at Function.ready (jquery-2.x.min.js:2)
at HTMLDocument.J (jquery-2.x.min.js:2)

Regard

Selector not working - selection menu will not show up

Hi. I've noticed that the selector no longer seems to work. It is set to one language (the one retrieved from environment in my case).

Current behavior:
When clicked on the selector, nothing happens.

Expected behavior:
When clicked on the selector, the roll-down menu with supported languages should appear.

Versions:
I am using Grav v1.7.42.3; Language Selector v1.1.0.

Tested on:
Firefox 115.0.2 (64 bit, windows 10)
Firefox 115.2.1 (Build #2015962859, Android 11)
Chrome 114.0.5735.196 (Android 11)
Edge 114.0.1823.82 (64 bit, Windows 10)

It worked on about 2 years old Firefox version, don't know which version. If requested I can retrieve the version in a few days.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.