Skip to content

Commit

Permalink
bump: 1.1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Oct 4, 2024
1 parent fed83dc commit 92ad5a6
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 86 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module.exports = {
".eslintrc.js",
"jest.config.js",
"dist",
"/types/"
"/types/",
"geraTraducoes.js"
],
parser: '@typescript-eslint/parser',
parserOptions: {
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: 16
node-version: 20
- run: npm ci
- run: npm test
env:
Expand All @@ -38,7 +38,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: 16
node-version: 20
registry-url: https://registry.npmjs.org/
- run: npm ci
- run: npm run build
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "azure-translator-code",
"version": "1.1.6",
"version": "1.1.7",
"description": "Azure Cognitive Services Translator Text API Code for Use with Common Languages",
"author": {
"name": "Gabriel Logan"
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import translateToUnicFolder from "./translateToUnicFolder";
import updateTranslationsMulti from "./updateTranslationMulti";
import updateTranslationsUnic from "./updateTranslationUnic";

export type { TranslationType, JSONValue, JSONArray } from "./types";
export {
translateToMultipleFolders,
translateToUnicFolder,
Expand Down
240 changes: 158 additions & 82 deletions src/translate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ interface TranslationResponse {

type TranslateResponseData = TranslationResponse[];

/**
*
* @param text
* @param from
* @param to
* @param endpoint
* @param key
* @param location
* @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: [ { text: 'Hola', to: 'es' }, { text: 'Bonjour', to: 'fr' } ]
*/
export async function translateText(
text: string,
from: string,
Expand Down Expand Up @@ -48,30 +65,6 @@ export async function translateText(
return response;
}

function isObjectNotNull(value: JSONValue): value is Record<string, JSONValue> {
return typeof value === "object" && value !== null;
}

function isString(value: JSONValue): value is string {
return typeof value === "string";
}

function isArray(value: JSONValue): value is JSONValue[] {
return Array.isArray(value);
}

function isNotNeedToTranslate(value: JSONValue): boolean {
const types = ["number", "boolean"];

return types.includes(typeof value);
}

function isInvalidJSONValue(value: JSONValue): value is never {
const invalidTypes = ["function", "symbol", "undefined"];

return invalidTypes.includes(typeof value);
}

/**
* Translates a JSON object from one language to another using the Azure Translator service.
* @param key - The Azure Translator subscription key.
Expand All @@ -81,6 +74,15 @@ function isInvalidJSONValue(value: JSONValue): value is never {
* @param toLang - The languages code of the target languages.
* @param jsonFile - The JSON object to be translated.
* @returns A Promise that resolves to the translated JSON object.
*
* @example
* const json = {
hello: "Hello",
};
*
* translate("key", "https://api.cognitive.microsofttranslator.com", "location", "en", ["pt", "es"], json);
*
* Returns: { hello: [ 'Olá', 'Hola' ] }
*/
export default async function translate(
key: string,
Expand All @@ -91,78 +93,152 @@ export default async function translate(
jsonFile: TranslationType,
) {
const translatedJson: TranslationType = {};

const isArrayBigerThanOne = toLangs.length > 1;

for (const [jsonKey, jsonValue] of Object.entries(jsonFile)) {
if (isInvalidJSONValue(jsonValue)) {
throw new Error("Invalid JSON value");
}
translatedJson[jsonKey] = await translateValue(
jsonValue,
key,
endpoint,
location,
fromLang,
toLangs,
isArrayBigerThanOne,
);
}

if (isNotNeedToTranslate(jsonValue)) {
translatedJson[jsonKey] = jsonValue;
} else if (isString(jsonValue)) {
const response = await translateText(
jsonValue,
fromLang,
toLangs,
endpoint,
key,
location,
);
return translatedJson;
}

const translations = response.data[0].translations;

const translatedValues = translations.map((translation) => {
return translation.text;
});

translatedJson[jsonKey] = isArrayBigerThanOne
? translatedValues
: translatedValues[0];
} else if (isArray(jsonValue)) {
const translatedArray: (TranslationType | TranslationType[])[] = [];
for (const item of jsonValue) {
if (typeof item === "string") {
const response = await translateText(
item,
fromLang,
toLangs,
endpoint,
key,
location,
);

const translations = response.data[0].translations;

const translatedValues = translations.map((translation) => {
return translation.text;
});

translatedArray.push(
isArrayBigerThanOne ? translatedValues : translatedValues[0],
);
} else {
translatedArray.push(item as TranslationType);
}
}
async function translateValue(
value: JSONValue,
key: string,
endpoint: string,
location: string,
fromLang: string,
toLangs: string[],
isArrayBigerThanOne: boolean,
): Promise<JSONValue> {
if (isInvalidJSONValue(value)) {
throw new Error("Invalid JSON value");
}

translatedJson[jsonKey] = translatedArray;
} else if (isObjectNotNull(jsonValue)) {
const response = await translate(
if (isNotNeedToTranslate(value)) {
return value;
} else if (isString(value)) {
return await translateString(
value,
key,
endpoint,
location,
fromLang,
toLangs,
isArrayBigerThanOne,
);
} else if (isArray(value)) {
return await translateArray(
value,
key,
endpoint,
location,
fromLang,
toLangs,
isArrayBigerThanOne,
);
} else if (isObjectNotNull(value)) {
return await translate(
key,
endpoint,
location,
fromLang,
toLangs,
value as TranslationType,
);
} else {
return null;
}
}

async function translateString(
value: string,
key: string,
endpoint: string,
location: string,
fromLang: string,
toLangs: string[],
isArrayBigerThanOne: boolean,
): Promise<JSONValue> {
const response = await translateText(
value,
fromLang,
toLangs,
endpoint,
key,
location,
);

const translations = response.data[0].translations;

const translatedValues = translations.map((translation) => {
return translation.text;
});

return isArrayBigerThanOne ? translatedValues : translatedValues[0];
}

async function translateArray(
array: JSONValue[],
key: string,
endpoint: string,
location: string,
fromLang: string,
toLangs: string[],
isArrayBigerThanOne: boolean,
): Promise<JSONValue[]> {
const translatedArray: (TranslationType | TranslationType[])[] = [];
for (const item of array) {
if (typeof item === "string") {
const translatedItem = await translateString(
item,
key,
endpoint,
location,
fromLang,
toLangs,
jsonValue as TranslationType,
isArrayBigerThanOne,
);

translatedJson[jsonKey] = response;
if (translatedItem !== null) {
translatedArray.push(
translatedItem as TranslationType | TranslationType[],
);
}
} else {
translatedJson[jsonKey] = null;
translatedArray.push(item as TranslationType);
}
}
return translatedArray;
}

return translatedJson;
function isObjectNotNull(value: JSONValue): value is Record<string, JSONValue> {
return typeof value === "object" && value !== null;
}

function isString(value: JSONValue): value is string {
return typeof value === "string";
}

function isArray(value: JSONValue): value is JSONValue[] {
return Array.isArray(value);
}

function isNotNeedToTranslate(value: JSONValue): boolean {
const types = ["number", "boolean"];

return types.includes(typeof value);
}

function isInvalidJSONValue(value: JSONValue): value is never {
const invalidTypes = ["function", "symbol", "undefined"];

return invalidTypes.includes(typeof value);
}

0 comments on commit 92ad5a6

Please sign in to comment.