At this stage, model translations are only accessible from the Python code. The backend and the storefront are prepared to handle the translated properties, but GraphQL API and UI views will be added in the future releases.
Model translations are available via
TranslationProxy defined on the to-be-translated
TranslationProxy gets user’s language, and checks if there’s a ModelTranslation created for that language.
If there’s no relevant
ModelTranslation available, it will return the original (therefore not translated) property. Otherwise, it will return the translated property.
Consider a product:
from django.db import models from saleor.core.utils.translations import TranslationProxy class Product(models.Model): name = models.CharField(max_length=128) description = models.CharField(max_length=256) ... translated = TranslationProxy()
The product has several properties, but we want to translate just its
We’ve also set a
translated property to an instance of
We will use
ProductTranslation to store our translated properties, it requires two base fields:
A language code that this translation correlates to.
ForeignKey relation to the translated object (in this case we named it product)
… and any other field you’d like to translate, in our example, we will use
TranslationProxyexpects that the
related_name, on the
ForeignKeyrelation is set to
from django.db import models class ProductTranslation(models.Model): language_code = models.CharField(max_length=10) product = models.ForeignKey( Product, related_name="translations", on_delete=models.CASCADE, ) name = models.CharField(max_length=128) description = models.CharField(max_length=256) class Meta: unique_together = ("product", "language_code")
Don’t forget to set
language_code, there should be only one translation per product per language.
ModelTranslationfields must always take the same arguments as the existing translatable model, eg. inconsistency in
max_lengthattribute could lead to UI bugs with translation turned on.
Given the example above, we can access translated properties via the
translated_name = product.translated.name
Translated property will be returned if there is a
ModelTranslationwith the same
language_codeas a user’s currently active language. Otherwise, the original property will be returned.