default language #4

Closed
sakulstra opened this Issue May 9, 2016 · 1 comment

Projects

None yet

2 participants

@sakulstra

Is there a way to have a default language? so when an article isn't available in e.g. french, the english original should be listed.

Regards,
lukas

@sylvaindurand
Owner

Hello,

And sorry: I have been quite busy these days...

But, good news, yes, it is possible!

In fact, many different goals can be achieved but the idea is still the same: just play with loops.

The actual code for listing French articles looks like:

<ul>
{% assign posts=site.posts | where:"lang", "fr" %}
{% for post in posts %}
  <li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
</ul>
``

It says : “get all posts written in French; loop to show them”.

What you want to do can be easily achieved with:

```liquid
<ul>
{% assign posts=site.posts | where:"lang", "en" %}
{% for default in posts %}
  {% assign translation = site.posts | where:"ref", default.ref | where:"lang", "fr" %}
  {% if translation[0] %}
    <li><a href="{{ translation[0].url }}">{{ translation[0].title }}</a></li>
  {% else %}
    <li><a href="{{ default.url }}">{{ default.title }}</a> [en] </li>
  {% endif %}
{% endfor %}
</ul>

Here, it says: “get all posts written in English; for each post, check if there is a French translation; if yes, show the French translation; if not, show the English one”.

Please let me know if you want to do something else. I'll put a link on my blog to this answer, as it is a quite common to have a default language. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment