Add a dynamic variable in title var inside mardown header

Hi! I would like to set the title of my page using

title: My Titlte

But I would like to use the language translation system provided my Grav: {{ “HEADER.MAIN_TEXT”|t }}

Sorry, didn’t finish my post - So is it possible to add dynamic variables inside the title property? Thank you!

Something like:

title:  {{ “HEADER.MYTITLE”|t }

Thanks

Hi @sebastienaxinte,

usually page titles are unique (site-wide) and thus it makes no sense to use the Twig translation filter. Usually, what you do is to create one page for each language. The docs tell you more about it.

However Grav has an option, called frontmatter.process_twig, which pre-renders certain frontmatter fields (https://github.com/getgrav/grav/blob/develop/system/config/system.yaml#L62-L64). If you turn this on, you above approach may work or may be not, because the processing may happen before the translation filter t is not loaded yet and only basic Twig variables like page are accessable.

Ok thanks, I will try but unfortunately I can’t use hard coded titles because I’m using a translation service for that, so I will try your option.

Did you/someone else find a solution?

My suggestion is that you use a translation file in your theme folder.
user/themes/your_theme/languages.yaml

en:
  SITE:
    TITLE: Your title
    DESCRIPTION: 
    COPYRIGHT: © Copyright %s Your Company
  SUBSCRIBE: Subscribe
娱乐:
  SITE:
    TITLE: .....
    DESCRIPTION: ...
    COPYRIGHT: ...
  SUBSCRIBE: ...
nb:
  SITE:
    TITLE: ....
    DESCRIPTION: ...
    COPYRIGHT: ...
  SUBSCRIBE: ...

And use this in twig:

{{ 'SITE.TITLE'|t }}
---

The evaluate_twig() Twig function should work. E.g. evaluate_twig(page.title)

Correct me if I’m wrong but this doesn’t really solve the issue for individual pages that have different titles for different languages? Would be handy to have all the translations in a single file for external translation services.

You can create a translation overrides file and put there all your titles. Then you still need to associate each page title to a string, so yo could enter

PAGE_TITLE:
  BLOG: Blog
  HOME: Home

and in your pages, use the string, e.g. PAGE_TITLE.BLOG. In the theme, use the |t filter to translate the title: {{ page.title|t }}