Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

How would I get the current language?

Started by Muut Archive 9 years ago · 6 replies · 2825 views
9 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

9 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') }}
9 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?

9 years ago

@rhukster and how would i get the natvie name?

9 years ago

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

9 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 1333 9 years ago
Archive · by Muut Archive, 9 years ago
2 924 9 years ago
Archive · by Muut Archive, 9 years ago
2 4055 9 years ago
Archive · by Muut Archive, 9 years ago
1 2933 9 years ago
Archive · by Muut Archive, 9 years ago
3 1110 9 years ago