-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement method to differentiate browser from node and use the…
… correct api and update translation logic
- Loading branch information
1 parent
1965a2b
commit 2482822
Showing
8 changed files
with
533 additions
and
74 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import axios from "axios"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
|
||
import type { TranslationType } from "../types"; | ||
|
||
interface Translation { | ||
text: TranslationType; | ||
to: TranslationType; | ||
} | ||
|
||
interface TranslationResponse { | ||
translations: Translation[]; | ||
} | ||
|
||
type TranslateResponseData = TranslationResponse[]; | ||
|
||
/** | ||
* | ||
* @param text - The text to translate. | ||
* @param from - The language code of the source language. | ||
* @param to - The languages code of the target languages. | ||
* @param endpoint - The Azure Translator endpoint URL. | ||
* @param key - The Azure Translator subscription key. | ||
* @param location - The Azure Translator subscription region. | ||
* @returns A Promise that resolves to the translated text. | ||
* @example | ||
* from: "en" | ||
* to: ["es", "fr"] | ||
* | ||
* translateText("Hello", "en", ["es", "fr"], "https://api.cognitive.microsofttranslator.com", "key", "location") | ||
* | ||
* Returns example: | ||
[ | ||
{ | ||
translations: [ | ||
{ text: "Hola", to: "es" }, | ||
{ text: "Bonjour", to: "fr" } | ||
] | ||
} | ||
] | ||
*/ | ||
export default async function translateText( | ||
text: string, | ||
from: string, | ||
to: string[], | ||
endpoint: string, | ||
key: string, | ||
location: string, | ||
): Promise<TranslateResponseData> { | ||
const filteredEndpoint = endpoint.replace(/\/$/, ""); | ||
const toDestructured = to.map((lang) => `to=${lang}`).join("&"); | ||
const url = `${filteredEndpoint}/translate?api-version=3.0&from=${from}&${toDestructured}`; | ||
|
||
const headers = { | ||
"Ocp-Apim-Subscription-Key": key, | ||
"Ocp-Apim-Subscription-Region": location, | ||
"Content-type": "application/json", | ||
"X-ClientTraceId": uuidv4(), | ||
}; | ||
|
||
const payload = [{ text }]; | ||
|
||
if (typeof window !== "undefined" && typeof window.fetch === "function") { | ||
const response = await fetch(url, { | ||
method: "POST", | ||
headers, | ||
body: JSON.stringify(payload), | ||
}); | ||
|
||
return await response.json(); | ||
} else { | ||
const response = await axios.post<TranslateResponseData>(url, payload, { | ||
headers, | ||
}); | ||
|
||
return response.data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import { translateText } from "../../src"; | ||
|
||
describe("translateText", () => { | ||
let fromLang: string; | ||
let toLangs: string[]; | ||
let endpoint: string; | ||
let key: string; | ||
let location: string; | ||
|
||
beforeAll(() => { | ||
fromLang = "en"; | ||
toLangs = ["pt"]; | ||
endpoint = "https://api.cognitive.microsofttranslator.com/translate"; | ||
key = "12345"; | ||
location = "eastus"; | ||
}); | ||
|
||
it("window.fetch is defined and should be called", async () => { | ||
global.fetch = jest.fn(() => | ||
Promise.resolve({ | ||
json: () => | ||
Promise.resolve({ | ||
translations: [ | ||
{ | ||
text: "Bem-vindo", | ||
}, | ||
], | ||
}), | ||
}), | ||
) as jest.Mock; | ||
|
||
const result: any = await translateText( | ||
"Welcome", | ||
fromLang, | ||
toLangs, | ||
endpoint, | ||
key, | ||
location, | ||
); | ||
|
||
expect(result.translations[0].text).toEqual("Bem-vindo"); | ||
}); | ||
}); |
Oops, something went wrong.