Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenAI support, Overwrite option #128

Merged
merged 8 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ As of this release, json-autotranslate offers five services:
to translate strings)
- **amazon-translate** (uses
[Amazon Translate](https://aws.amazon.com/translate/) to translate strings)
- **OpenAI** (uses gpt-4o and can take a context file path from the context option)
- **manual** (allows you to translate strings manually by entering them into the
CLI)
- **dry-run** (outputs a list of strings that will be translated without
Expand Down Expand Up @@ -307,7 +308,7 @@ Options:
-s, --service <service> selects the service to be used for translation (default: "google-translate")
-g, --glossaries [glossariesDir] set the glossaries folder to be used by DeepL. Keep empty for automatic determination of matching glossary
-a, --appName <appName> specify the name of your app to distinguish DeepL glossaries (if sharing an API key between multiple projects) (default: "json-autotranslate")
--context <context> set the context that is used by DeepL for translations
--context <context> set the context that is used by DeepL for translations, for OpenAI it is the path to the json file containing the context for each key
--list-services outputs a list of available services
-m, --matcher <matcher> selects the matcher to be used for interpolations (default: "icu")
--list-matchers outputs a list of available matchers
Expand All @@ -316,6 +317,7 @@ Options:
-d, --delete-unused-strings deletes strings in translation files that don't exist in the template
--directory-structure <default|ngx-translate> the locale directory structure
--decode-escapes decodes escaped HTML entities like &#39; into normal UTF-8 characters
-o, --overwrite overwrite already present translations
-h, --help display help for command
```

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ commander
)
.option(
'--context <context>',
`set the context that is used by DeepL for translations`,
`set the context that is used by DeepL for translations, for OpenAI it's the path to a JSON file`,
)
.option('--list-services', `outputs a list of available services`)
.option(
Expand Down Expand Up @@ -94,6 +94,10 @@ commander
'--decode-escapes',
'decodes escaped HTML entities like &#39; into normal UTF-8 characters',
)
.option(
'-o, --overwrite',
'overwrite existing translations instead of skipping them',
)
.parse(process.argv);

const translate = async (
Expand All @@ -112,6 +116,7 @@ const translate = async (
glossariesDir?: string | boolean,
appName?: string,
context?: string,
overwrite: boolean = false,
) => {
const workingDir = path.resolve(process.cwd(), inputDir);
const resolvedCacheDir = path.resolve(process.cwd(), cacheDir);
Expand Down Expand Up @@ -297,6 +302,7 @@ const translate = async (
dirStructure,
deleteUnusedStrings,
withArrays,
overwrite,
);

switch (dirStructure) {
Expand Down Expand Up @@ -426,6 +432,7 @@ translate(
commander.glossaries,
commander.appName,
commander.context,
commander.overwrite,
).catch((e: Error) => {
console.log();
console.log(chalk.bgRed('An error has occurred:'));
Expand All @@ -445,6 +452,7 @@ function createTranslator(
dirStructure: DirectoryStructure,
deleteUnusedStrings: boolean,
withArrays: boolean,
overwrite,
) {
return async (
sourceFile: TranslatableFile,
Expand Down Expand Up @@ -472,7 +480,7 @@ function createTranslator(
: [];
const templateStrings = Object.keys(sourceFile.content);
const stringsToTranslate = templateStrings
.filter((key) => !existingKeys.includes(key) || cacheDiff.includes(key))
.filter((key) => overwrite || !existingKeys.includes(key) || cacheDiff.includes(key))
.map((key) => ({
key,
value: sourceFile.type === 'key-based' ? sourceFile.content[key] : key,
Expand Down
2 changes: 2 additions & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AzureTranslator } from './azure-translator';
import { ManualTranslation } from './manual';
import { Matcher } from '../matchers';
import { AmazonTranslate } from './amazon-translate';
import { OpenAITranslator } from './openai';

export interface TranslationResult {
key: string;
Expand Down Expand Up @@ -44,6 +45,7 @@ export const serviceMap: {
azure: new AzureTranslator(),
manual: new ManualTranslation(),
'amazon-translate': new AmazonTranslate(),
'openai': new OpenAITranslator(),
};

export interface DeepLGlossary {
Expand Down
Loading