Skip to content

Commit

Permalink
optional trim
Browse files Browse the repository at this point in the history
  • Loading branch information
while1618 committed Dec 19, 2024
1 parent 860b009 commit 4078ca3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ $ npx i18n-auto-translation -k SUBSCRIPTION_KEY -d PROJECT_DIR -t DESIRED_LANGUA
| --certificatePath | -c | Path to a custom certificate. | / |
| --spaces | -s | Number of spaces to use when generating output JSON files. | 2 |
| --maxLinesPerRequest | -l | Maximum number of lines per request. For every `x` number of lines, separated request is sent to the api. | 50 |
| --context | -x | Context for the translation. Used only by the DeepL API. | default |
| --formality | -m | Formality for the translation. Used only by the DeepL API. | / |
| --context | -x | Context for the translation. Used only by the DeepL API. | / |
| --formality | -m | Formality for the translation. Used only by the DeepL API. | default |
| --trim | -i | Trim string after translation. | true |

## Demo

Expand Down
9 changes: 8 additions & 1 deletion src/translate/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ interface Arguments {
spaces: number;
maxLinesPerRequest: number;
context?: string;
formality?: string;
formality: string;
trim: boolean;
}

export const argv: Arguments = yargs(process.argv.slice(2))
Expand Down Expand Up @@ -107,5 +108,11 @@ export const argv: Arguments = yargs(process.argv.slice(2))
choices: ['default', 'more', 'less', 'prefer_more', 'prefer_less'],
default: 'default',
},
trim: {
type: 'boolean',
alias: 'i',
description: 'Trim string after translation.',
default: true,
},
})
.parseSync();
6 changes: 3 additions & 3 deletions src/translate/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export abstract class Translate {

private skipWords(value: string): string {
return value.replace(Translate.skipWordRegex, (match: string) => {
this.skippedWords.push(match.trim());
this.skippedWords.push(match);
return `{{${this.skippedWords.length - 1}}}`;
});
}
Expand Down Expand Up @@ -190,7 +190,7 @@ export abstract class Translate {

private saveTranslation = (translations: string, objectBeforeTranslation: JSONObj): void => {
let objectAfterTranslation: JSONObj = this.createTranslatedObject(
translations.split(Translate.sentenceDelimiter.trim()),
translations.split(Translate.sentenceDelimiter),
objectBeforeTranslation,
);
if (fs.existsSync(this.saveTo) && !argv.override) {
Expand All @@ -217,7 +217,7 @@ export abstract class Translate {
if (typeof json[key] === 'object') {
addTranslations(json[key] as JSONObj);
} else {
json[key] = translations[index++]?.trim();
json[key] = argv.trim ? translations[index++]?.trim() : translations[index++];
}
});
})(translatedObject);
Expand Down

0 comments on commit 4078ca3

Please sign in to comment.