From 8cf65f5a187d02aa613be867ef2c62366298d642 Mon Sep 17 00:00:00 2001 From: Leo Bernard Date: Wed, 10 Jan 2024 15:07:53 +0100 Subject: [PATCH] :sparkles: Add --with-arrays flag to enable support for arrays in source files Fixes #102 --- README.md | 33 ++++++++++++++++++++------------- src/index.ts | 13 ++++++++++++- src/util/file-system.ts | 5 ++++- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 97632bc..18b0813 100644 --- a/README.md +++ b/README.md @@ -181,19 +181,25 @@ DeepL Free is limited to 500,000 characters translated per month. After you have completed your sign-up, you can pass the API key to json-autotranslate using the `-c` or `--config` option. -The value of the `--config` argument is a comma separated string with the following: `appKey,formality,batchSize`. - -The `formality` argument currently only works for target languages "DE" (German), "FR" (French), "IT" (Italian), -"ES" (Spanish), "NL" (Dutch), "PL" (Polish), "PT-PT", "PT-BR" (Portuguese) and "RU" (Russian). Possible options are: - -- "default" (default) -- "more" - for a more formal language -- "less" - for a more informal language - -To improve performance and prevent DeepL rate-limiting json-autotranslate batches multiple tokens into a single translation request. -By default, the `batchSize` is set to `1000`, meaning that `1000` tokens are translated at once. This can be controlled by adjusting the value in the `--config` parameter. -This value was chosen because the DeepL prevents the body of a request to be larger than `128 KiB (128 · 1024 bytes)``. Based on experimentation, even with long tokens, this limit is not reached. - +The value of the `--config` argument is a comma separated string with the +following: `appKey,formality,batchSize`. + +The `formality` argument currently only works for target languages "DE" +(German), "FR" (French), "IT" (Italian), "ES" (Spanish), "NL" (Dutch), "PL" +(Polish), "PT-PT", "PT-BR" (Portuguese) and "RU" (Russian). Possible options +are: + +- "default" (default) +- "more" - for a more formal language +- "less" - for a more informal language + +To improve performance and prevent DeepL rate-limiting json-autotranslate +batches multiple tokens into a single translation request. By default, the +`batchSize` is set to `1000`, meaning that `1000` tokens are translated at once. +This can be controlled by adjusting the value in the `--config` parameter. This +value was chosen because the DeepL prevents the body of a request to be larger +than `128 KiB (128 · 1024 bytes)``. Based on experimentation, even with long +tokens, this limit is not reached. Reference @@ -297,6 +303,7 @@ Options: -s, --service selects the service to be used for translation (default: "google-translate") --list-services outputs a list of available services -m, --matcher selects the matcher to be used for interpolations (default: "icu") + -a, --with-arrays enables support for arrays in files, but removes support for keys named 0, 1, 2, etc. --list-matchers outputs a list of available matchers -c, --config supply a config parameter (e.g. path to key file) to the translation service -f, --fix-inconsistencies automatically fixes inconsistent key-value pairs by setting the value to the key diff --git a/src/index.ts b/src/index.ts index fb82609..fca8237 100644 --- a/src/index.ts +++ b/src/index.ts @@ -45,6 +45,10 @@ commander /^(key-based|natural|auto)$/, 'auto', ) + .option( + '-a, --with-arrays', + `enables support for arrays in files, but removes support for keys named 0, 1, 2, etc.`, + ) .option( '-s, --service ', `selects the service to be used for translation`, @@ -85,6 +89,7 @@ const translate = async ( sourceLang: string = 'en', deleteUnusedStrings = false, fileType: FileType = 'auto', + withArrays: boolean = false, dirStructure: DirectoryStructure = 'default', fixInconsistencies = false, service: keyof typeof serviceMap = 'google-translate', @@ -122,7 +127,11 @@ const translate = async ( sourceLang, ); - const templateFiles = loadTranslations(templateFilePath, fileType); + const templateFiles = loadTranslations( + templateFilePath, + fileType, + withArrays, + ); if (templateFiles.length === 0) { throw new Error( @@ -275,6 +284,7 @@ const translate = async ( const existingFiles = loadTranslations( evaluateFilePath(workingDir, dirStructure, language), fileType, + withArrays, ); if (deleteUnusedStrings) { @@ -386,6 +396,7 @@ translate( commander.sourceLanguage, commander.deleteUnusedStrings, commander.type, + commander.withArrays, commander.directoryStructure, commander.fixInconsistencies, commander.service, diff --git a/src/util/file-system.ts b/src/util/file-system.ts index 3e65f09..daa78e4 100644 --- a/src/util/file-system.ts +++ b/src/util/file-system.ts @@ -44,6 +44,7 @@ export const detectFileType = (json: any): FileType => { export const loadTranslations = ( directory: string, fileType: FileType = 'auto', + withArrays = false, ) => fs .readdirSync(directory) @@ -58,7 +59,9 @@ export const loadTranslations = ( type, content: type === 'key-based' - ? flatten(require(path.resolve(directory, f)), { safe: true }) + ? flatten(require(path.resolve(directory, f)), { + safe: !withArrays, + }) : require(path.resolve(directory, f)), } as TranslatableFile; });