Skip to content
● Meet Grav Guard: keep every Grav site you run secure and up to date, all from one place. Free for up to 3 sites. Read more →

Translating to other than current languages

Started by Muut Archive 9 years ago · 3 replies · 1378 views
9 years ago

I would like to translate a string located in languages.yaml in my theme folder into all the languages with a for loop. I can't use the t filter, as it uses the current language.

Many thanks.

9 years ago

Hello,

I never tried it before, but if you have a look at this page:

TWIG
{% set langobj  = grav['language'] %}
{% set curlang  = langobj.getLanguage() %}
{% set fruits   = langobj.getTranslation(curlang,'FRUITS',true) %}
<span data-ticker="{{ fruits|join(',') }}">{{ random(fruits) }}</span>

I believe that you can do something like getTranslation('language you want', 'FRUITS', true)

9 years ago

Thank you very much. I did, however, make some changes to the code you provided:

TWIG
{% for lang in langswitcher.languages %}
  {% set langobj  = grav['language'] %}
  {% set org = langobj.getTranslation(lang,'ORGANIZATION',true) %}  
  <span class="org-type">{{ org }}</span>
{% endfor %}

It's intended use was in a for loop, where I used the langswitcher plugn, otherwise it would use the current language.
Thanks for your answer and pointing me in the direction of getTranslation. Much appreciated.

9 years ago

Amazing, thanks for sharing the code you used ! :)

Suggested topics

Topic Participants Replies Views Activity
Archive · by Muut Archive, 10 years ago
1 671 1 week ago
Archive · by Deleted User, 9 years ago
0 3041 9 years ago
Archive · by Muut Archive, 9 years ago
2 1980 9 years ago
Archive · by Muut Archive, 9 years ago
2 5335 9 years ago
Archive · by Muut Archive, 9 years ago
1 4133 9 years ago