kurtsson / jekyll-multiple-languages-plugin

I18n support for Jekyll and Octopress
MIT License
923 stars 201 forks source link

String interpolation #131

Open camel113 opened 5 years ago

camel113 commented 5 years ago

It seems that we can not use string interpolation.

I would like to do the following: key: "{{what}} is {{how}}" like that {% t key, { what: "Code", how: "Cool" } %}

See an official example here: https://www.i18next.com/translation-function/interpolation

pklada commented 4 years ago

Late to this, but we accomplished this by creating an include which handles the interpolation for us. Its a bit hacky, but works.

So we have an include translation file, which accepts a interpolate property which can have multiple strings to interpolate, separated by pipes

  {% capture translation %}
    {% t include.key %}
  {% endcapture %}

  {% if include.interpolate %}
    {% assign interpolateStrings = include.interpolate | split: "|" }} %}

    {% for string in interpolateStrings %}
      {% capture translation %} 
        {% assign leftBrace = "{" %}
        {% assign rightBrace = "}" %}
        {% capture toReplace %}{{ leftBrace }}{{ forloop.index }}{{ rightBrace }}{% endcapture %}
        {{ translation | replace: toReplace, string }}
      {% endcapture %}

    {% endfor %}
  {% endif %}

  {{ translation | strip }}

Then, instead of the default translation liquid tag provided by this plugin, we use our own include:

{% capture interpolate %}<a href="{{ site.baseurl }}/some-url">|</a>{% endcapture %}
{% assign key = "my_key_here" %}
{% include trans.html key=key interpolate=interpolate %}</a></p>

The strings then need to have the proper format:

my_key_here: "{1}this is some text i want to be a link{2}, and a continuing paragraph here.

Its important with this implementation to have the {1}, {2}, etc format tags match the order of the strings you supply the interpolate include.