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.

Support

TWIG: Translation dependent on page header (frontmatter) content

plugins

Started by p 3 years ago · 2 replies · 395 views
3 years ago

Hi,

I want to improve the translations in the events plugin and have the following situation and code in files (excerpts):

languages.yaml:

YAML
en:
  PLUGIN_EVENTS:
    EVENTS:
      FREQ_NONE: none
      FREQ_DAILY: daily
      FREQ_WEEKLY: weekly
      FREQ_MONTHLY: monthly
      FREQ_YEARLY: yearly
de:
  PLUGIN_EVENTS:
    EVENTS:
      FREQ_NONE: keine Wiederholung
      FREQ_DAILY: täglich
      FREQ_WEEKLY: wöchentlich
      FREQ_MONTHLY: monatlich
      FREQ_YEARLY: jährlich

blueprints/event.yaml:

YAML
            header.event.freq:
              type: select
              label: Frequency
              help: How often should this event repeat?
              default: none
              options:
                'none': PLUGIN_EVENTS.EVENTS.FREQ_NONE
                'daily': PLUGIN_EVENTS.EVENTS.FREQ_DAILY
                'weekly': PLUGIN_EVENTS.EVENTS.FREQ_WEEKLY
                'monthly': PLUGIN_EVENTS.EVENTS.FREQ_MONTHLY
                'yearly': PLUGIN_EVENTS.EVENTS.FREQ_YEARLY

event.md:

YAML
---
uid: 'e046a6da-2179-4b53-9382-c8dbf259d46e'
title: 'test-custom'
event:
    start: '12-01-2023 15:00'
    end: '12-01-2023 16:00'
    freq: daily
    repeat: MTWRF
---

Now I want to translate the value of header.event.freq from the markdown file inside twig.
<span>{{ ("PLUGIN_EVENTS.EVENTS.FREQ_"~page.header.event.freq|upper)|t }}</span> works, but looks quite ugly in my opinion. Are there other ways to achieve this?

3 years ago

I use a similar approach, but with a set variable, something like

TWIG
{% set event_freq = ('PLUGIN_EVENTS.EVENTS.FREQ_' ~ page.header.event.freq|upper)|t %}
...
<span>{{ event_freq|e }}</span>

with some checking if string has translation
I'm fine with it as it works) but would be interested to see other options

by the way, in your case you can use switch tag

TWIG
{% switch page.header.event.freq %}
  {% case 'daily' %}
     {% set event_freq = 'PLUGIN_EVENTS.EVENTS.FREQ_DAILY'|t %}
  {% case 'weekly' %}
     {% set event_freq = 'PLUGIN_EVENTS.EVENTS.FREQ_WEEKLY'|t -%}
  {% case 'monthly' %}
     {% set event_freq = 'PLUGIN_EVENTS.EVENTS.FREQ_MONTHLY'|t -%}
  {% case 'yearly' %}
     {% set event_freq = 'PLUGIN_EVENTS.EVENTS.FREQ_YEARLY'|t -%}
  {% default %}
     {% set event_freq = 'PLUGIN_EVENTS.EVENTS.FREQ_NONE'|t %}
{% endswitch %}
...
<span>{{ event_freq|e }}</span>
last edited 01/07/23 by Vadym
3 years ago

@pikim, An alternative is having the blueprint write 'PLUGIN_EVENTS.EVENTS.FREQ_DAILY' into the frontmatter. But you might find that ugly too...

Or, in your languages.yaml, you could use FREQ_daily: daily. AFAIK, using uppercase for translation strings is just a convention, not a requirement.

Whatever alternative you use, some ugliness remains...

last edited 01/07/23 by pamtbaau

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 53 10 hours ago
Support · by Anna, 3 days ago
2 60 13 hours ago
Support · by Justin Young, 14 hours ago
1 30 14 hours ago
Support · by Duc , 1 week ago
2 65 5 days ago
Support · by Colin Hume, 1 week ago
2 56 5 days ago