Translation syntax

Translation syntax

Translation domains

Shortcuts allow access to translations relative to certain contexts.

// Relative to the layout (HTML meta tags and main page frame)
{{ '@layout::meta.description' | trans }}
// Relative to the page (page body or modal for example)
{{ '@page::page_title' | trans }}

Inclusions

It is important to reduce duplicate text in a translation file. Magic keys allow inclusions from one file to another using the internal syntax @folder.file::key.sub_key.

You can combine it with the % keyword if the key or key tree is the same.

# translation-file-one.en.yml
example_key:
  example_sub_key: Translation string

# translation-file-two.en.yml
example_key:
  # Using this key resolves to the first file
  # using the same key "example_key.example_sub_key"
  example_sub_key: '@translation-file-one::%'

Extensions

If most keys in a file are the same as another, it is simpler to use the ~extends syntax to reuse all keys and only add the new ones.

# translation-file-two.en.yml
~extends: '@translation-file-one'

Caching

Translation files are resolved and cached to preserve performance.

Javascript

Translations can be passed to Javascript for dynamic use.

// template.html.twig
trans_js('@page::javascript.*')
// Hello Javascript!
app.layout.page.trans('@page::javascript.demo_text');