Azure Translator Code
The Azure Translator Code library is a powerful tool for translating JSON files into multiple languages using the Azure Cognitive Translator service. This library supports translation of JSON files in multiple folders or within a single folder, depending on your needs.
NPM PAGE: https://www.npmjs.com/package/azure-translator-code
GITHUB PAGE: https://github.com/gabriel-logan/Azure-translator-code
You con test the library in the following link: https://azuretranslatorcode.vercel.app
Installation
To get started, you can install the library via npm:
As devDependencies
npm install -D azure-translator-code
You also can use yarn
yarn add -D azure-translator-code
or
As dependencies
npm install azure-translator-code
You also can use yarn
yarn add azure-translator-code
Usage
You can import the JSON file you want to translate in two different ways:
Importing a JSON File
const jsonFile = require('./jsonFileToTranslate/en.json');
const jsonFile = {
"HomePage": {
"welcome": 'Welcome',
"hello": 'Hello',
"SubText": {
"subText": 'This is a subtext'
}
}
}
Now, you can use the library to translate the JSON file into multiple languages:
const { translateToMultipleFolders, translateToUnicFolder, updateTranslationsMulti, updateTranslationsUnic } = require('azure-translator-code');
or
import { translateToMultipleFolders, translateToUnicFolder, updateTranslationsMulti, updateTranslationsUnic } from 'azure-translator-code';
const key = 'sds12312a213aaaa9b2d0c37eds37b';
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);
You can also choose the folder or folder name where you will save the files.
Saving always starts from the project root folder.
const { translateToMultipleFolders, translateToUnicFolder } = require('azure-translator-code');
const key = 'sds12312a213aaaa9b2d0c37eds37b';
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');
translateToUnicFolder(key, endpoint, location, fromLang, toLangs, jsonFile, './myFolder/OtherFolder/etc');
updateTranslationsMulti(key, endpoint, location, fromLang, toLangs, jsonFile, 'myFolder');
Translate and Log the result
You can also log the result of the translation in the console.
const { translate } = require('azure-translator-code');
const key = 'sds12312a213aaaa9b2d0c37eds37b';
const endpoint = 'https://api.cognitive.microsofttranslator.com/';
const location = 'eastus';
const fromLang = 'en';
const toLang = 'pt';
const jsonFile = {
HomePage: {
Welcome: "Welcome",
Hello: "Hello",
},
};
translate(key, endpoint, location, fromLang, toLang, jsonFile).then((result) => console.log(result));
Make sure to replace the key and endpoint information with your own Azure access credentials. Ensure that the JSON file and settings are correctly defined according to your needs.
Contributing
If you wish to contribute to this project, feel free to open an issue or submit a pull request on our GitHub repository.