license import { ɵMessageId , ɵParsedTranslation , ɵparseTranslation , } from '@angular/localize' ; import { Diagnostics } from '../../common/diagnostics' ; import { extname } from 'path' ; import { ParseAnalysis , ParsedTranslationBundle , TranslationParser , } from './translation_parser' ; /**
 * A translation parser that can parse JSON that has the form:
 *
 * ```
 * {
 *   "locale": "...",
 *   "translations": {
 *     "message-id": "Target message string",
 *     ...
 *   }
 * }
 * ```
 */ export class SimpleJsonTranslationParser implements TranslationParser < Object > { constructor ( private diagnostics ) { } canParse ( filePath , contents ) { const result = this . analyze ( filePath , contents ) ; return result . canParse && result . hint ; } analyze ( filePath , contents ) { const diagnostics = new Diagnostics ( ) ; if ( extname ( filePath ) !== '.json' ) { diagnostics . warn ( 'File does not have .json extension.' ) ; return { canParse : false , diagnostics } ; } try { const json = JSON . parse ( contents ) ; if ( json . locale === undefined ) { diagnostics . warn ( 'Required "locale" property missing.' ) ; return { canParse : false , diagnostics } ; } if ( typeof json . locale !== 'string' ) { diagnostics . warn ( 'The "locale" property is not a string.' ) ; return { canParse : false , diagnostics } ; } if ( json . translations === undefined ) { diagnostics . warn ( 'Required "translations" property missing.' ) ; return { canParse : false , diagnostics } ; } if ( typeof json . translations !== 'object' ) { diagnostics . warn ( 'The "translations" is not an object.' ) ; return { canParse : false , diagnostics } ; } return { canParse : true , diagnostics , hint : json } ; } catch ( e ) { diagnostics . warn ( 'File is not valid JSON.' ) ; return { canParse : false , diagnostics } ; } } parse ( _filePath , contents ) { const { locale : parsedLocale , translations } = JSON . parse ( contents ) ; const parsedTranslations = { } ; for ( const messageId in translations ) { const targetMessage = translations [ messageId ] ; parsedTranslations [ messageId ] = ɵparseTranslation ( targetMessage ) ; } return { locale : parsedLocale , translations : parsedTranslations , diagnostics : this . diagnostics , } ; } }