Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What you are trying to do isn't possible without modifying Jekyll. The <code>include</code> filter they defined treats its parameter as a string, not as an expression.</p> <p>I've created a couple bilingual sites with Jekyll in the past. I found that the cleanest solution was often storing locale-dependant variables inside _config.yml, and reference it when needed.</p> <pre><code># _config.yml ... locales: en: welcome_message: "Welcome to my site" ... es: welcome_message: "Bienvenido a mi sitio" </code></pre> <p>For every page I render, I need to know its locale. The simplest way of doing that is adding a <code>locale</code> field on the top yaml that page:</p> <pre><code>--- # a page or post (for example index.html) ... locale: en --- </code></pre> <p>You can then get the current page locale by doing <code>page.locale</code>.</p> <p>If you have divided your site in folders (<code>/en/ for english, /es/ for spanish, and so on</code>, you can use the page url to calculate the locale, so that you don't need to specify the locale on each page:</p> <pre><code>{% capture locale %}{{ page.url | truncate: 3, "" | remove: "/" }}{% endcapture %} {% if locale == "" %}{% assign locale = "en" %}{% endif %} </code></pre> <p>For a page like <code>/en/blog/</code>, <code>locale</code> will be 'en'; for <code>/fr/le-blog</code>, it will be 'fr'. You will have to use that line in all the layouts and includes that need to use the locale, though.</p> <p>You can then get localized texts like this:</p> <pre><code>{{ site.locales[locale].welcome_message }} </code></pre> <p>Or, if you prefer <code>page.locale</code>:</p> <pre><code>{{ site.locales[page.locale].welcome_message }} </code></pre> <p>Menus are the same; you can generate them from _config.yml, too:</p> <pre><code># _config.yml ... locales: en: nav: - text: Welcome url: /en/welcome - text: Blog url: /en/blog layout: post ... es: nav: - text: Bienvenido url: /es/bienvenido - text: Blog url: /es/blog layout: post ... </code></pre> <p>Then you can have a single menu.html that generates the right menu depending on the page locale:</p> <pre><code>{% capture locale %}{{ page.url | truncate: 3, "" | remove: "/" }}{% endcapture %} {% if locale == "" %}{% assign locale = "en" %}{% endif %} &lt;nav&gt; &lt;ul class="nav"&gt; {% for link in site.locales[locale].nav %} {% assign current = nil %} {% if page.url == link.url or page.layout == link.layout %} {% assign current = 'current' %} {% endif %} &lt;li class="{% if forloop.first %}first{% endif %} {{ current }} {% if forloop.last %}last{% endif %}"&gt; &lt;a class="{{ current }}" href="{{ link.url }}"&gt;{{ link.text }}&lt;/a&gt; &lt;/li&gt; {% endfor %} &lt;/ul&gt; &lt;/nav&gt; </code></pre> <p>For using ´page.locale´ instead of ´locale´, just remove the first two lines and replace <code>site.locales[locale].nav</code> by <code>site.locales[page.locale].nav</code></p> <p>I hope this helps.</p> <p>Regards!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload