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).

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:

['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:


Translation with placeholders

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

Entry in the glossary:

“” “My name is %name%”

Now replace it with a value in the Twig template:

{{ "" | 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:


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