Localization
Django Standard Translation
Translations are related to the language settings users need.
Internationalization and Localization
Internationalization and Localization allow format and language adjustments in a Django project to meet user needs. Accordingly, Django supports text translation, date, time and number formatting and time zone usage. In this context, it is determined in which parts of the project applications (apps) the format and language conversions will be made, and it is ensured that the relevant applications are presented according to the language preferences made by the users. Internationalization means making the project suitable for language and format conversions. This process is carried out by the developer. Localization, on the other hand, is the writing of local formats with translations. This process is carried out by translators.
Translating Product Model Fields into Different Languages
omnitron.products.models.Product
model is inherited from omnicore.products.models.BaseProduct
model, which is inherited from omnicore.eav.models.Entity
abstract class model. In the entity model, there are localized_attributes
and localized_attributes_kwargs
fields related to localization. The set_translatable_field
method defined in the model allows the selected fields to be selected for translation. In the product model, the "name" space is defined for localization. This way, product names can be translated into the languages allowed in the settings according to the user’s preferences.
Model, Service and ViewSet
In the Meta class in the models in Omnitron, the translatable fields of the model are indicated as translatable_fields
. The addition of the required translations as model fields is made for the areas specified in translatable_fields over TranslatableModelMixin
using the nece python package. In the models where Mixin is used, a field named translations is formed and translation information is included in the form of key:value.
The methods in the omnicore.eav.models.Entity
model inherited by the product model allow translations of the fields specified in translatable_fields. Attributes can be added with the set_attribute method in the model. If the is_localizable
property of the Attribute is True, the added property is added to localized_attributes
.
Entity Model Methods
The Entity Model inherited by the Product Model enables product attributes to be translatable. The defined methods from the Entity Model are listed below.
set_translatable_field
set_translatable_field
Received Parameters: key, value, language=None
Function: Allows adding attributes to the instance. If the key value given as input is defined in translatable_fields, it is added as localized_attribute; if not, it is added as attributes_kwargs
.
set_language
set_language
Received Parameters: language_code
Function: Allows adding the _language_code variable to the instance.
set_attribute_language
set_attribute_language
Received Parameters: language_code
Function: Adds localized attribute values to the instance and updates the attribute values according to the selected language.
get_attribute_value
get_attribute_value
Received Parameters: key, language, default
Function: Allows calling of attribute information.
get_attribute_kwargs_value
get_attribute_kwargs_value
Received Parameters: key, language, default
Function: Allows calling of attribute_kwargs information.
clear_attributes
clear_attributes
Received Parameters: language
Function: Allows the deletion of attributes, attribute_kwargs, localized_attributes and localized_attributes_kwargs information.
set_attribute
set_attribute
Received Parameters: key, value, language=None
Function: Allows recording the attributes, attributes_kwargs, localized_attributes and localized_attributes_kwargs information to the Product instance. Allows attribute and attribute_kwargs with is_localizable value to be saved as localized_atributes and localized_attribute_kwargs.
get_attributes
get_attributes
Received Parameters: language=None
Function: Allows getting all Instance attributes and localized_attributes values.
get_attributes_kwargs
get_attributes_kwargs
Received Parameters: language=None
Function: Allows getting all Instance attributes_kwargs and localized_attribute_kwargs values.
default_language
default_language
Received Parameters: -
Function: Returns the value LANGUAGE_CODE, which is defined in settings and specifies the default language option.
get_localized_attributes
: function
get_localized_attributes
: functionReceived Parameters: language=None
Function: Returns localized attribute values (localized_attribute).
get_localized_attributes_kwargs
get_localized_attributes_kwargs
Received Parameters: language=None *Function: Returns localized attribute_kwargs values (localized_attribute_kwargs).
get_merged_attributes
get_merged_attributes
Received Parameters: language=None
Function: Allows all attribute and attribute_kwargs of the instance to be returned together.
get_translatable_field_keys
get_translatable_field_keys
Received Parameters: -
Function: Returns the key information in the "translatable_fields" value of the model.
Models with Translatable Fields
The following models in Omnitron (and other models that inherit from these models) are models that can take the translatable_fields value in the Meta class, which allows translation according to the selected language. Other information about the models is provided in the table.
App
Model
translatable_field
Translation Inheritance From
Serializer
products
Product
name
Entity
ProductSerializer
products
Attribute
name
TranslatableModelMixin
AttributeSerializerWithIsLocalizable
products
AttributeValue
label
TranslatableModelMixin
AttributeValueSerializer
address
Country
name
TranslatableModelMixin
CountrySerializer
address
City
name
TranslatableModelMixin
CitySerializer
address
RetailStore
name
TranslatableModelMixin
PaymentOptionSerializer
payments
Installment
label
TranslatableModelMixin
InstallmentSerializer
orders
CancellationReason
subject
TranslatableModelMixin
CancellationReasonSerializer
catalogs
CategoryNode
name
Entity
CategoryNodeSerializer
search
SortOption
label
TranslatableModelMixin
SortOptionSerializer
TranslatableModelMixin
TranslatableModelMixin is inherited by many of the models in Omnitron defined as translatable_fields. The list of these models can be found above.The translation values of the fields specified for model localization are created in JSONB format as a translation field object is returned. Returns None if there is no translation for the specified language.
language_as_dict
Received Parameters: language_code
Function: With language_code, it returns the key-value (dictionary) in the translations field of the desired language.
Example
TranslationModel also inherits TranslationMixin from the nece package. This mixin class takes the TRANSLATIONS_DEFAULT and TRANSLATIONS_MAP values from the project settings. If these values are not specified in the project, the value en_us is set as the default language option.
HTTP Request Examples
Examples are based on the omnitron.address.Country model, which includes a translations field. The model inherits TranslatableModelMixin.
POST
Create Country
POST
Create CountryPOST request creates a new instance of Country. If successful, the response status will be HTTP 201.
Path: /api/v1/countries/
Response
PATCH
Update Country
PATCH
Update CountryPATCH request updates an existing instance. If the body of the request includes the translations
field regarding values for languages, then the instance's translatable fields can be translated into these languages.
Path: /api/v1/countries/{pk}/
Response
Product Model
Since an attribute or attribute value of a product in the Product model inherits from the TranslatableModelMixin class, it comes with a translations field. In order to access translations of an attribute or attribute value in different languages, a request can be made by adding "translations" to the url. Examples are listed below.
Attribute adds new objects to the table. With the JSONField, which can be provided as the translation value in the Request Body, the translations of the name field in different languages are included in the translations field in the object. The AttributeSerializer class at omnitron.products.resources.serializers is used for data verification.
POST
Create Attribute
POST
Create AttributePOST request creates a new Attribute instance. If successful, the response status will be HTTP 201.
Path: /api/v1/attributes/
Response
GET
Attribute Translations
GET
Attribute TranslationsGET request to the translation endpoint gives the available translatable fields and their translations.
Path: /api/v1/attributes/{pk}/translations/
The response is returned upon serializing the object specified with the primary key from the Attribute table together with the AttributeTranslationSerializer class defined at omnitron.products.resources.serializers.
Response
Failed Response
If the attribute instance does not exist with the given pk, the response will be as follows:
PATCH
Update Attribute Translations
PATCH
Update Attribute TranslationsAttribute updates the object specified with the primary key from the table. Updates the translations field according to the values in JSON. The AttributeSerializer class at omnitron.products.resources.serializers is used for data verification.
Path: /api/v1/attributes/{pk}/translations/
Response
Last updated
Was this helpful?