From 5853575a8e5d5efa2d3d711f2584d3e31ce92aaf Mon Sep 17 00:00:00 2001 From: Petrisor Lacatus Date: Sun, 7 Apr 2024 22:17:29 +0300 Subject: [PATCH] DeepL support context parameter, fix preserving newlines (#110) * fix(deepl): preserve newlines in the token values during translations * feat(deepl): optionally provide a context when translating using deepl --- src/index.ts | 7 +++++++ src/services/deepl.ts | 12 +++++++++++- src/services/index.ts | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index f3eb90d..09ed15e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -63,6 +63,10 @@ commander `specify the name of your app to distinguish DeepL glossaries (if sharing an API key between multiple projects)`, 'json-autotranslate', ) + .option( + '--context ', + `set the context that is used by DeepL for translations`, + ) .option('--list-services', `outputs a list of available services`) .option( '-m, --matcher ', @@ -107,6 +111,7 @@ const translate = async ( config?: string, glossariesDir?: string, appName?: string, + context?: string, ) => { const workingDir = path.resolve(process.cwd(), inputDir); const resolvedCacheDir = path.resolve(process.cwd(), cacheDir); @@ -172,6 +177,7 @@ const translate = async ( decodeEscapes, glossariesDir, appName, + context, ); console.log(chalk`└── {green.bold Done}`); console.log(); @@ -419,6 +425,7 @@ translate( commander.config, commander.glossaries, commander.appName, + commander.context, ).catch((e: Error) => { console.log(); console.log(chalk.bgRed('An error has occurred:')); diff --git a/src/services/deepl.ts b/src/services/deepl.ts index 8475df4..147cb9f 100644 --- a/src/services/deepl.ts +++ b/src/services/deepl.ts @@ -15,6 +15,7 @@ export class DeepL implements TranslationService { private apiEndpoint: string; private glossariesDir: string; private appName: string; + private context: string; private apiKey: string; /** * Number to tokens to translate at once @@ -46,6 +47,7 @@ export class DeepL implements TranslationService { decodeEscapes?: boolean, glossariesDir?: string, appName?: string, + context?: string, ) { if (!config) { throw new Error(`Please provide an API key for ${this.name}.`); @@ -62,6 +64,7 @@ export class DeepL implements TranslationService { this.decodeEscapes = decodeEscapes; this.glossariesDir = glossariesDir; this.appName = appName; + this.context = context; } async fetchLanguages() { @@ -274,10 +277,12 @@ export class DeepL implements TranslationService { text: cleaned.map((c) => c.clean), source_lang: from.toUpperCase(), target_lang: to.toUpperCase(), - // see https://www.deepl.com/docs-api/html/disabling + // see https://developers.deepl.com/docs/xml-and-html-handling/html // set in order to indicate to DeepL that the interpolated strings that the matcher // replaced with `${index} should not be translated tag_handling: 'html', + // set to 1, because all newlines in the source text should be preserved + split_sentences: '1', }; // Should a glossary be used? @@ -293,6 +298,11 @@ export class DeepL implements TranslationService { body['formality'] = this.formality; } + if (this.context) { + // context is only added if it has been provided by as a command line argument + body['context'] = this.context; + } + // send request as a POST request, with all the tokens as separate texts in the body const response = await fetch(`${this.apiEndpoint}/translate`, { body: JSON.stringify(body), diff --git a/src/services/index.ts b/src/services/index.ts index f0e1c4b..9a135bf 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -24,6 +24,7 @@ export interface TranslationService { decodeEscapes?: boolean, glossariesDir?: string, appName?: string, + context?: string, ) => Promise; supportsLanguage: (language: string) => boolean; translateStrings: (