Multilanguage twig variable in markdown

Bonjour,

Problem: my urls output as

https://mysite:8890/page-url-espanol-123es

instead of

https://mysite:8890/es/page-url-espanol-123

I am working on a local development website, multilanguage fr, en & es, with fr set as default language in site.yalm.

My languages file is located inside the user/themes/my-theme/languages.yaml and works as desired.

My urls output fine as

https://mysite:8890/
https://mysite:8890/en
https://mysite:8890/es
https://mysite:8890/page-url-français-123
https://mysite:8890/en/page-url-english-123
https://mysite:8890/es/page-url-espanol-123

I have a page content in markdown (Process twig is enabled.) with

some text in French [**link text**](/{{ 'CATEGORIES.VILLAGE.URL'|t }}) some text in French

It works fine in fr : the url output is

https://mysite:8890/page-url-français-123

But in other languages, the output is

https://mysite:8890/page-url-espanol-123es

instead of

https://mysite:8890/es/page-url-espanol-123

Same situation obviously for English.
I tried many things, including setting the link in HTML, or even just the text without link but none worked.

For ref, this is the content of system.yaml

languages:
  supported:
    - fr
    - en
    - es
  default_lang: fr
  include_default_lang: false
  include_default_lang_file_extension: true
  translations: true
  translations_fallback: true
  session_store_active: false
  http_accept_language: true
  override_locale: false
  pages_fallback_only: false
  debug: false

Any suggestion to have the language code inserted just after the domain?

Thank you for your suggestions.

@red, Twig needs to be processed before Markdown is being processed. Set the following in the frontmatter of the page:

 twig_first: true

Thank you for the suggestion, but…
With this in the page’s frontmatter:

process:
    twig: true
    twig_first: true

result is same.

Note that my system.yaml contains:

pages:
  process:
    twig: true
    twig_first: true
  frontmatter:
    process_twig: false
twig:
  cache: false
  debug: true
  auto_reload: true
  autoescape: true
  undefined_functions: true
  undefined_filters: true
  safe_functions: {  }
  safe_filters: {  }
  umask_fix: false

@red, Have another good look at the docs on Configuration…

By the way, do you really want for every page the Twig parser being called? If not, move the settings into the header of the page.

Several hours later, hopefully learning something on the way…

I went through the data I provided you and could not find the mistake you did not mention in the configuration. My skill level could lead me to miss some critical aspects.

So, I setup a virgin new Grav+admin install on a remote server.

With
-system.yaml from virgin install adjusted to allow multilingual (copy below)
-languages.yaml from simplified local file (copy below).

I created 2 pages:

  • a default page default.es.md with this content:
---
title: Default ES
menu: Default ES
process:
    twig: true
twig_first: true
---
# Default español
Some text in spanish [**de 14 a 15 personas**](/{{ 'CATEGORIES.14A15PERS.URL'|t }}) some more text.
  • a modular page modular.es.md with this content:
---
title: Modular ES
menu: Modular ES
content:
    items: @self.modular
---
  • with modular sub-page text.es.md with this content:
---
title: Modular ES
menu: Modular ES
process:
    twig: true
twig_first: true
---
# Modular sub-page español
Some text in spanish [**de 14 a 15 personas**](/{{ 'CATEGORIES.14A15PERS.URL'|t }}) some more text.

The default page outputs the correct url:
http://test-gravfr.ptlx6361.odns.fr/es/chalets-14-15-personas

The modular do not parse the markdown and output:
# Default español Some text in spanish [**de 14 a 15 personas**](/chalets-14-15-personas) some more text.

Changing autoescape to true did not change.
Where is the elephant hiding?

Could it be that this cannot work in modular sub-pages?


For ref:

config/system.yaml:

absolute_urls: false

languages:
  supported:
    - fr
    - en
    - es
  default_lang: fr
  include_default_lang: false

home:
  alias: '/home'
  hide_in_urls: true

pages:
  theme: quark
  markdown:
    extra: false
  process:
    markdown: true
    twig: false
  twig_first: false

cache:
  enabled: false
  check:
    method: file
  driver: auto
  prefix: 'g'

twig:
  cache: false
  debug: true
  auto_reload: true
  autoescape: true

assets:
  css_pipeline: false
  css_minify: false
  css_rewrite: false
  js_pipeline: false
  js_module_pipeline: false
  js_minify: false

errors:
  display: true
  log: true

debugger:
  enabled: false
  twig: true
  shutdown:
    close_connection: true
gpm:
  verify_peer: true

themes/quark/languages.yaml

fr:
  CATEGORIES:
    14A15PERS:
      URL: 'chalets-14-15-personnes'
en:
  CATEGORIES:
    14A15PERS:
      URL: 'chalets-14-15-persons'
es:
  CATEGORIES:
    14A15PERS:
      URL: 'chalets-14-15-personas'

Tks for warning about the Twig parser. I’ll review that when this problem is solved.

@red,

I went through the data I provided you and could not find the mistake you did not mention in the configuration. My skill level could lead me to miss some critical aspects.

There were issues in the system,yaml

Your config:                     Correct config:
pages:                           pages:
  process:                         process:
    twig: true                       twig: true
    twig_first: true               twig_first: true  (Note: level of indentation)
  frontmatter:                     frontmatter: 
    process_twig: false              process_twig: false  (Note: Not needed since this is the default)             

This will fix the Twig parsing of a Regular page.

Note, that by setting these settings in system.yaml it will apply to all pages. If only needed for few pages, you might prefer to set the process and twig_first inside the header of the page.

In your initial question, you didn’t mention you are using a Modular page. I’ve tested a modular page and it indeed fails to parse correctly.

It appears modules of a modular page are parsed differently. The template of the module is being used when parsing Twig. After parsing using Twig, block elements in the template are being added to the content of the page. Think of <section>, <div>.

Unfortunately, Markdown is not being processed inside block elements.

I would consider this a bug…

Thank you for prompt and always detailed feedback.
I will revert to hardcoded urls, then.

And, as you consider this a bug, I’ll create an issue in Github.

@red, Just created an issue: `twig-first: true` in Module makes Markdown fail · Issue #3808 · getgrav/grav · GitHub

Noted. Tks for all your help.

@red, Some feedback from Github:

  • It’s by design for modular pages.
  • The suggestion is to:
    • not use Markdown links, but HTML itself,
    • or create a shortcode that will inject the proper link.

…getting in the weeds… :grin:

I end-up using HTML with urls like this
<a href="/es/{{ 'CATEGORIES.14A15PERS.URL'|t }}">
This is fetching the url in the correct language.

Satisfied with this as my target was to avoid hardcoded url everywhere.

I tried several times this HTML solution, even with just
{{ 'CATEGORIES.14A15PERS.URL'|t }}
in the markdown without any HTML but somehow it always failed, probably due to some non-matching/wrong config.

Thank you for assistance.