API
For the API, i18n has three goals:
- Translate the validation error messages.
- Translate the emails.
- Translate the spreadsheets.
The symfony/translation package helps us for that task.
note
📣  It requires the PHP extension intl
, already configured in the api
service.
#
Basic UsageSymfony provides a TranslatorInterface
you can inject in your class, i.e.:
You can then use it as below:
note
📣  Most of the time, you don't have to use the TranslatorInterface
as either Symfony or the parent class call it for you.
#
Translations FolderFolder src/api/translations contains one YAML file per locale and domain.
A domain is a sort of scope. For instance, src/api/translations/emails.en.yaml and src/api/translations/emails.fr.yaml
are both for the email
domain (used for translating emails!).
Each of these files contains translation keys and the associated texts.
For instance:
note
📣  All files from the same domain should have the same organization (same translations keys, identical sorting, etc.).
#
ValidationLet's say you have a class with the following validation annotations:
The message
property of each Assert
annotation is a translation key from the validators
domain:
The web application implements a mechanism for setting the correct locale to translate these validation error messages (see interactions between the web application and the API chapter).
#
EmailsEmails translation uses the emails
domain. The corresponding YAML files are:
- src/api/translations/emails.en.yaml
- src/api/translations/emails.fr.yaml
Let's take a look at the CreateEmail
use case:
The method create
takes, among other arguments, a User
instance and the translation key
of the email's subject. The User
has a locale
property used for translating both the email's subject and its content.
See the Emails guide for more details on how to extend this use case.
The Twig templates of your emails should look like this:
The CreateEmail
use case will provide both domain
and locale
values.
#
SpreadsheetsSpreadsheets translation uses the spreadsheets
domain. The corresponding YAML files are:
- src/api/translations/spreadsheets.en.yaml
- src/api/translations/spreadsheets.fr.yaml
As you might want to translate the headers and cell values of your XLSX exports, the boilerplate provides examples on how of do so.
Let's take a look at the CreateXLSXExport
use case:
The method create
takes, among other arguments, a locale. It will use it to translate the spreadsheet's headers
accordingly.
For values, you should translate them directly in your use cases before calling the create
method.