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.

Archive

Translating to other than current languages

Started by Muut Archive 9 years ago · 3 replies · 951 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 Deleted User, 9 years ago
0 1280 9 years ago
Archive · by Muut Archive, 9 years ago
2 888 9 years ago
Archive · by Muut Archive, 9 years ago
2 4017 9 years ago
Archive · by Muut Archive, 9 years ago
1 2892 9 years ago
Archive · by Muut Archive, 9 years ago
3 1077 9 years ago