The cleanest way to use tags on mutlilingual site

Hello,

I’m struggling to set up tags with restricted options in a multilingual site, and I feel like I’m missing something.

My goal is simple (I don’t want a full WordPress-like taxonomy):

  • I have a list of films
  • I want only two categories
  • I want URLs like:
/films/tag:Catalogue
/en/movies/tag:Catalog

or, if tags must be unique internally, this will be OK:

/films/tag:catalog
/en/movies/tag:catalog

My blueprint :

tags:
  type: tags
  label:
    en: Categories
    fr: Catégories
  width: 1/4
  options:
    catalogue:
      fr: Catalogue
      en: Catalog
    in-development:
      fr: En développement
      en: In development

My template :

$tagMap = [
  'catalogue' => ['fr' => 'Catalogue', 'en' => 'Catalog'],
  'in-development' => ['fr' => 'En développement', 'en' => 'In development']
];

$movies = $page->children()->listed();
$allTags = $movies->pluck('tags', ',', true);
$activeTagSlug = param('tag');

if ($activeTagSlug && in_array($activeTagSlug, array_keys($tagMap))) {
  $movies = $movies->filterBy('tags', $activeTagSlug, ',');
}
<?php foreach ($allTags as $tagItem): ?>
  <?php
    $tagLabel = $tagMap[$tagItem][kirby()->language()->code()] ?? $tagItem;
    $isCurrent = ($activeTagSlug === $tagItem) ? 'is-current' : '';
    $tagUrl = $page->url() . '/tag:' . $tagItem;
  ?>
  <li>
    <a href="<?= $tagUrl ?>" class="btn btn--semi <?= $isCurrent ?>">
      <?= html($tagLabel) ?>
    </a>
  </li>
<?php endforeach ?>

My question, what is the cleanest and recommended way in Kirby to have clean URLs and avoid maintaining a manual tag mapping in templates with $tagMap?

Am I using the tags field wrong, or is this a known limitation?

Thanks in advance!

I guess you don’t want the tag values to be translatable (i.e. different in different languages), therefore the first step would be to make the field untranslatable by setting:

translate: false

Once that is done, the tags can only be applied in the default language.

It might make sense to set the translations up as translation variables instead of putting the translations into the blueprint: Custom language variables | Kirby CMS, this can be done via the Panel. That way, they are easier to use in the frontend using the t() helper than querying the translations from the blueprint.

After that, you have two options: use the value, i.e catalogue and in-development in the url, and the translation just as display value for the link.

Or use the translation variable mapping to use the translation as both display and link value, but then filter by the original value using the mapping.

Thanks Sonja!

So, is it correct to say that Kirby does not natively support translated tag slugs in URLs, and that the recommended approach is to keep tag values language-independent and only translate their labels?

In other words, this is not possible natively:

/films/tag:catalogue
/en/movies/tag:catalog

Thanks!

It would be possible if you use options that depend on the selected language, but that would mean that you have to assign the tags per language, which in my opinion doesn’t make any sense.

1 Like