🚚 How to migrate to Kirby Tips & tutorials
Skip to content

Custom language variables

When working with a multi-language site, you often need language-specific variables/strings. Simple things like translated button labels for your contact form, for example, or other translatable parts which don't need to be modified by editors. For such cases, Kirby has a built-in language variable management.

Where to store language variables

Language variables can be stored in two places in Kirby:

  1. As an array of key/value pairs in your language definition files in /site/languages
  2. As an array of key/value pairs in a plugin using the translations extension

Those arrays can contain as many custom variables as you need.

Manually adding translations in language definition files

/site/languages/en.php
<?php

return [
  'code' => 'en',
  'default' => true,
  'direction' => 'ltr',
  'locale' => 'en',
  'name' => 'English',
  'url' => '/',
  'translations' => [
    'change' => 'Change',
    'confirm' => 'OK',
    'copy' => 'Copy',
    'create' => 'Create'
  ]
];
/site/languages/de.php
<?php

return [
  'code' => 'de',
  'default' => false,
  'direction' => 'ltr',
  'locale' => 'de_DE',
  'name' => 'Deutsch',
  'translations' => [
    'change' => 'Ändern',
    'confirm' => 'OK',
    'copy' => 'Kopieren',
    'create' => 'Erstellen'
  ]
];
Since 4.0.0

Language editor in the Panel

Once you have added one or more languages, you can start adding translation variables via the Language editor. Visit the language you want to edit. You can now add language variables as key/value pairs. These variables will also be stored in the language definition files.

Access to the language editor can be restricted via permissions

Language variables that you add in other languages than the default language will be stored in the default language as well. Language variables added in the default language will only be stored once translated.

If you stored your language variables in separate files as described in this cookbook recipe they will be read by the Panel, but then moved to the language definition files.

Core translation variables and translation variables from plugins are ignored and cannot be overwritten via the Panel.

Using language variables in your template

Those translated variables can be fetched in your template, plugins or snippets with the t() helper:

<input type="submit" value="<?php echo t('change') ?>" />

You can provide a fallback if the language variable is not defined:

<input type="submit" value="<?php echo t('change', 'Change') ?>" />

More information

Check out the following cookbook recipes to find out how to make language variables more flexible: