> For the complete documentation index, see [llms.txt](https://ai-assistant.sunbird.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ai-assistant.sunbird.org/components/sakhi-api-service/pluggability-of-transaltion-service.md).

# Pluggability of Transaltion service

In this guide, we will learn how to plugin a new translation service by extending the `BaseTranslationClass` interface.

&#x20;You can seamlessly leverage it within existing Sakhi API workflows with minimal code changes.

### Integration <a href="#implementation" id="implementation"></a>

Translation service is used for supporting multiple languages in Sakhi-api-service.

### BaseTranslationClass <a href="#base-chat-model" id="base-chat-model"></a>

This `BaseTranslationClass` class has below methods which are to be implemented mandatorily.

| Method/Property  | Description                                                  | Required/Optional |
| ---------------- | ------------------------------------------------------------ | ----------------- |
| \_\_init\_\_     | Method to initialise or define necessary variables           | Optional          |
| translate\_text  | This method translates a text string to another language.    | Required          |
| text\_to\_speech | This method converts text to speech in a specified language. | Required          |
| speech\_to\_text | This method converts speech from an audio file to text.      | Required          |

### Implementation <a href="#implementation" id="implementation"></a>

Let's say we create a `YourTranslationClass` class inheriting from `BaseTranslationClass.` The class needs to implement the following methods translate\_text, text\_to\_speech and speech\_to\_text.

Add the necessary environment variables to  `.env` file. `TRANSLATION_TYPE` is to be defined mandatorily.

```
TRANSLATION_TYPE=your_translation_service
```

Now go to the 'translation' folder and create your plugin '`your_translation_service.py`'. Provide the implementation for the abstract methods.&#x20;

{% code title="your\_translation\_service.py" %}

```python

from translation.base import BaseTranslationClass


class YourTranslationClass(BaseTranslationClass):

    def translate_text(self, text: str, source: str, destination: str) -> str:
        # code to translate text
        return translated_text

    def text_to_speech(self, language: str, text: str) -> Any:
        return base_64_decoded_audio
        
    def speech_to_text(self, audio_file: Any, input_language: str) -> str:
        return transcripted_text    
```

{% endcode %}

Go to the 'translation' folder and update `__init__.py` with the module lookup entry for "YourTranslationClass" and also for TYPE\_CHECKING.

```python
if TYPE_CHECKING:
    ...
    from translation.your_translation_service import (
        YourTranslationClass
    )

_module_lookup = {
    ...,
    "YourTranslationClass": "translation.your_translation_service"
}
```

Modify `env_manager.py` to import `YourTranslationClass`  and add a mapping for `YourTranslationClass` in the `self.indexes` dictionary.

```python
from translation import (
    ...,
    YourTranslationClass
)
```

<pre class="language-python"><code class="lang-python"><strong>self.indexes = {
</strong>    "translation": {
        "class": {
            ...,
            "your_translation_service": YourTranslationClass
        },
        "env_key": "TRANSLATION_TYPE"
    }
}
</code></pre>

This setup ensures that YourTranslationClass can be instantiated based on specific environment variables, effectively integrating it into the environment management system. The `self.indexes` the dictionary now includes a mapping where `"your_translation_service"` corresponds to the `"YourTranslationClass"` class and uses `"TRANSLATION_TYPE"` as the environment key.
