Skip to content
Snippets Groups Projects
Commit fb35c3b3 authored by Jaime Pérez's avatar Jaime Pérez
Browse files

Update the new sandbox page with lots of information on how twig templates and translations work.

Add also proofs of concept for every example given.
parent bcd08412
No related branches found
No related tags found
No related merge requests found
{% extends "base.twig" %} {% extends "base.twig" %}
{% block content %} {% block content %}
<p>This page exists as a sandbox to play with twig without affecting anything else. The template is in ./templates.</p> <h1>Sandbox</h1>
<p>{{ sometext }}</p> <p>This page serves as a demonstration of the <strong>new template and translation sub-systems</strong> in
<h2>And now for some localization</h2> SimpleSAMLphp. The page itself is written as a <em>Twig</em> template, which is very similar to other
<p>Locale backend in use: {{ localeBackend }}</p> templating languages, stored in the <code>templates</code> directory.</p>
<p>Original: Hello, Untranslated World!</p> <p>Twig templates allow you to print values of variables very easily. For example, the code <code>{{ '{{ ' }}
<p>Translated: {% trans 'Hello, Untranslated World!' %}</p> sometext }}</code>
<p>Filtertrans-test: {{ 'Hello, Untranslated World!'|trans }}</p> will print the following text, contained in the variable <code>sometext</code>:
<p>Current locale set: {{ currentLanguage }}</p> </p>
<p><em>{{ sometext }}</em></p>
<p>Twig supports setting your own variables, control structures like <code>if</code> clauses and loops.
Take a look at the <a href="http://twig.sensiolabs.org/doc/templates.html">Twig documentation for
template designers</a> if you want to know more.
</p>
<h2>Localization</h2>
{% set variable = 'Hello, Untranslated World!' %}
<p>SimpleSAMLphp lets you choose which translation backend to choose, thanks to the
<code>language.i18n.backend</code> configuration option. Three possible values are supported there:
</p>
<ul>
<li><code>SimpleSAMLphp</code>: to keep using the old SimpleSAMLphp translation system. This is the
default, and will disappear as an option in SimpleSAMLphp 2.0.</li>
<li><code>ext-intl</code>: to use PHP's native <em>gettext</em> implementation. Bear in mind that using this
will require you to install the locales of the languages you are planning to use, system-wide.</li>
<li><code>gettext/gettext</code>: to use a <em>gettext</em> implementation written entirely in PHP, allowing
you to use any locale, no matter if they are installed on the system or not.</li>
</ul>
<p>Note that <code>gettext/gettext</code> <strong>will become the default</strong> in SimpleSAMLphp 2.0.
Currently, you are using the following backend: <code>{{ localeBackend }}</code>.</p>
<p>Now, Twig allows you to translate strings in your templates. There are several ways to do that. If you want
to translate the following text: <em>Hello, Untranslated World!</em>, you can do it with:</p>
<ul>
<li><em>Inline trans tags</em>: using <code>{{ '{%' }} trans 'Hello, Untranslated World! %}</code> you would get
"{% trans 'Hello, Untranslated World!' %}".</li>
<li><em>Expanded trans tags</em>: using <code>{{ '{%' }} trans %}Hello, Untranslated World!{{ '{%' }} endtrans
%}</code> you would get "{% trans %}Hello, Untranslated World!{% endtrans %}".</li>
<li><em>Filters</em>: using <code>{{ '{{' }} variable|trans }}</code> you would get "{{ variable|trans }}".</li>
</ul>
<p>Translations support arguments too, so that you can replace parts of the translated string with the contents of
variables. Just use placeholders of the form <code>%variable%</code> in the place where the contents of the
variables should be placed, and pass an associative array to the <code>trans </code> filter.</p>
{% set variable = 'Hello, %who%!' %}
{% set who = {'%who%': 'World'} %}
<p>If you have a variable with the text "<code>Hello, %who%!</code>" The code <code>{{ '{{' }}
variable|trans({'%who%': 'World' }) }}</code> will print "{{ variable|trans({'%who%': 'World'}) }}". The array
can also be passed in a variable, so that <code>{{ '{{' }} variable|trans(who) }}</code> will also output
"{{ variable|trans(who) }}". Note that placeholders have names, so order is irrelevant, and can be changed
between translations.</p>
{% endblock content %} {% endblock content %}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment