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

Cheking if translation exists

Solved by pamtbaau View solution

Started by P. Fighter 8 years ago · 2 replies · 1246 views
8 years ago

Is there a way - in Twig - to check if a specific translation exists in the currently active language (html_lang)?

I tried something like this

TWIG
{% if 'STRING.TRANSLATION.PATH' %}
//do stuff
{% endif %}

But that always equals true, apparently.

Setting translations_fallback to false doesn't help.

8 years ago

There may be a simpler way I don't know, but ...

What do you get from the t custom Twig filter (translate string to current language)? What do you get for a result if you output {{ 'MY_STRING'|t }}. If it's empty or undefined, you should be able to simply put an if in front of it, do if .. is defined, if .. is not empty etc.

8 years ago Solution

TLDR: Based on some tests I noticed the following

  • Is language supported: html_lang in config.system.languages.supported
  • Is translation available: html_lang == grav.language.getLanguage
  • Does string have translation: 'TRANSLATE_ME'|t != 'TRANSLATE_ME'

Created a site with the following settings:

YAML
/user/config/system:
    languages:
      supported: (Note: no Spanish)
        - it
        - fr
        - en
      translations: true
      translations_fallback: false
/user/config/site:
    default_lang: en
user/pages/
    01.home (Note: page for supported languages + Spanish)
        default.en.md
        default.fr.md
        default.it.md
        default.es.md

The site has a theme with the following translations:

YAML
languages.yaml: (Note: No Italian, No Spanish)
    fr:
      TRANSLATE_ME: Traduis-moi
    en:
      TRANSLATE_ME: Translate me

The themes partials/base.html.twig looks like:

TWIG
<p>html_lang: {{ html_lang }}<br/>
Is language supported: {{ html_lang in config.system.languages.supported ? 'Yes' : 'No' }}<br/>
html_lang == getLanguage: {{ html_lang == grav.language.getLanguage ? 'Yes' : 'No' }}<br/>
Is translated: {{ 'TRANSLATE_ME'|t == 'TRANSLATE_ME' ? 'No' : 'Yes' }}<br/>
Translation: {{ 'TRANSLATE_ME'|t }}</p>

This setup yielded the following when browsing to the different pages:

YAML
http://localhost/grav-dev/en
    html_lang: en
    Is language supported: Yes
    html_lang == getLanguage: Yes
    Is translated: Yes
    Translation: Translate me

http://localhost/grav-dev/fr
    html_lang: fr
    Is language supported: Yes
    html_lang == getLanguage: Yes
    Is translated: Yes
    Translation: Traduis-moi

http://localhost/grav-dev/it
    html_lang: en
    Is language supported: Yes
    html_lang == getLanguage: No
    Is translated: No
    Translation: TRANSLATE_ME

http://localhost/grav-dev/es -> Page does not exist error
    html_lang: en
    Is language supported: Yes
    html_lang == getLanguage: No
    Is translated: No
    Translation: TRANSLATE_ME
👍 2
last edited 06/18/18 by pamtbaau

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 53 9 hours ago
Support · by Anna, 3 days ago
2 59 12 hours ago
Support · by Justin Young, 13 hours ago
1 30 13 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