fix(ivy): i18n - update `localize-translate` to accept target-locales (#33381)

The `localize-translate` command line tool can now accept an array of
target locales to support the case where translation files do not
contain them. Specify this array via the `--target-locales` option.

NOTE to early adopters: in order to support this, the original `-t`
option for the binary has changed from being a glob pattern to an array
of paths, which will have matching indices to any provided target-locales.

PR Close #33381
This commit is contained in:
Pete Bacon Darwin 2019-10-25 10:55:23 +01:00 committed by Andrew Kushnir
parent 62b2840822
commit 41979d6a27
2 changed files with 13 additions and 6 deletions

View File

@ -12,7 +12,7 @@
"start": "ng serve",
"pretest": "ng version",
"test": "ng test && yarn e2e --configuration=ci && yarn e2e --configuration=ci-production && yarn translated:test && yarn translated:legacy:test",
"translate": "localize-translate -r \"dist/\" -s \"**/*\" -l \"en-US\" -t \"src/locales/messages.*\" -o \"../tmp/translations/{{LOCALE}}\"",
"translate": "localize-translate -r \"dist/\" -s \"**/*\" -l \"en-US\" -t \"src/locales/messages.de.json\" \"src/locales/messages.fr.json\" -o \"../tmp/translations/{{LOCALE}}\"",
"translated:test": "yarn build && yarn translate && yarn translated:fr:e2e && yarn translated:de:e2e && yarn translated:en:e2e",
"translated:fr:serve": "serve ../tmp/translations/fr --listen 4200",
"translated:fr:e2e": "npm-run-all -p -r translated:fr:serve \"ng e2e --configuration=translated-fr\"",

View File

@ -47,8 +47,17 @@ if (require.main === module) {
.option('t', {
alias: 'translations',
required: true,
array: true,
describe:
'A glob pattern indicating what translation files to load, either absolute or relative to the current working directory. E.g. `my_proj/src/locale/messages.*.xlf.',
'A list of paths to the translation files to load, either absolute or relative to the current working directory.\n' +
'E.g. "-t src/locale/messages.en.xlf src/locale/messages.fr.xlf src/locale/messages.de.xlf".',
})
.option('target-locales', {
array: true,
describe:
'A list of target locales for the translation files, which will override any target locale parsed from the translation file.\n' +
'E.g. "-t en fr de".',
})
.option('o', {
@ -71,14 +80,12 @@ if (require.main === module) {
const sourceRootPath = options['r'];
const sourceFilePaths =
glob.sync(options['s'], {absolute: true, cwd: sourceRootPath, nodir: true});
const translationFilePaths = glob.sync(options['t'], {absolute: true, nodir: true});
const translationFilePaths: string[] = options['t'];
const outputPathFn = getOutputPathFn(options['o']);
const diagnostics = new Diagnostics();
const missingTranslation: MissingTranslationStrategy = options['m'];
const sourceLocale: string|undefined = options['l'];
// For CLI we do not have a way to specify the locale of the translation files
// It must be extracted from the file itself.
const translationFileLocales: string[] = [];
const translationFileLocales: string[] = options['target-locales'] || [];
translateFiles({sourceRootPath, sourceFilePaths, translationFilePaths, translationFileLocales,
outputPathFn, diagnostics, missingTranslation, sourceLocale});