Filtration of posts by language in jekyll blog on GitHub Pages
-
I add to my blog the translation of posts into different languages. I've been doing the translation, but I can't figure out how I can display messages in one language on the post page. To date, I have duplicated articles in different languages. For starters, this is my code that connects attributes.
{% if page.layout != 'index' %} {% assign posts=site.posts | where:"lang-ref", page.lang-ref | sort: 'lang' %} {% if posts.size == 0 %} {% assign posts=site.pages | where:"lang-ref", page.lang-ref | sort: 'lang' %} {% endif %} {% if posts.size > 1 %} <footer class="lang-options"> <em>Read this page in a different language:</em> <ul> {% for post in posts %} <a href="{{ site.base-url }}{{ post.url }}" class="{{ post.lang }}" title="View in {{post.lang}}">{{ post.lang }}</a> {% endfor %} </ul>
That's how I put the posts on the page:
{% for post in paginator.posts %} <article class="post-preview"> <a href="{{ post.url | prepend: site.baseurl | replace: '//', '/' }}"> <h2 class="post-title">{{ post.title }}</h2> {% if post.subtitle %} <h3 class="post-subtitle">{{ post.subtitle }}</h3> {% else %} <h3 class="post-subtitle">{{ post.excerpt | strip_html | truncatewords: 15 }}</h3> {% endif %} </a> <p class="post-meta">Posted by {% if post.author %} {{ post.author }} {% else %} {{ site.author }} {% endif %} on {{ post.date | date: '%B %d, %Y' }} · {% include read_time.html content=post.content %} </p> </article>
That's how I try to filter messages on the page. The code is intended to eject messages in the language of the current page.
<article class="post-preview"> {% assign posts = site.posts | where:"lang", page.lang %} {% for post in posts %} <a href="{{ post.url | prepend: site.baseurl | replace: '//', '/' }}"> <h2 class="post-title">{{ post.title }}</h2> {% if post.subtitle %} <h3 class="post-subtitle">{{ post.subtitle }}</h3> {% else %} <h3 class="post-subtitle">{{ post.excerpt | strip_html | truncatewords: 15 }}</h3> {% endif %} </a> {% endfor %}
But filtering doesn't work, and I get a mistake.
Liquid Exception: undefined method `split' for nil:NilClass in posts/index.html
-
Maybe someone also faced this filtration problem. That's how I decided to:
{% assign sorted-posts = site.posts | where: "lang", page.lang %} {% if sorted-posts.size > 0 %} {% for post in sorted-posts %}
Same grading on the front page:
{% assign sorted-posts = site.posts | where: "lang", "en" %} {% for post in sorted-posts limit: 5 %}
It's just that all the pages should be able to use the language and remove the posts in the front page only.