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

optional trim #56

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading