getgrav / grav

Modern, Crazy Fast, Ridiculously Easy and Amazingly Powerful Flat-File CMS powered by PHP, Markdown, Twig, and Symfony
https://getgrav.org
MIT License
14.4k stars 1.39k forks source link

Mutlilingual support for media meta data #323

Open attiks opened 8 years ago

attiks commented 8 years ago

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?

attiks commented 8 years ago

I found a solution:

image.meta.yaml

alt_text: My Alt Text
title:
  en: EN Image description
  nl: NL Image description

html.twig

  {% include 'partials/image_list.html.twig' with {'images': page.media.images, 'language': langswitcher.current} only %}

partials/image_list.html.twig

    {% 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 %}
rhukster commented 8 years ago

There is no metadata mutli-language support yet.

rhukster commented 8 years ago

Your solution is a good one! :)

attiks commented 8 years ago

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?

rhukster commented 8 years ago

try this:

{{ grav.language.getActive }}
attiks commented 8 years ago

{{ 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

attiks commented 8 years ago

For others facing the same problem:

Do the include without the only param.

  {% include 'partials/image_list.html.twig' with {'images': page.media.images}  %}

partial

    {% 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 %}
nunocodex commented 8 years ago

Yes, the last is the best solution :)

attiks commented 8 years ago

Should this be added to the docs?

flaviocopes commented 8 years ago

Can be a nice one for the cookbook ;)

hwmaier commented 8 years ago

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
attiks commented 8 years ago

@hwmaier how will this be handled in twig, the same as my code above, or in another way?

hwmaier commented 8 years ago

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.

lennerd commented 8 years ago

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.

hwmaier commented 8 years ago

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.