Skip to content

Commit

Permalink
Merge pull request #56 from while1618/skip_trim
Browse files Browse the repository at this point in the history
optional trim
  • Loading branch information
while1618 authored Dec 19, 2024
2 parents 860b009 + 3fb9e38 commit bde9e32
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 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();
3 changes: 1 addition & 2 deletions src/translate/providers/azure-rapid-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Translate } from '../translate';
import { addCustomCert } from '../util';

export class AzureRapidAPI extends Translate {
private static readonly endpoint: string = 'microsoft-translator-text.p.rapidapi.com';
private static readonly endpoint: string = 'microsoft-translator-text-api3.p.rapidapi.com';
private static readonly axiosConfig: AxiosRequestConfig = {
headers: {
'X-ClientTraceId': crypto.randomUUID(),
Expand All @@ -16,7 +16,6 @@ export class AzureRapidAPI extends Translate {
'Content-type': 'application/json',
},
params: {
'api-version': '3.0',
from: argv.from,
to: argv.to,
},
Expand Down
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 bde9e32

Please sign in to comment.