Open attiks opened 8 years ago
I found a solution:
alt_text: My Alt Text
title:
en: EN Image description
nl: NL Image description
{% include 'partials/image_list.html.twig' with {'images': page.media.images, 'language': langswitcher.current} only %}
{% for img in images %}
<li><a href="{{ img.url() }}">
<figure>{{ img.cropZoom(150, 150).html() }}
{% if img.meta.title[language] %}
<figcaption>{{ img.meta.title[language] }}</figcaption>
{% endif %}
</figure>
</a></li>
{% endfor %}
There is no metadata mutli-language support yet.
Your solution is a good one! :)
Thanks :blush:
The only thing I don't like about it, is that I have to pass the language to the twig include, but I guess there's no global language available?
try this:
{{ grav.language.getActive }}
{{ dump (grav.language.getActive) }} return null when added to _partials/imagelist.html.twig, it works inside html.twig. I guess the only param is blocking it, if I remove it, it works.
Thanks
For others facing the same problem:
Do the include without the only param.
{% include 'partials/image_list.html.twig' with {'images': page.media.images} %}
{% for img in images %}
<li><a href="{{ img.url() }}">
<figure>{{ img.cropZoom(150, 150).html() }}
{{ dump (grav.language.getActive) }}
{% if img.meta.title[grav.language.getActive] %}
<figcaption>{{ img.meta.title[grav.language.getActive] }}</figcaption>
{% endif %}
</figure>
</a></li>
{% endfor %}
Yes, the last is the best solution :)
Should this be added to the docs?
Can be a nice one for the cookbook ;)
The automatic alt/title tag generation based on meta files introduced here https://github.com/getgrav/grav/commit/79f6380aae36c3d6bb354a73d90f43216d757ca4 has to be taken into account for a multilang solution.
At least the structure of the meta file has to change from
alt_text: My Alt Text
title:
en: EN Image description
nl: NL Image description
to something like this:
alt: My Alt Text
title: EN Image description
nl:
title: NL Image description
@hwmaier how will this be handled in twig, the same as my code above, or in another way?
Good point, it is not as elegantly achievable like your previous solution were the active language was used as array index.
Some more thought has to be put into a meta.yaml structure which satisfies both needs, auto-magic alt/title attributes and mult-lang meta data.
Did you try to have multiple meta data files? For example image.jpg.meta.yml
for general data and image.jpg.meta.en.yml
or image.jpg.meta.de.yml
for translations. I think this is not possible with Grav out of the box at the moment. But maybe it's worth thinking about.
Refer to previous comment: "There is no metadata mutli-language support yet."
Given the small amount of information in a meta file, I think multi-lang should be handled within just a single file. Otherwise we end up with too many files in a directory, which is not good for Grav as it slows things down and you quickly loose oversight too. Think about a page with 10 images and 5 languages. You end up with 50 meta files. Have a site with 100 pages and you have 5000 files to scan and parse.
I would like to see an approach were the language content is structured using language codes similar to the translation files.
Examples are given above.
I want to translate the meta data (alt_txt and title) of images, I tried adding files like:
But the effect is that the image is shown twice and always using the Dutch meta data.
What is the advised way to handle this?