Translations
Every text that the package shows to a person can be translated: the messages of its exceptions, the flash messages, the login page and its form, and the answers of the API. The texts in the code are in English (it is their translation id, and what is shown when there is no translation), and the Spanish translation ships with the package. The mechanism is the one of derafu/translation.
What is translated, and where
| Domain | What | Catalogue |
|---|---|---|
errors |
The messages of the exceptions (AuthenticationException, ConfigurationException, FormException, AuthorizationException). |
resources/translations/errors+intl-icu.es.php |
auth |
The flash messages, the title of the login page, the titles of the login form (Username and Password) and the answers of the API (Unauthorized…). |
resources/translations/auth+intl-icu.es.php |
The package gives its catalogues to the translator with Derafu\Auth\Translation\AuthTranslationResourceProvider, which auth-services.yaml tags as derafu_translation.resource_provider, so it is picked up when the application builds its translator with TranslatorFactory (see derafu/translation). The services of both providers import it.
The flash messages are data
A flash message is not a text: it is the message as data (its id in English, its parameters and its domain), kept in the session, and it is translated when it is shown, in the language of whoever sees it. So a message that was left in one language shows in another if the user changed it.
// In the authentication: the message with its parameters, not a translated text.
$this->addErrorFlash($request, 'You must be logged in to access the requested page {path}', ['path' => '/admin']);
The partial partials/flash-messages.html.twig shows them (the login page includes it; include it in the layout of the application to show them in every page):
{% include 'partials/flash-messages.html.twig' %}
The text of the translation is trusted (it can have HTML: a link, a line break), and the parameters are escaped before they go in it, because they can come from a user. The partial needs the filter trans of derafu/twig and the variable app.flashes, which the controllers of the package give.
The answers of the API
The 401 of an unauthenticated request to /api, and the answer of an authorization that is denied, have a title and a detail in the language of the translator (Symfony\Contracts\Translation\TranslatorInterface), when the application has one: the services get it by autowiring, and without it they are in English.
The login form
The titles of the fields of the login form are fixed texts in the domain auth (Username, Password) and the form says it with the option translation_domain: auth: the form factory of derafu/form translates them when the form is created, in the language of the translator.
Another language
An application adds a language with its own catalogues, in the same domains and with the same ids (the English text), and its own provider:
// resources/translations/auth+intl-icu.fr.php
return [
'Login' => 'Connexion',
'Invalid identity or password.' => 'Identifiant ou mot de passe invalide.',
// ...
];
The ids are the texts of the English catalogue: resources/translations/auth+intl-icu.es.php is the list of what to translate. The messages with parameters use the ICU format ({path}, {minutes, plural, one {# minute} other {# minutes}}).
That nothing is left untranslated
The tests of the package audit it: a message of the code, or a text of the templates, or of the login form, without its translation in the Spanish catalogue fails the test, and so does an entry of the catalogue that nothing uses. See Testing.