Azure Translator Code
Azure Translator Code is a powerful library for translating JSON files into multiple languages using the Azure Cognitive Translator service. This library supports translating JSON files located in multiple folders or within a single folder, depending on your needs.
Important Links
Installation
Install the library using npm or yarn:
As Development Dependency
npm install -D azure-translator-code
or
yarn add -D azure-translator-code
As Production Dependency
npm install azure-translator-code
or
yarn add azure-translator-code
Usage
You can import the JSON file you want to translate in two ways:
Importing a JSON File
const jsonFile = require("./jsonFileToTranslate/en.json");
const jsonFile = {
HomePage: {
welcome: "Welcome",
hello: "Hello",
SubText: {
subText: "This is a subtext",
},
},
};
Translating JSON to Multiple Languages
After importing the JSON file, you can use the library to translate it:
Importing the Functions
const {
translate,
translateText,
translateToMultipleFolders,
translateToUnicFolder,
updateTranslationsMulti,
updateTranslationsUnic,
} = require("azure-translator-code");
import {
translate,
translateText,
translateToMultipleFolders,
translateToUnicFolder,
updateTranslationsMulti,
updateTranslationsUnic,
} from "azure-translator-code";
import type { TranslationType } from "azure-translator-code";
Define your Azure API details and languages:
const key = "YOUR_AZURE_KEY";
const endpoint = "https://api.cognitive.microsofttranslator.com/";
const location = "eastus";
const fromLang = "en";
const toLangs = [
"pt",
"de",
"es",
"fr",
"it",
"ja",
"ko",
"nl",
"ru",
"zh",
"pt-pt",
"ar",
"tlh-Latn",
];
const jsonFile = {
translation: {
welcome: "Welcome",
hello: "Hello",
},
};
translateToMultipleFolders(key, endpoint, location, fromLang, toLangs, jsonFile);
translateToUnicFolder(key, endpoint, location, fromLang, toLangs, jsonFile);
updateTranslationsMulti(key, endpoint, location, fromLang, toLangs, jsonFile);
updateTranslationsUnic(key, endpoint, location, fromLang, toLangs, jsonFile);
Customizing Folder Structure
You can specify the folder name or location where translations will be saved. Saving always starts from the project root folder.
const key = "YOUR_AZURE_KEY";
const endpoint = "https://api.cognitive.microsofttranslator.com/";
const location = "eastus";
const fromLang = "en";
const toLangs = ["pt", "de"];
const jsonFile = {
translation: {
welcome: "Welcome",
hello: "Hello",
},
};
translateToMultipleFolders(
key,
endpoint,
location,
fromLang,
toLangs,
jsonFile,
"myFolder",
);
translateToUnicFolder(
key,
endpoint,
location,
fromLang,
toLangs,
jsonFile,
"./myFolder/OtherFolder/etc",
);
Translating and Logging Results
You can also log the translation results to the console for verification.
const { translate, translateText } = require("azure-translator-code");
const key = "YOUR_AZURE_KEY";
const endpoint = "https://api.cognitive.microsofttranslator.com/";
const location = "eastus";
const fromLang = "en";
const toLangs = ["pt"];
const jsonFile = {
HomePage: {
Welcome: "Welcome",
Hello: "Hello",
},
};
translate(key, endpoint, location, fromLang, toLangs, jsonFile).then((res) => {
console.log(res);
});
translateText("Hello World!", fromLang, toLangs, endpoint, key, location).then(
(res) => {
console.log(res[0].translations);
},
);
Make sure to replace the key
, location
and endpoint
with your actual Azure access credentials.
Contributing
If you would like to contribute to this project, feel free to open an issue or submit a pull request on our GitHub repository.