Multilingual
This page shows you how Unblock works with WPML and Polylang: each page in its language, and your Unblock texts translated with the plugin's own tools.
Supported plugins
There is nothing to set up: when one of them is active, Unblock follows it.
The page's language
On a translated page, your dynamic data follows its language:
- Loops list the posts of that language, as the plugin filters them.
site.urlleads to the home of that language.get_term()gives the term in that language.term.postsandpost.childrenlist the posts of that language.- Dates and
__()use that language.
The editor preview shows the same: open the French page, and its loops, dates and texts are the French ones.
To show a block in one language only, use a condition on the locale:
{% if site.language == 'fr_FR' %}
For a language switcher, use the plugin's own block: Language switcher in both WPML and Polylang.
Translating your texts
When you send a page to translation with the plugin's tools, the texts of your Unblock blocks go with the rest of the page:
| Sent | Example |
|---|---|
| Text content | Read more |
| Text attributes | title, alt, aria-label, placeholder |
| Links | href="/about" |
__() strings | {{ __('Posted on %s') }} |
What is not text stays as it is: scripts, styles, SVG, classes, IDs and your dynamic data. A link built with an expression, such as href="{{ post.link }}", is not sent: it is built for each page.
Dynamic data inside a text
In Hello {{ user.name }}, welcome, the translator sees a marker in place of {{ user.name }} and can move it where the language needs it. Unblock puts the expression back in its new place. If the marker comes back damaged or missing, the original text is kept, so an expression is never lost.
Text inside an expression
A string inside an expression is code, so it is not sent, except the one you give to __(). Write the whole sentence there, the values going in with format:
{{ __('Posted on %s')|format(post.date) }}
The translator sees Posted on %s and translates the whole sentence.
Keep an element out of translation
Add the attribute translate with the value no in the block settings panel. The element and everything inside it stay as written. An element inside it can set translate to yes to be translated again.
Item loops
The values of an Item loop can be text (question, answer) or data (slug, icon), and only you know which. Declare the keys that hold text, and the plugin translates their values itself:
<wpml-config>
<gutenberg-blocks>
<gutenberg-block type="unblock/loop" translate="1">
<key name="queryArgs">
<key name="*">
<key name="question"/>
<key name="answer"/>
</key>
</key>
</gutenberg-block>
</gutenberg-blocks>
</wpml-config>
- WPML: paste it in WPML > Settings > Custom XML Configuration.
- Polylang: save it as
wpml-config.xmlin your theme or inwp-content/polylang/.
The declaration applies to every Item loop of the site. For a Variable holding item data, use unblock/variable as the block type. Without a declaration, the values stay as in the original page.
Scripts in translations
A translation keeps the JavaScript of the page it comes from, even when it is written by someone who may not add scripts, such as a translator or an automatic translation. They can keep the scripts in place, not add or change them.
For your security, an event attribute such as onclick, placed on an element whose text attribute (title, a link…) is translated, is removed from the translation. Put that code in the block's JavaScript instead.
Limits
- Polylang and templates: Polylang cannot translate text placed directly in a template, whatever the block. Put the text in a template part or a pattern, which Polylang translates.
- Dynamic data in Item loop values is sent as it is, without a marker.