Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Multilanguage twig variable in markdown

Solved by pamtbaau View solution

Started by Thomas 2 years ago · 10 replies · 295 views
2 years ago

Bonjour,

Problem: my urls output as

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

instead of

TXT
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

TXT
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

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

It works fine in fr : the url output is

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

But in other languages, the output is

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

instead of

TXT
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

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.

last edited 02/27/24 by Thomas
2 years ago

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

TXT
 twig_first: true
2 years ago

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

YAML
process:
    twig: true
    twig_first: true

result is same.

Note that my system.yaml contains:

TXT
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
2 years ago

@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.

2 years ago

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:
    YAML
    ---
    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:
    YAML
    ---
    title: Modular ES
    menu: Modular ES
    content:
    items: @self.modular
    ---
    
  • with modular sub-page text.es.md with this content:
    YAML
    ---
    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?
<br><br>

For ref:

config/system.yaml:

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

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.

last edited 02/28/24 by Thomas
2 years ago

@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

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...

2 years ago

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.

2 years ago

Noted. Tks for all your help.

2 years ago Solution

@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.
2 years ago

…getting in the weeds… 😁

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.

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 76 7 hours ago
General · by pamtbaau, 13 hours ago
1 47 12 hours ago
General · by Andy Miller, 1 day ago
0 44 1 day ago
General · by Marcel, 12 months ago
6 346 5 days ago
General · by Duc , 5 days ago
3 40 5 days ago