Translations are carried out through simple dictionary files, in either JSON or Yaml formats.
Under the hood, the 18next
library is used to perform the translations.
On each request, a req.t()
function is available to perform translations in code.
On each template, a t()
function is available to perform translations within templates.
You can specify as many dictionary folders as you wish, and they must each match this structure:
/my-folder
/en
/file1.yaml
/file2.json
...
/cy
/file1.yaml
/file.json
...
Yaml is probably a better choice as it's less verbose and supports multiline content.
Each file corresponds to a namespace, and the entries in that file correspond to keys. For example, given this dictionary ...
# welcome.yaml
page_title: Hello, {{ salutation }}!
fields:
name:
label: What's your name?
Then you'd access these keys as so, in Nunjucks:
<h1>{{ t('welcome:page_title', { salutation: 'World' }) }}</h1>
<p class="govuk-body">
{{ t('welcome:fields.name.label') }}
</p>
This example also demonstrates i18next's string interpolation capability.