From 3962908d36f52ede9deacc57c915c90fbab27abf Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Sun, 19 Apr 2020 17:42:37 +0100 Subject: [PATCH] refactor(localize): tighten up recognition of simple JSON translations (#36745) PR Close #36745 --- .../simple_json_translation_parser.ts | 18 +++++++++++++----- .../translation_parsers/simple_json_spec.ts | 15 ++++++++++----- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/packages/localize/src/tools/src/translate/translation_files/translation_parsers/simple_json_translation_parser.ts b/packages/localize/src/tools/src/translate/translation_files/translation_parsers/simple_json_translation_parser.ts index 7a3ce58738..30115f30ad 100644 --- a/packages/localize/src/tools/src/translate/translation_files/translation_parsers/simple_json_translation_parser.ts +++ b/packages/localize/src/tools/src/translate/translation_files/translation_parsers/simple_json_translation_parser.ts @@ -23,13 +23,21 @@ import {ParsedTranslationBundle, TranslationParser} from './translation_parser'; * } * ``` */ -export class SimpleJsonTranslationParser implements TranslationParser { - canParse(filePath: string, _contents: string): boolean { - return (extname(filePath) === '.json'); +export class SimpleJsonTranslationParser implements TranslationParser { + canParse(filePath: string, contents: string): Object|false { + if (extname(filePath) !== '.json') { + return false; + } + try { + const json = JSON.parse(contents); + return (typeof json.locale === 'string' && typeof json.translations === 'object') && json; + } catch { + return false; + } } - parse(_filePath: string, contents: string): ParsedTranslationBundle { - const {locale: parsedLocale, translations} = JSON.parse(contents); + parse(_filePath: string, contents: string, json?: Object): ParsedTranslationBundle { + const {locale: parsedLocale, translations} = json || JSON.parse(contents); const parsedTranslations: Record<ɵMessageId, ɵParsedTranslation> = {}; for (const messageId in translations) { const targetMessage = translations[messageId]; diff --git a/packages/localize/src/tools/test/translate/translation_files/translation_parsers/simple_json_spec.ts b/packages/localize/src/tools/test/translate/translation_files/translation_parsers/simple_json_spec.ts index 3607fb413b..2f3276f95c 100644 --- a/packages/localize/src/tools/test/translate/translation_files/translation_parsers/simple_json_spec.ts +++ b/packages/localize/src/tools/test/translate/translation_files/translation_parsers/simple_json_spec.ts @@ -10,11 +10,16 @@ import {SimpleJsonTranslationParser} from '../../../../src/translate/translation describe('SimpleJsonTranslationParser', () => { describe('canParse()', () => { - it('should return true if the file extension is `.json`', () => { - const parser = new SimpleJsonTranslationParser(); - expect(parser.canParse('/some/file.xlf', '')).toBe(false); - expect(parser.canParse('/some/file.json', '')).toBe(true); - }); + it('should return true if the file extension is `.json` and contains top level `locale` and `translations` properties', + () => { + const parser = new SimpleJsonTranslationParser(); + expect(parser.canParse('/some/file.xlf', '')).toBe(false); + expect(parser.canParse('/some/file.json', '{}')).toBe(false); + expect(parser.canParse('/some/file.json', '{ "translations" : {} }')).toBe(false); + expect(parser.canParse('/some/file.json', '{ "locale" : "fr" }')).toBe(false); + expect(parser.canParse('/some/file.json', '{ "locale" : "fr", "translations" : {}}')) + .toBeTruthy(); + }); }); describe('parse()', () => {