# 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/`

```json
{
    "is_active": boolean,         # mandatory
    "name": string,               # mandatory
    "code": "de",                 # mandatory
    "translations": JSONField     # optional
}

```

**Response**

```json
{
    "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}/`

```json
{
    "translations": {
        "tr": {"name": "Almanya"},
        "de": {"name": "Deutschland"},
        "it": {"name": "Germania"}
        }
}
```

**Response**

```json
  {
    "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/`

```json
{
    "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**

```json
 {
    "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**

```json
 {
    "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:

```json
 {
    "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/`

```json
 {
    "translations": {
        "fr-fr": {
            "name": "Taille"
        },
        "ar": {
            "name": "بحجم"
        },
        "tr-tr": {
            "name": "Beden"
        }
    }
}
```

**Response**

```json
 {
    "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",
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://apidocs.akinon.com/omnitron/pim/localization.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
