Adding translations for Yves

Edit on GitHub

Textual translations are handled by the Glossary module. You can use the GlossaryFacade in Zed to add entries to Glossary (or you can use the Zed UI).

<?php
class GlossaryFacade extends AbstractFacade
{
    public function createTranslation($keyName, LocaleTransfer $locale, $value, $isActive = true){ ... }

    // there are several other methods in this facade
}

In the glossary, an entry has a key and translations per locale, like this:

<?php
['say.hello' => [
    'de_DE => 'Hallo',
    'en_US' => 'Hello']
]

Before it can be used in Yves, this data must be exported to the KV storage. You can use the Redis Desktop Manager to look inside and see the values.

Glossary KV and DB

Usage in Twig templates

The translation function is provided by the Symfony translation component.

Simple translation

You can use the key in a Twig template in Yves:

{{ 'say.hello' | trans }}

Sometimes you need to list all keys which are used in a template. Currently, there is no good solution, but this regex does the job pretty good:

{{.?"(.*)".?\|.?trans.?}}

Translation with placeholders

When you have a dynamic part in the translation, you can use placeholders.

Entry in the glossary:

KEY VALUE
“my.name” “My name is %name%”

Now replace it with a value in the Twig template:

{{ "auth.my.name" | trans({'%name%' : 'Fabian'}, "app") }}

This shows “My name is Fabian”.

Translation with AJAX requests

To use an AJAX request, you need to send translated content directly from the controller. In Yves, you can locate the translator and use it directly:

<?php

public function ajaxAction()
{
    $app = $this->getLocator()->application()->pluginPimple()->getApplication();   
    $text = $app->trans('what.ever');
    return $this->jsonResponse['text' => $text];
}