Grav Multi Language - Grav

How to add multi-language functionality

Let us see how to use different languages in Grav website. Grav looks for .md file for the representation of any page. While enabling multi-language support, Grav looks for the language file like or

How to connfigure the language

Let us set up some basic language configuration in our user/config/system.yaml file first.
By doing do, you have enabled multi–language support in Grav. The site will now support English (en) and French (fr). Here the default language is English. If you write Fr first, then that becomes your default language.

Multiple Language Pages

We have already seen that Grav looks for the appropriate language file. If you have set English as the default language, then Grav will look for file. If it is not found, then it looks for another language you have set. If both the languages are not found, then it looks for the file.
Example file
For French as default language, the file will be −

Active Language via URL

If you want to update a URL to your website with a language code, then type the below line in your browser for English−

and for French

Active Language via Browser

Grav has the ability to get the http_accept_language value and compare them to present supported language. Enable this function if you want in your user/system.yaml file in the language section as −

Language-Based Homepage

To have a language based home page, you must enable the following code in your site.yaml file −
In this way, Grav will find out which language to use from the active languages.
The following code will force Grav to redirect you to your default language route. And the include_route option forces to add the language code in your url like

Language-Based Twig Templates

If your file is, then Grav will look for a twig file as default.html.twig. When you need a language–specific twig file, then you must upload it at the root level of the language folder. If your present theme is in templates/default.html.twig you must create a templates/en/ folder and place your English-specific folder in it as: templates/en/default.html.twig

Language Switcher

Language switcher plugin is available at Grav Package Manager (GPM).

Translations via Twig

Use twig filter or t() function as both function alike. If you have another twig file, then it lets you to translate from an array.

Plugin and Theme Language Translations

Provide your translations in plugins and themes by creating a languages.yaml file in the root of your theme or plugin (/user/plugins/error/languages.yaml) and must contain all the supported languages.

Translation Overrides

If you want to override translation, then you must put the value pair in the language file in your the user/languages/ folder.


Environment – Based Language Handling
It is possible to route users to the correct version of your site according to URL. If your site url is, an alias for your standard, then you can create a configuration as /user/
It uses inverted language order. In the above code, fr is the default language. If you change the order by keeping en at the top and fr at the bottom, then en becomes the default language.

Language Alias Routes

Switching between different language versions of the same page can be done using the Page.rawRoute() method on your page object. It gets the same raw route for different language translations of a single page. Put the language code in the front to get a proper route.

If you are on page in French with a custom route of −
English page has the custom route of −
You get the raw page of the French page and that might be −
Then just add the language you want which will be your new URL.

Translations Support

Grav provides simple mechanism for providing translations in Twig via PHP to be used in themes and plugins. It is enabled by default and uses en language if no specific language is defined. To enable or disable, go to system.yaml file and make the changes.
You can provide translations in many ways and different places. The first place is system/languages folder. Files must be created in en.yaml, fr.yaml, etc. format. Each yaml file must consist an array or nested arrays of key pairs.

Session Based Active Language

You can activate session-based storage of the active language. To enable you must have session : enables : true in system.yaml and enable language setting.

Language Switcher

Install a language switching plugin from GPM.

Setup with language specific domains

Have Environment based language handling configuration to assign default languages. Add this option to your system.yaml; it must be set to true.

Add the following to your .htaccess file and pick the language slugs and domain names according to your requirements.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

Grav Topics