Skip to content

Translation Liquid Tag

Vincent Wochnik edited this page Nov 18, 2015 · 1 revision

The language plugin comes with a t tag, short for translation tag.

Syntax

{% t key_expression[: replacement_expression[, replacement_expression[, ...]]] %}

Arguments

The first argument, key_expression, is an expression evaluating to the language key where the string to be printed can be found. This can be a quoted string directly evaluating to the key or a liquid variable whose contents evaluate to the language key.

Any further arguments, replacement_expressions, are expressions evaluating to a string that is replacing a placeholder (%%) in the original language string. Those expressions can be liquid variables as well. This provides vast flexibility for the developer.

Examples

Simple language key print

Provided there is a key welcome for each language we can simply print out the translation with the following statement:

{% t 'welcome' %}

If a subset is specified by the page or post, first a lookup within that subset of the language is performed. Only if the key could not be retrieved, another lookup within the root of the language is performed.

Variable language key print

The example above can be modified to demonstrate the expression capability of the t tag. Internally t uses Liquid::Parser and Liquid::Expression to evaluate expressions and retrieve the values for liquid variables.

{% assign language_key = 'welcome' %}
{% t language_key %}

This example is quite silly for the use-case but there are other scenarios where you need to have the key from a variable, for instance within an iteration.

Language key print with replacement

Given a language key post.quoted_title for each language which looks similar to '»%%«', a quoted post title can be printed using the following statement:

{% t 'post.quoted_title': post.title %}

Of course, post.title needs to be a valid liquid variable for this to work.