Skip to content
Grav 2.1 is out: every page speaks Markdown. Read the announcement →

How would I get the current language?

Started by Muut Archive 10 years ago · 6 replies · 2997 views
10 years ago

Hi,
How do I go about getting the current language? No just the country code 'en', 'de' but the actual name for example 'English' or 'Deutsch'.

I am using langswitcher, but can seem to find anything that can help. I have tried to use
'{{ grav.language.getLanguage }}' but this only shows the country code

10 years ago

Currently that is not available in Twig directly. That lookup is in the class LanguageCodes.php, but it currently can only be used by PHP, ie in a plugin or something. You could do this easily enough in your theme or plugin class:

I'm using Antimatter theme here for example:

PHP
<?php
namespace Grav\Theme;

use Grav\Common\Language\LanguageCodes;
use Grav\Common\Theme;

class Antimatter extends Theme
{
    public static function getSubscribedEvents()
    {
        return [
            'onTwigPageVariables' => ['onTwigVariables', 0],
            'onTwigSiteVariables' => ['onTwigVariables', 0]
        ];
    } 

    public function onTwigVariables()
    {
        $this->grav['twig']->twig_vars['language_codes'] = new LanguageCodes;
    }

}

Then in your page content (with Twig processing enabled) or Twig template you can use it like this:

TWIG
{{ language_codes.getName('pt') }}
10 years ago

@rhukster, thats handy to know, thanks very much :D i will take a look at the exisiting solution. How long until Grav 1.2 is released roughly?

10 years ago

@rhukster and how would i get the natvie name?

10 years ago

Its ok i worked it out :D - {{ language_codes.getnativeName(grav.language.getActive) }}

10 years ago

Yup, and Grav 1.2.0 should be release this week.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 2277 9 years ago
Archive · by Muut Archive, 9 years ago
2 1496 9 years ago
Archive · by Muut Archive, 9 years ago
2 4745 9 years ago
Archive · by Muut Archive, 9 years ago
1 3555 9 years ago
Archive · by Muut Archive, 9 years ago
3 1619 9 years ago