Skip to content

Commit

Permalink
DeepL support context parameter, fix preserving newlines (#110)
Browse files Browse the repository at this point in the history
* fix(deepl): preserve newlines in the token values during translations

* feat(deepl): optionally provide a context when translating using deepl
  • Loading branch information
stefan-lacatus authored Apr 7, 2024
1 parent 3b0da28 commit 5853575
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <context>',
`set the context that is used by DeepL for translations`,
)
.option('--list-services', `outputs a list of available services`)
.option(
'-m, --matcher <matcher>',
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -172,6 +177,7 @@ const translate = async (
decodeEscapes,
glossariesDir,
appName,
context,
);
console.log(chalk`└── {green.bold Done}`);
console.log();
Expand Down Expand Up @@ -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:'));
Expand Down
12 changes: 11 additions & 1 deletion src/services/deepl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}.`);
Expand All @@ -62,6 +64,7 @@ export class DeepL implements TranslationService {
this.decodeEscapes = decodeEscapes;
this.glossariesDir = glossariesDir;
this.appName = appName;
this.context = context;
}

async fetchLanguages() {
Expand Down Expand Up @@ -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 `<span translate="no">${index}</span> 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?
Expand All @@ -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),
Expand Down
1 change: 1 addition & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface TranslationService {
decodeEscapes?: boolean,
glossariesDir?: string,
appName?: string,
context?: string,
) => Promise<void>;
supportsLanguage: (language: string) => boolean;
translateStrings: (
Expand Down

0 comments on commit 5853575

Please sign in to comment.