Translations
Sylius uses the approach of personal translations - where each entity is bound with a translation entity, that has its table (instead of keeping all translations in one table for the whole system). This results in the ProductTranslation
class and sylius_product_translation
table for the Product
entity.
The logic of handling translations in Sylius is in the ResourceBundle
The fields of an entity that are meant to be translatable are saved on the translation entity, only their getters and setters are also on the original model.
Let’s see an example:
Assuming that we would like to have a translatable model of a Supplier
, we need a Supplier class and a SupplierTranslation class.
The actual entity has access to its translation by using the TranslatableTrait
which provides the getTranslation()
method.
Remember that the Translations collection of the entity (from the TranslatableTrait) has to be initialized in the constructor!
Fallback Translations
The getTranslation()
method gets a translation for the current locale, while we are in the shop, but we can also manually impose the locale - getTranslation('pl_PL')
will return a Polish translation if there is a translation in this locale.
But when the translation for the chosen locale is unavailable, instead the translation for the fallback locale (the one that was either set in config/services.yaml
or using the setFallbackLocale()
method from the TranslatableTrait on the entity) is used.
How to add a new translation programmatically?
You can programmatically add a translation to any of the translatable resources in Sylius. Let’s see how to do it in the example of a ProductTranslation.
Learn more
Last updated