Skip to content

Commit

Permalink
feat: new gpt versions adde
Browse files Browse the repository at this point in the history
  • Loading branch information
ParvinEyvazov committed Jul 31, 2024
1 parent 4853271 commit 5115a38
Show file tree
Hide file tree
Showing 5 changed files with 1,077 additions and 124 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "3.0.2",
"version": "3.1.0",
"license": "MIT",
"main": "dist/index.js",
"description": "Translate your JSON file or object into another languages with Google Translate API",
Expand Down
50 changes: 35 additions & 15 deletions src/core/translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,23 @@ export async function plaintranslate(

clonedSkipModuleKeys.push(clonedTranslationConfig.moduleKey);

const { newModuleKey, newFrom, newTo } = newTranslationModule(
const {
newModuleKey,
newFrom,
newTo,
skippedModuleKeys,
} = newTranslationModule(
clonedTranslationConfig.moduleKey,
clonedSkipModuleKeys,
from,
to
);

// add skippedModuleKeys to general skipModuleKeys
skippedModuleKeys.forEach(skippedModuleKey => {
clonedSkipModuleKeys.push(skippedModuleKey);
});

let stop: boolean =
!clonedTranslationConfig.fallback || newModuleKey === undefined;

Expand Down Expand Up @@ -101,31 +111,41 @@ function newTranslationModule(
newModuleKey: undefined,
newFrom: undefined,
newTo: undefined,
skippedModuleKeys: [],
};
const skippedModuleKeys: string[] = [];

const allModuleKeys: string[] = translationModuleKeys();

const result: string[] = allModuleKeys.filter(
item => !skipModuleKeys.includes(item)
);

let newModuleKey = result[0];

if (!newModuleKey) {
return default_data; // default
if (result.length == 0) {
return default_data;
}

let newFrom = getLanguageVariant(sourceModuleKeys, from, newModuleKey);
let newTo = getLanguageVariant(sourceModuleKeys, to, newModuleKey);
for (let i = 0; i < result.length; i++) {
let newModuleKey = result[i];

let newFrom = getLanguageVariant(sourceModuleKeys, from, newModuleKey);
let newTo = getLanguageVariant(sourceModuleKeys, to, newModuleKey);

if (!newFrom || !newTo) {
return default_data; // default
// if found valid newFrom & newTo, return
if (newFrom && newTo) {
return {
newModuleKey,
newFrom,
newTo,
skippedModuleKeys,
};
}
// otherwise skip to next module, add key to skippedModuleKeys
else {
skippedModuleKeys.push(newModuleKey);
}
}

// has valid newModuleKey & from & to
return {
newModuleKey,
newFrom,
newTo,
};
default_data.skippedModuleKeys = skippedModuleKeys as any;
return default_data;
}
20 changes: 20 additions & 0 deletions src/modules/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ export async function translateWithGoogle2(
return response.text;
}

export async function translateWithGPT35Turbo(
str: string,
from: string,
to: string
) {
return translateWithGPT('gpt-3.5-turbo', str, from, to);
}

export async function translateWithGPT4(str: string, from: string, to: string) {
return translateWithGPT('gpt-4', str, from, to);
}

export async function translateWithGPT4o(
str: string,
from: string,
Expand All @@ -186,6 +198,14 @@ export async function translateWithGPT4o(
return translateWithGPT('gpt-4o', str, from, to);
}

export async function translateWithGPT4oMini(
str: string,
from: string,
to: string
) {
return translateWithGPT('gpt-4o-mini', str, from, to);
}

export async function translateWithGPT(
model: string,
str: string,
Expand Down
Loading

0 comments on commit 5115a38

Please sign in to comment.