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
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
Received Parameters: language_code
Function: Allows adding the _language_code variable to the instance.
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
Received Parameters: key, language, default
Function: Allows calling of attribute information.
get_attribute_kwargs_value
Received Parameters: key, language, default
Function: Allows calling of attribute_kwargs information.
clear_attributes
Received Parameters: language
Function: Allows the deletion of attributes, attribute_kwargs, localized_attributes and localized_attributes_kwargs information.
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
Received Parameters: language=None
Function: Allows getting all Instance attributes and localized_attributes values.
get_attributes_kwargs
Received Parameters: language=None
Function: Allows getting all Instance attributes_kwargs and localized_attribute_kwargs values.
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
Received Parameters: language=None
Function: Returns localized attribute values (localized_attribute).
get_localized_attributes_kwargs
Received Parameters: language=None *Function: Returns localized attribute_kwargs values (localized_attribute_kwargs).
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
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
>> country = Country.objects.first()
>> print(country.name)
Türkiye
>> country.language_as_dict('en-us')
{'name': 'Turkey'}
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 request creates a new instance of Country. If successful, the response status will be HTTP 201.
Path: /api/v1/countries/
{
"is_active": boolean, # mandatory
"name": string, # mandatory
"code": "de", # mandatory
"translations": JSONField # optional
}
Response
{
"pk": 12,
"is_active": true,
"name": "Germany",
"code": "de",
"translations": null,
"modified_date": "2023-01-04T17:40:49.214191Z",
"created_date": "2023-01-04T17:40:49.214163Z"
}
PATCH
Update Country
PATCH 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}/
{
"translations": {
"tr": {"name": "Almanya"},
"de": {"name": "Deutschland"},
"it": {"name": "Germania"}
}
}
Response
{
"pk": 12,
"is_active": true,
"name": "Germany",
"code": "de",
"translations": {
"de": {
"name": "Deutschland"
},
"tr": {
"name": "Almanya"
},
"it": {
"name": "Germania"
}
},
"modified_date": "2023-01-04T17:41:52.987683Z",
"created_date": "2023-01-04T17:40:13.065454Z"
}
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 request creates a new Attribute instance. If successful, the response status will be HTTP 201.
Path: /api/v1/attributes/
{
"name": "Erp Gender",
"data_type": "dropdown",
"entity_type_id": 2,
"erp_code": "erp_gender",
"is_filterable": false,
"is_form_field_required": false,
"is_form_required": false,
"is_localizable": false,
"is_required": false,
"is_searchable": true,
"is_variant": false,
"is_varian_listable": false,
"is_visible": true,
"key": "erp_gender",
"pre_attribute": true,
"translations": {"ar": {"name": "Erp الجنس"}, "en-us": {"name": "Erp Gender"}, "tr-tr": {"name": "ERP Cinsiyet"}}
}
Response
{
"pk": 61,
"key": "erp_gender",
"data_type": "dropdown",
"default_value": null,
"is_required": false,
"is_visible": true,
"is_searchable": true,
"is_filterable": false,
"is_variant": false,
"is_variant_listable": false,
"name": "Erp Gender",
"is_form_required": false,
"is_form_field_required": false,
"erp_code": "erp_gender",
"pre_attribute": true,
"description": null,
"is_localizable": false,
"modified_date": "2023-01-04T18:13:24.489211Z",
"created_date": "2023-01-04T18:13:24.489194Z",
"entity_type": 1,
}
GET
Attribute Translations
GET 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
{
"translations": {
"fr-fr": {
"name": "Couleur"
},
"ar": {
"name": "اللون"
},
"tr-tr": {
"name": "Renk"
}
}
}
Failed Response
If the attribute instance does not exist with the given pk, the response will be as follows:
{
"detail": "Not found."
}
PATCH
Update Attribute Translations
Attribute 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/
{
"translations": {
"fr-fr": {
"name": "Taille"
},
"ar": {
"name": "بحجم"
},
"tr-tr": {
"name": "Beden"
}
}
}
Response
{
"pk": 2,
"key": "beden",
"data_type": "dropdown",
"default_value": null,
"is_required": false,
"is_visible": false,
"is_searchable": false,
"is_filterable": false,
"is_variant": false,
"is_variant_listable": false,
"name": "Taille",
"is_form_required": false,
"is_form_field_required": false,
"erp_code": "erp_beden",
"pre_attribute": false,
"description": null,
"modified_date": "2023-01-04T18:45:59.196763Z",
"created_date": "2021-02-23T08:46:05.164000Z",
}