Reference translation (stored in yml file) in navigation yml file

Hello,

In our navigation, we have the following loop:

{% for link in site.data.navbar %}
<div class="nav-item ml-2">
    <a class="nav-link" href="{{ site.baseurl }}{{ link.url }}">{{ link.title }}</a>
</div>
{% endfor %}

The loop refers to the file _data/[locale]/navbar.yml, which contains this:

- link:
  title: About
  url: /about
- link:
  title: Blog
  url: /blog

Now, there is one navbar.yml per locale (per language). But as we already have the page titles translated in a main translation file in _i18n/[locale].yml:

## Page & menu titles
titles:
  blog: "Blog"
  about: "About"

We’re using the jekyll-multiple-languages-plugin.

Rather including duplicate strings in our translation system (‘About’ from the page title variable, and from the navigation array item), I was thinking: can we not reference the translation string, which is stored in the translation yml file, in navbar.yml like this:

- link:
  title: {% t titles.about }
  url: /about
- link:
  title: {% t titles.about }
  url: /blog

I tried the above, but when building the site the following error is thrown: found character that cannot start any token while scanning for the next token at line 2 column 11

Is it correct that this is not possible, or did I just do something wrong in the execution? If what I attempt is not possible, would there be a workaround, so that we don’t need to have one navbar.yml per language?

Many thanks!

This doesn’t look like valid yaml or liquid.

And that is causing a syntax error.

What are you trying to do on this line?

If your goal is to reduce duplication, then have a base maybe the English and if no override is set for a language then use the base.

E.g.

English

- title: About
  url: /about

Some other

- url: /about

Then in your Liquid

{% assign title = item.title | default: site.data.engish[0].title %}

{{ title }}

But also I don’t know how to use the plugin

You can also try the approach with duplicates on a small scale and see if you error goes away. Then change on item without duplication to get it to work with better approach. That way you start from a working state and make small changes, to help narrow down exactly what does and doesn’t work.

Thanks @MichaelCurrin, you pointed me in the right direction there!

Instead of including the printed title in the _data/[lang]/navbar.yml, I now have a single _data/navbar.yml with not the actual texts, but just keys:

# note: entry must match page title key as used in _i18n/en.yml
- entry: about
  url: /about
- entry: blog
  url: /blog

Which I then use in _includes/nav.html to create the full string key that the translation plugin can pick up from _i18n/[locale].yml (instead of simply printing the ‘title’ for each item, taken from the navbar.yml):

{% for link in site.data.navbar %}
{% capture title %}titles.{{ link.entry }}{% endcapture %}
<div class="nav-item ml-2">
    <a class="nav-link" href="{{ site.baseurl }}{{ link.url }}">{% t {{ title }} %}</a>
</div>
{% endfor %}

The translations file _i18n/[locale].yml used by the plugin then looks like this (and if the site is generated in another language, it provides the translated version of the string):

titles:
  about: "About"
  blog: "Blog"
1 Like

@MichaelCurrin The website in question is that of the open-source podcast manager for Android AntennaPod: www.antennapod.org. I’m preparing the site to be better translatable. I’m thinking just now: would you be open to doing a (quick) code review? I’m just building experience with Jekyll as I’m working on the site, so a pair of experienced eyes would be very welcome. Please don’t hesitate to say no, also :slight_smile:

1 Like

Sure, you can assign me to “@” mention me on a GitHub PR and I can review.

Account on GH:

@MichaelCurrin
1 Like