Skip to content

Commit

Permalink
feat(@angular-devkit/build-angular): support extracting with json file
Browse files Browse the repository at this point in the history
  • Loading branch information
wenqi73 authored and alan-agius4 committed Jan 8, 2021
1 parent c622d2e commit dc97667
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
6 changes: 4 additions & 2 deletions packages/angular/cli/lib/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1403,7 +1403,8 @@
"xlif",
"xliff",
"xlf2",
"xliff2"
"xliff2",
"json"
]
},
"i18nFormat": {
Expand All @@ -1417,7 +1418,8 @@
"xlif",
"xliff",
"xlf2",
"xliff2"
"xliff2",
"json"
]
},
"i18nLocale": {
Expand Down
11 changes: 11 additions & 0 deletions packages/angular_devkit/build_angular/src/extract-i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ function getI18nOutfile(format: string | undefined) {
case 'xlf2':
case 'xliff2':
return 'messages.xlf';
case 'json':
return 'messages.json';
default:
throw new Error(`Unsupported format "${format}"`);
}
Expand Down Expand Up @@ -65,6 +67,12 @@ async function getSerializer(format: Format, sourceLocale: string, basePath: str

// tslint:disable-next-line: no-any
return new Xliff2TranslationSerializer(sourceLocale, basePath as any, useLegacyIds, {});
case Format.Json:
const { SimpleJsonTranslationSerializer } =
await import('@angular/localize/src/tools/src/extract/translation_files/json_translation_serializer');

// tslint:disable-next-line: no-any
return new SimpleJsonTranslationSerializer(sourceLocale);
}
}

Expand All @@ -86,6 +94,9 @@ function normalizeFormatOption(options: ExtractI18nBuilderOptions) {
case Format.Xliff2:
format = Format.Xlf2;
break;
case Format.Json:
format = Format.Json;
break;
case undefined:
format = Format.Xlf;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"xlif",
"xliff",
"xlf2",
"xliff2"
"xliff2",
"json"
]
},
"i18nFormat": {
Expand All @@ -33,7 +34,8 @@
"xlif",
"xliff",
"xlf2",
"xliff2"
"xliff2",
"json"
]
},
"i18nLocale": {
Expand Down
10 changes: 10 additions & 0 deletions tests/legacy-cli/e2e/tests/i18n/ivy-localize-dl-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { executeTest } from './ivy-localize-dl-xliff2';
import { setupI18nConfig } from './legacy';

export default async function() {
// Setup i18n tests and config.
await setupI18nConfig(true, 'json');

// Execute the tests
await executeTest();
}
11 changes: 10 additions & 1 deletion tests/legacy-cli/e2e/tests/i18n/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ export const formats = {
[/<source>.*?<\/source>/g, ''],
],
},
'json': {
ext: 'json',
sourceCheck: '"locale": "en-US"',
replacements: [
],
}
};

export async function setupI18nConfig(useLocalize = true, format: keyof typeof formats = 'xlf') {
Expand Down Expand Up @@ -226,7 +232,10 @@ export async function setupI18nConfig(useLocalize = true, format: keyof typeof f
const translationFile = `src/locale/messages.${formats[format].ext}`;
await expectFileToExist(translationFile);
await expectFileToMatch(translationFile, formats[format].sourceCheck);
await expectFileToMatch(translationFile, `An introduction header for this sample`);

if (format !== 'json') {
await expectFileToMatch(translationFile, `An introduction header for this sample`);
}

// Make translations for each language.
for (const { lang, translationReplacements } of langTranslations) {
Expand Down

0 comments on commit dc97667

Please sign in to comment.