From 0af47d77f456c3429bb40caa2b694a5ce97f92b6 Mon Sep 17 00:00:00 2001 From: gabriel-logan Date: Fri, 5 Apr 2024 01:48:46 -0300 Subject: [PATCH] chore: update gitignore and eslintignore --- .eslintignore | 1 + types/addInto/index.d.ts | 125 ------------------ types/index.d.ts | 4 - types/translateToMultipleFolders/index.d.ts | 50 -------- types/translateToUnicFolder/index.d.ts | 50 -------- types/updateTranslationMulti/index.d.ts | 53 -------- types/updateTranslationUnic/index.d.ts | 133 -------------------- 7 files changed, 1 insertion(+), 415 deletions(-) delete mode 100644 types/addInto/index.d.ts delete mode 100644 types/index.d.ts delete mode 100644 types/translateToMultipleFolders/index.d.ts delete mode 100644 types/translateToUnicFolder/index.d.ts delete mode 100644 types/updateTranslationMulti/index.d.ts delete mode 100644 types/updateTranslationUnic/index.d.ts diff --git a/.eslintignore b/.eslintignore index 80d2367..c6c2e6a 100644 --- a/.eslintignore +++ b/.eslintignore @@ -3,3 +3,4 @@ dist/ tests/ coverage/ types/ +jest.config.js diff --git a/types/addInto/index.d.ts b/types/addInto/index.d.ts deleted file mode 100644 index b76f0f4..0000000 --- a/types/addInto/index.d.ts +++ /dev/null @@ -1,125 +0,0 @@ -/** - * import axios from 'axios'; -import { v4 as uuidv4 } from 'uuid'; -import fs from 'fs'; -import path from 'path'; - -interface TranslationType { - translation: Record; -} - -export default function addInto( - key: string, - endpoint: string, - location: string, - fromLang: string, - toLangs: string[], - jsonFile: TranslationType, - folderName: string = 'unicFolderGeneratedTranslations', -) { - const traducoesDir: string = path.join(__dirname, '..', '..', folderName); - - if (!fs.existsSync(traducoesDir)) { - fs.mkdirSync(traducoesDir, { recursive: true }); - } - - const { translation } = jsonFile; - - function translateText(text: string, from: string, to: string) { - return axios({ - baseURL: endpoint, - url: '/translate', - method: 'post', - headers: { - 'Ocp-Apim-Subscription-Key': key, - 'Ocp-Apim-Subscription-Region': location, - 'Content-type': 'application/json', - 'X-ClientTraceId': uuidv4().toString(), - }, - params: { - 'api-version': '3.0', - from: from, - to: to, - }, - data: [ - { - text: text, - }, - ], - responseType: 'json', - }); - } - - async function translateAndSave(lang: string, existingTranslations: Record = {}) { - const translations: Record = {}; - - for (const key in translation) { - try { - console.log('TO FAZENDO UMA REQUISICAO'); - const response = await translateText(translation[key], fromLang, lang); - const translatedText: string = response.data[0].translations[0].text; - translations[key] = translatedText; - console.log(`Translating ${translation[key]} to ${lang} \n\n`); - } catch (error) { - if (error instanceof Error) { - console.error(`Error translating "${key}" to ${lang}: ${error.message} \n`); - } else { - console.error(`An error occurred within the error (: \n`); - } - } - } - - const mergedTranslations = { ...existingTranslations, ...translations }; - return mergedTranslations; - } - - async function translateAndSaveAll() { - let existingTranslations: Record = {}; - - // Load existing translations if the files exist - for (const lang of toLangs) { - const filePath = path.join(traducoesDir, `${lang}.json`); - if (fs.existsSync(filePath)) { - const fileContent = fs.readFileSync(filePath, 'utf-8'); - const jsonData = JSON.parse(fileContent); - existingTranslations = { ...existingTranslations, ...jsonData.translation }; - } - } - - const translationPromises = toLangs.map((lang) => translateAndSave(lang, existingTranslations)); - - const mergedTranslations = await Promise.all(translationPromises); - - for (const [index, lang] of toLangs.entries()) { - const outputFileName = path.join(traducoesDir, `${lang}.json`); - fs.writeFileSync( - outputFileName, - JSON.stringify({ translation: mergedTranslations[index] }, null, 4), - ); - console.log(`Translations for ${lang} saved in ${outputFileName} \n\n`); - } - } - - translateAndSaveAll().catch((error) => { - console.error(`Error translating and saving texts: ${error.message} \n`); - }); -} - -const jsonFile = { - translation: { - AAAaddwd: 'AWdD', - AAAdaasdwd: '123456', - AAasdAawd: 'AWasdD', - AAasdAaawd: 'AasdWD', - AAAasasadwd: 'AasdWD', - }, -}; -const key = '50ee9953ce4b4c0cab5e00f08518fe9f'; // THIS CODE NOT WORK, REPLACE YOURS HERE -const endpoint = 'https://api.cognitive.microsofttranslator.com/'; -const location = 'eastus2'; -const fromLang = 'en'; -const toLangs = ['pt', 'es', 'de']; - -addInto(key, endpoint, location, fromLang, toLangs, jsonFile); - - */ diff --git a/types/index.d.ts b/types/index.d.ts deleted file mode 100644 index d54d9a2..0000000 --- a/types/index.d.ts +++ /dev/null @@ -1,4 +0,0 @@ -import translateToMultipleFolders from './translateToMultipleFolders'; -import translateToUnicFolder from './translateToUnicFolder'; -import updateTranslationsMulti from './updateTranslationMulti'; -export { translateToMultipleFolders, translateToUnicFolder, updateTranslationsMulti }; diff --git a/types/translateToMultipleFolders/index.d.ts b/types/translateToMultipleFolders/index.d.ts deleted file mode 100644 index cd64596..0000000 --- a/types/translateToMultipleFolders/index.d.ts +++ /dev/null @@ -1,50 +0,0 @@ -interface TranslationType { - translation: Record; -} -/** - * @param key Your key from azure translator, something like: 'sds12312a213aaaa9b2d0c37eds37b' - * @param endpoint The endpoint: 'https://api.cognitive.microsofttranslator.com/' - * @param location Ex. 'eastus' - * @param fromLang Ex. 'en' - * @param toLangs Ex. [ - 'pt', - 'de', - 'es', - 'fr', - 'it', - 'ja', - 'ko', - 'nl', - 'ru', - 'zh', - 'pt-pt', - 'ar', - 'tlh-Latn' - ]; - * @param jsonFile - * It must follow the following structure: - * - * { - "translation": { - "welcome": "Welcome", - "hello": "Hello", - "good_morning": "Good morning", - "good_afternoon": "Good afternoon", - "good_evening": "Good evening", - "thank_you": "Thank you", - "please": "Please", - "yes": "Yes", - "no": "No", - "error_message": "An error occurred" - } - } - * - If you need, copy this structure to get better then make your modification - * - @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. - @return {void} This function will return a folder called folder multiFolderGeneratedTranslations in root folder or YourChoice - */ -export default function translateToMultipleFolders(key: string, endpoint: string, location: string, fromLang: string, toLangs: string[], jsonFile: TranslationType, folderNamePath?: string): void; -export {}; diff --git a/types/translateToUnicFolder/index.d.ts b/types/translateToUnicFolder/index.d.ts deleted file mode 100644 index 78ee30b..0000000 --- a/types/translateToUnicFolder/index.d.ts +++ /dev/null @@ -1,50 +0,0 @@ -interface TranslationType { - translation: Record; -} -/** - * @param key Your key from azure translator, something like: 'sds12312a213aaaa9b2d0c37eds37b' - * @param endpoint The endpoint: 'https://api.cognitive.microsofttranslator.com/' - * @param location Ex. 'eastus' - * @param fromLang Ex. 'en' - * @param toLangs Ex. [ - 'pt', - 'de', - 'es', - 'fr', - 'it', - 'ja', - 'ko', - 'nl', - 'ru', - 'zh', - 'pt-pt', - 'ar', - 'tlh-Latn' - ]; - * @param jsonFile - * It must follow the following structure: - * - * { - "translation": { - "welcome": "Welcome", - "hello": "Hello", - "good_morning": "Good morning", - "good_afternoon": "Good afternoon", - "good_evening": "Good evening", - "thank_you": "Thank you", - "please": "Please", - "yes": "Yes", - "no": "No", - "error_message": "An error occurred" - } - } - * - If you need, copy this structure to get better then make your modification - * - @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. - @return {void} This function will return a folder called folder unicFolderGeneratedTranslations in root folder or YourChoice - */ -export default function translateToUnicFolder(key: string, endpoint: string, location: string, fromLang: string, toLangs: string[], jsonFile: TranslationType, folderName?: string): void; -export {}; diff --git a/types/updateTranslationMulti/index.d.ts b/types/updateTranslationMulti/index.d.ts deleted file mode 100644 index 49f8d00..0000000 --- a/types/updateTranslationMulti/index.d.ts +++ /dev/null @@ -1,53 +0,0 @@ -interface TranslationType { - translation: Record; -} -/** - * @param key Your key from azure translator, something like: 'sds12312a213aaaa9b2d0c37eds37b' - * @param endpoint The endpoint: 'https://api.cognitive.microsofttranslator.com/' - * @param location Ex. 'eastus' - * @param fromLang Ex. 'en' - * @param toLangs Ex. [ - 'pt', - 'de', - 'es', - 'fr', - 'it', - 'ja', - 'ko', - 'nl', - 'ru', - 'zh', - 'pt-pt', - 'ar', - 'tlh-Latn' - ]; - * @param jsonFile - * It must follow the following structure: - * - * { - "translation": { - "welcome": "Welcome", - "hello": "Hello", - "good_morning": "Good morning", - "good_afternoon": "Good afternoon", - "good_evening": "Good evening", - "thank_you": "Thank you", - "please": "Please", - "yes": "Yes", - "no": "No", - "error_message": "An error occurred" - } - } - * - If you need, copy this structure to get better then make your modification - * - * @description This function checks the json with the already existing translations and adds only the non-existing translations to the file, this serves to save data. - *Otherwise it works the same as the other 2 functions - * - * @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. - @return {void} This function will return a folder called folder multiFolderGeneratedTranslations in root folder or YourChoice - */ -export default function updateTranslationsMulti(key: string, endpoint: string, location: string, fromLang: string, toLangs: string[], jsonFile: TranslationType, folderNamePath?: string): void; -export {}; diff --git a/types/updateTranslationUnic/index.d.ts b/types/updateTranslationUnic/index.d.ts deleted file mode 100644 index 4b15309..0000000 --- a/types/updateTranslationUnic/index.d.ts +++ /dev/null @@ -1,133 +0,0 @@ -/** - * import axios from 'axios'; -import { v4 as uuidv4 } from 'uuid'; -import fs from 'fs'; -import path from 'path'; - -interface TranslationType { - translation: Record; -} - -export default function updateTranslations( - key: string, - endpoint: string, - location: string, - fromLang: string, - toLangs: string[], - jsonFile: TranslationType, - folderName: string = 'unicFolderGeneratedTranslations', -) { - const traducoesDir: string = path.join(__dirname, '..', '..', folderName); - - if (!fs.existsSync(traducoesDir)) { - fs.mkdirSync(traducoesDir, { recursive: true }); - } - - const { translation } = jsonFile; - - function translateText(text: string, from: string, to: string) { - return axios({ - baseURL: endpoint, - url: '/translate', - method: 'post', - headers: { - 'Ocp-Apim-Subscription-Key': key, - 'Ocp-Apim-Subscription-Region': location, - 'Content-type': 'application/json', - 'X-ClientTraceId': uuidv4().toString(), - }, - params: { - 'api-version': '3.0', - from: from, - to: to, - }, - data: [ - { - text: text, - }, - ], - responseType: 'json', - }); - } - - async function translateAndSave(lang: string, existingTranslations: Record = {}) { - const translations: Record = {}; - - for (const key in translation) { - // Verifique se a tradução já existe no idioma de destino - if (!existingTranslations[key]) { - try { - console.log('TO FAZENDO UMA REQUISICAO'); - const response = await translateText(translation[key], fromLang, lang); - const translatedText: string = response.data[0].translations[0].text; - translations[key] = translatedText; - console.log(`Translating ${translation[key]} to ${lang} \n\n`); - } catch (error) { - if (error instanceof Error) { - console.error(`Error translating "${key}" to ${lang}: ${error.message} \n`); - } else { - console.error(`An error occurred within the error (: \n`); - } - } - } else { - // A tradução já existe, use a tradução existente - translations[key] = existingTranslations[key]; - } - } - - const mergedTranslations = { ...existingTranslations, ...translations }; - return mergedTranslations; - } - - async function translateAndSaveAll() { - let existingTranslations: Record = {}; - - // Load existing translations if the files exist - for (const lang of toLangs) { - const filePath = path.join(traducoesDir, `${lang}.json`); - if (fs.existsSync(filePath)) { - const fileContent = fs.readFileSync(filePath, 'utf-8'); - const jsonData = JSON.parse(fileContent); - existingTranslations = { ...existingTranslations, ...jsonData.translation }; - } - } - - const translationPromises = toLangs.map((lang) => translateAndSave(lang, existingTranslations)); - - const mergedTranslations = await Promise.all(translationPromises); - - for (const [index, lang] of toLangs.entries()) { - const outputFileName = path.join(traducoesDir, `${lang}.json`); - fs.writeFileSync( - outputFileName, - JSON.stringify({ translation: mergedTranslations[index] }, null, 4), - ); - console.log(`Translations for ${lang} saved in ${outputFileName} \n\n`); - } - } - - translateAndSaveAll().catch((error) => { - console.error(`Error translating and saving texts: ${error.message} \n`); - }); -} - -const jsonFile = { - translation: { - AAAaddwd: 'AWdD', - AAAdaad: 'AWjkldD', - AAasdAawd: 'Hi friend', - AAasdasd: 'AasdWD', - AAAasasadwd: 'AasdWD', - 'EAI GAY': 'HELLO GAY', - haha: 'Hi bro', - }, -}; -const key = '50ee9953ce4b4c0cab5e00f08518fe9f'; // THIS CODE NOT WORK, REPLACE YOURS HERE -const endpoint = 'https://api.cognitive.microsofttranslator.com/'; -const location = 'eastus2'; -const fromLang = 'en'; -const toLangs = ['pt', 'es', 'de']; - -updateTranslations(key, endpoint, location, fromLang, toLangs, jsonFile); - - */