New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

azure-translator-code

Package Overview
Dependencies
Maintainers
2
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

azure-translator-code - npm Package Compare versions

Comparing version

to
1.0.6

4

javascript/index.js

@@ -7,5 +7,5 @@ "use strict";

exports.translateToUnicFolder = exports.translateToMultipleFolders = void 0;
const translateToMultipleFolders_1 = __importDefault(require("./translateToMultipleFolders"));
var translateToMultipleFolders_1 = __importDefault(require("./translateToMultipleFolders"));
exports.translateToMultipleFolders = translateToMultipleFolders_1.default;
const translateToUnicFolder_1 = __importDefault(require("./translateToUnicFolder"));
var translateToUnicFolder_1 = __importDefault(require("./translateToUnicFolder"));
exports.translateToUnicFolder = translateToUnicFolder_1.default;
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {

@@ -6,13 +42,13 @@ return (mod && mod.__esModule) ? mod : { "default": mod };

Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = __importDefault(require("axios"));
const uuid_1 = require("uuid");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
function translateToMultipleFolders(key, endpoint, location, fromLang, toLangs, jsonFile) {
const folderName = 'multiFolderGeneratedTranslations';
const traducoesDir = path_1.default.join(__dirname, '..', '..', '..', '..', folderName);
var axios_1 = __importDefault(require("axios"));
var uuid_1 = require("uuid");
var fs_1 = __importDefault(require("fs"));
var path_1 = __importDefault(require("path"));
function translateToMultipleFolders(key, endpoint, location, fromLang, toLangs, jsonFile, folderNamePath) {
if (folderNamePath === void 0) { folderNamePath = 'multiFolderGeneratedTranslations'; }
var traducoesDir = path_1.default.join(__dirname, '..', '..', '..', '..', folderNamePath);
if (!fs_1.default.existsSync(traducoesDir)) {
fs_1.default.mkdirSync(traducoesDir);
fs_1.default.mkdirSync(traducoesDir, { recursive: true });
}
const { translation } = jsonFile;
var translation = jsonFile.translation;
function translateText(text, from, to) {

@@ -42,36 +78,74 @@ return (0, axios_1.default)({

}
async function translateAndSave(lang) {
const translations = {};
for (const key in translation) {
try {
const response = await translateText(translation[key], fromLang, lang);
const translatedText = response.data[0].translations[0].text;
translations[key] = translatedText;
console.log(`Traduzindo ${translation[key]} para ${lang} \n\n`);
}
catch (error) {
if (error instanceof Error) {
console.error(`Erro ao traduzir "${key}" para ${lang}: ${error.message} \n`);
function translateAndSave(lang) {
return __awaiter(this, void 0, void 0, function () {
var translations, _a, _b, _c, _i, key_1, response, translatedText, error_1, langDir, outputFileName;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
translations = {};
_a = translation;
_b = [];
for (_c in _a)
_b.push(_c);
_i = 0;
_d.label = 1;
case 1:
if (!(_i < _b.length)) return [3, 6];
_c = _b[_i];
if (!(_c in _a)) return [3, 5];
key_1 = _c;
_d.label = 2;
case 2:
_d.trys.push([2, 4, , 5]);
return [4, translateText(translation[key_1], fromLang, lang)];
case 3:
response = _d.sent();
translatedText = response.data[0].translations[0].text;
translations[key_1] = translatedText;
console.log("Translating ".concat(translation[key_1], " to ").concat(lang, " \n\n"));
return [3, 5];
case 4:
error_1 = _d.sent();
if (error_1 instanceof Error) {
console.error("Error translating \"".concat(key_1, "\" to ").concat(lang, ": ").concat(error_1.message, " \n"));
}
else {
console.error("An error occurred within the error (: \n");
}
return [3, 5];
case 5:
_i++;
return [3, 1];
case 6:
langDir = path_1.default.join(traducoesDir, lang);
if (!fs_1.default.existsSync(langDir)) {
fs_1.default.mkdirSync(langDir);
}
outputFileName = path_1.default.join(langDir, "".concat(lang, ".json"));
fs_1.default.writeFileSync(outputFileName, JSON.stringify({ translation: translations }, null, 4));
console.log("Translations for ".concat(lang, " saved in ").concat(outputFileName, " \n\n"));
return [2];
}
else {
console.error(`Algum erro aconteceu no erro (: \n`);
});
});
}
function translateAndSaveAll() {
return __awaiter(this, void 0, void 0, function () {
var translationPromises;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
translationPromises = toLangs.map(function (lang) { return translateAndSave(lang); });
return [4, Promise.all(translationPromises)];
case 1:
_a.sent();
return [2];
}
}
}
const langDir = path_1.default.join(traducoesDir, lang);
if (!fs_1.default.existsSync(langDir)) {
fs_1.default.mkdirSync(langDir);
}
const outputFileName = path_1.default.join(langDir, `${lang}.json`);
fs_1.default.writeFileSync(outputFileName, JSON.stringify({ translation: translations }, null, 4));
console.log(`Traduções para ${lang} salvas em ${outputFileName} \n\n`);
});
});
}
async function translateAndSaveAll() {
const translationPromises = toLangs.map((lang) => translateAndSave(lang));
await Promise.all(translationPromises);
}
translateAndSaveAll().catch((error) => {
console.error(`Erro ao traduzir e salvar textos: ${error.message} \n`);
translateAndSaveAll().catch(function (error) {
console.error("Error translating and saving texts: ".concat(error.message, " \n"));
});
}
exports.default = translateToMultipleFolders;
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {

@@ -6,13 +42,13 @@ return (mod && mod.__esModule) ? mod : { "default": mod };

Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = __importDefault(require("axios"));
const uuid_1 = require("uuid");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
function translateToUnicFolder(key, endpoint, location, fromLang, toLangs, jsonFile) {
const folderName = 'unicFolderGeneratedTranslations';
const traducoesDir = path_1.default.join(__dirname, '..', '..', '..', '..', folderName);
var axios_1 = __importDefault(require("axios"));
var uuid_1 = require("uuid");
var fs_1 = __importDefault(require("fs"));
var path_1 = __importDefault(require("path"));
function translateToUnicFolder(key, endpoint, location, fromLang, toLangs, jsonFile, folderName) {
if (folderName === void 0) { folderName = 'unicFolderGeneratedTranslations'; }
var traducoesDir = path_1.default.join(__dirname, '..', '..', '..', '..', folderName);
if (!fs_1.default.existsSync(traducoesDir)) {
fs_1.default.mkdirSync(traducoesDir);
fs_1.default.mkdirSync(traducoesDir, { recursive: true });
}
const { translation } = jsonFile;
var translation = jsonFile.translation;
function translateText(text, from, to) {

@@ -42,36 +78,75 @@ return (0, axios_1.default)({

}
const outputData = {};
async function translateAndSave(lang) {
const translations = {};
for (const key in translation) {
try {
const response = await translateText(translation[key], fromLang, lang);
const translatedText = response.data[0].translations[0].text;
translations[key] = translatedText;
console.log(`Traduzindo ${translation[key]} para ${lang} \n\n`);
}
catch (error) {
if (error instanceof Error) {
console.error(`Erro ao traduzir "${key}" para ${lang}: ${error.message} \n`);
var outputData = {};
function translateAndSave(lang) {
return __awaiter(this, void 0, void 0, function () {
var translations, _a, _b, _c, _i, key_1, response, translatedText, error_1;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
translations = {};
_a = translation;
_b = [];
for (_c in _a)
_b.push(_c);
_i = 0;
_d.label = 1;
case 1:
if (!(_i < _b.length)) return [3, 6];
_c = _b[_i];
if (!(_c in _a)) return [3, 5];
key_1 = _c;
_d.label = 2;
case 2:
_d.trys.push([2, 4, , 5]);
return [4, translateText(translation[key_1], fromLang, lang)];
case 3:
response = _d.sent();
translatedText = response.data[0].translations[0].text;
translations[key_1] = translatedText;
console.log("Translating ".concat(translation[key_1], " to ").concat(lang, " \n\n"));
return [3, 5];
case 4:
error_1 = _d.sent();
if (error_1 instanceof Error) {
console.error("Error translating \"".concat(key_1, "\" to ").concat(lang, ": ").concat(error_1.message, " \n"));
}
else {
console.error("An error occurred within the error (: \n");
}
return [3, 5];
case 5:
_i++;
return [3, 1];
case 6:
outputData[lang] = translations;
return [2];
}
else {
console.error(`Algum erro aconteceu no erro (: \n`);
});
});
}
function translateAndSaveAll() {
return __awaiter(this, void 0, void 0, function () {
var translationPromises, _i, toLangs_1, lang, outputFileName;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
translationPromises = toLangs.map(function (lang) { return translateAndSave(lang); });
return [4, Promise.all(translationPromises)];
case 1:
_a.sent();
for (_i = 0, toLangs_1 = toLangs; _i < toLangs_1.length; _i++) {
lang = toLangs_1[_i];
outputFileName = path_1.default.join(traducoesDir, "".concat(lang, ".json"));
fs_1.default.writeFileSync(outputFileName, JSON.stringify({ translation: outputData[lang] }, null, 4));
console.log("Translations for ".concat(lang, " saved in ").concat(outputFileName, " \n\n"));
}
return [2];
}
}
}
outputData[lang] = translations;
});
});
}
async function translateAndSaveAll() {
const translationPromises = toLangs.map((lang) => translateAndSave(lang));
await Promise.all(translationPromises);
for (const lang of toLangs) {
const outputFileName = path_1.default.join(traducoesDir, `${lang}.json`);
fs_1.default.writeFileSync(outputFileName, JSON.stringify({ translation: outputData[lang] }, null, 4));
console.log(`Traduções para ${lang} salvas em ${outputFileName} \n\n`);
}
}
translateAndSaveAll().catch((error) => {
console.error(`Erro ao traduzir e salvar textos: ${error.message} \n`);
translateAndSaveAll().catch(function (error) {
console.error("Error translating and saving texts: ".concat(error.message, " \n"));
});
}
exports.default = translateToUnicFolder;
{
"name": "azure-translator-code",
"version": "1.0.5",
"version": "1.0.6",
"description": "Azure Cognitive Services Translator Text API Code for Use with Common Languages",

@@ -11,3 +11,4 @@ "author": {

"scripts": {
"test": ""
"test": "jest",
"test:silent": "jest --watchAll --silent --noStackTrace"
},

@@ -23,3 +24,5 @@ "repository": {

"translator",
"translator code"
"translator code",
"i18n json generator",
"i18n"
],

@@ -40,2 +43,3 @@ "license": "MIT",

"devDependencies": {
"@types/jest": "^29.5.5",
"@types/node": "^20.6.2",

@@ -48,3 +52,5 @@ "@types/uuid": "^9.0.4",

"eslint-plugin-prettier": "^5.0.0",
"jest": "^29.7.0",
"prettier": "^3.0.3",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",

@@ -51,0 +57,0 @@ "typescript": "^5.2.2"

@@ -61,7 +61,9 @@ # Azure Translator Code

const { translateToMultipleFolders, translateToUnicFolder } = require('azure-translator-code');
// or
```
or
```javascript
import { translateToMultipleFolders, translateToUnicFolder } from 'azure-translator-code';
```
```javascript
const key = 'sds12312a213aaaa9b2d0c37eds37b'; // REPLACE WITH YOUR OWN KEY HERE

@@ -84,3 +86,3 @@ const endpoint = 'https://api.cognitive.microsofttranslator.com/';

'ar',
'tlh-Latn',
'tlh-Latn'
];

@@ -91,3 +93,3 @@

"welcome": "Welcome",
"hello": "Hello",
"hello": "Hello"
}

@@ -98,3 +100,3 @@ };

translateToMultipleFolders(key, endpoint, location, fromLang, toLangs, jsonFile);
// This function will return a folder called folder multiFolderGeneratedTranslations
// This function will return a folder called multiFolderGeneratedTranslations

@@ -105,5 +107,41 @@

translateToUnicFolder(key, endpoint, location, fromLang, toLangs, jsonFile);
// This function will return a folder called folder unicFolderGeneratedTranslations
// This function will return a folder called unicFolderGeneratedTranslations
```
#### You can also choose the folder or folder name where you will save the files.
##### Saving always starts from the project root folder.
```javascript
const { translateToMultipleFolders, translateToUnicFolder } = require('azure-translator-code');
const key = 'sds12312a213aaaa9b2d0c37eds37b'; // REPLACE WITH YOUR OWN KEY HERE
const endpoint = 'https://api.cognitive.microsofttranslator.com/';
const location = 'eastus';
const fromLang = 'en';
const toLangs = ['pt', 'de'];
const jsonFile = {
"translation": {
"welcome": "Welcome",
"hello": "Hello",
}
};
// IMPORTANT: If you use '../../' it will try to save to a location before the root folder
// Translation to multiple folders
translateToMultipleFolders(key, endpoint, location, fromLang, toLangs, jsonFile, 'myFolder');
// This function will return a folder called myFolder
// Translation to a single folder
translateToUnicFolder(key, endpoint, location, fromLang, toLangs, jsonFile, 'myFolder/OtherFolder/etc');
// This function will return a folder called ./myFolder/OtherFolder/etc
// Translation to a single folder
translateToUnicFolder(key, endpoint, location, fromLang, toLangs, jsonFile, './myFolder/OtherFolder/etc');
// This function will return a folder called ./myFolder/OtherFolder/etc
```
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.

@@ -110,0 +148,0 @@

@@ -44,3 +44,6 @@ interface TranslationType {

*
@description This function will return a folder called folder multiFolderGeneratedTranslations
@param [folderNamePath='multiFolderGeneratedTranslations'] If it is undefined, it will be associated by default: multiFolderGeneratedTranslations
You can use this like: 'myfoldername' or 'myfoldername/otherfolder' or './myfoldername/etcfolder'
@IMPORTANT Saving always starts from the project root folder.
@description This function will return a folder called folder multiFolderGeneratedTranslations in root folder or YourChoice
*/

@@ -54,3 +57,4 @@ export default function translateToMultipleFolders(

jsonFile: TranslationType,
folderNamePath?: string,
): void;
export {};

@@ -44,3 +44,6 @@ interface TranslationType {

*
@description This function will return a folder called folder unicFolderGeneratedTranslations
@param [folderNamePath='unicFolderGeneratedTranslations'] If it is undefined, it will be associated by default: unicFolderGeneratedTranslations
You can use this like: 'myfoldername' or 'myfoldername/otherfolder' or './myfoldername/etcfolder'
@IMPORTANT Saving always starts from the project root folder.
@description This function will return a folder called folder unicFolderGeneratedTranslations in root folder or YourChoice
*/

@@ -54,3 +57,4 @@ export default function translateToUnicFolder(

jsonFile: TranslationType,
folderName?: string,
): void;
export {};