> For the complete documentation index, see [llms.txt](https://gurita-alexandru.gitbook.io/cloud-computing-2023-simpre/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gurita-alexandru.gitbook.io/cloud-computing-2023-simpre/cloudcomputing2022/translate-api/translate-api-integrare-si-utilizare.md).

# Translate API - Integrare și utilizare

Vom utiliza serviciul de traducere oferit de Google în **proiectul asociat Back-end**-ului

* **Instalați pachetul translate din google-cloud** - npm i ... în consolă

```
npm i @google-cloud/translate
```

* Introducem calea către credențialele furnizate de Google în .env

```
// .env
GOOGLE_APPLICATION_CREDENTIALS="./translateKey.json"
```

* Pentru a păstra proiectul organizat, creați un fișier nou în care vom introduce logica de traducere. Îl puteți numi translateFunctions.js
* Cele mai simple metode pe care le putem utiliza sunt **.detect(input), .translate(input, targetLanguage).** Pentru cazul în care ne-am dori să mai prelucrăm răspunsurile primite de la API, dar și pentru a ascunde complexitatea (importuri de funcții, creare de obiecte, etc), vom defini 2 funcții utile și le vom exporta

```
// translateFunctions.js
const {Translate} = require('@google-cloud/translate').v2;
const translateAPI = new Translate();

async function detectLanguage(inputText) {
    const languageDetection = await translateAPI.detect(inputText);
    return languageDetection;
}

async function translateText(inputText, targetLanguage) {
    const outputText = await translateAPI.translate(inputText, targetLanguage);
    return outputText;
}

module.exports = {
    detectLanguage,
    translateText
}
```

* Fiecare limbă pe care o putem trimite ca parametru în funcția translateText este reprezentată printr-un cod ISO. Putem astfel să definim un dicționar al limbilor - codurilor și să îl exportăm. **Creați fișierul dictionaries.js**

```
// dictionaries.js
const LANGUAGE_ISO_CODE = {
    'ENGLISH': 'en',
    'SPANISH': 'es',
    'FRENCH': 'fr',
    'GERMAN': 'de',
    'ITALIAN': 'it',
    'PORTUGUESE': 'pt',
    'ROMANIAN': 'ro',
    'SWEDISH': 'sv',
    'DUTCH': 'nl',
    'FINNISH': 'fi',
    'NORWEGIAN': 'no',
    'POLISH': 'pl',
    'HUNGARIAN': 'hu',
};

module.exports = {
    LANGUAGE_ISO_CODE,
};
```

* Putem testa că programul nostru funcționează importând cele 2 funcții în index.js și apelându-le (observați că am introdus cele 2 fișiere noi într-un **folder utils**)

```
// index.js
const {detectLanguage, translateText} = require("./utils/translateFunctions.js");
const {LANGUAGE_ISO_CODE} = require("./utils/dictionaries.js");

const processLanguage = async (text) => {
  const languageDetection = await detectLanguage(text);
  const translatedText = await translateText(text, LANGUAGE_ISO_CODE.ROMANIAN);
  console.log(languageDetection);
  console.log(translatedText);
}

processLanguage("Hello world! This is my first time using Google Translate API!");
```

![](/files/u9mwA1PnSDJrrzbynv09)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://gurita-alexandru.gitbook.io/cloud-computing-2023-simpre/cloudcomputing2022/translate-api/translate-api-integrare-si-utilizare.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.
