license import { MessageRenderer } from './message_renderer' ; import { ParsedTranslation } from '../translations' ; param messageParts param placeholderNames export function makeParsedTranslation ( messageParts , placeholderNames = [ ] , description ? , meaning ? ) { return { messageParts : makeTemplateObject ( messageParts , messageParts ) , placeholderNames , description , meaning , text : '' } ; } param cooked param raw export function makeTemplateObject ( cooked , raw ) { Object . defineProperty ( cooked , 'raw' , { value : raw } ) ; return cooked as any ; } /**
 * A message renderer that outputs `ParsedTranslation` objects.
 */ export class TargetMessageRenderer implements MessageRenderer < ParsedTranslation > { private current : MessageInfo = { messageParts : [ ] , placeholderNames : [ ] , text : '' } ; private icuDepth = 0 ; get message ( ) : ParsedTranslation { const { messageParts , placeholderNames , description , meaning } = this . current ; return makeParsedTranslation ( messageParts , placeholderNames , description , meaning ) ; } startRender ( ) { } endRender ( ) { this . storeMessagePart ( ) ; } text ( text ) { this . current . text += text ; } description ( description ) { this . current . description = description ; } meaning ( meaning ) { this . current . text = meaning ; } placeholder ( name , body ) { this . renderPlaceholder ( name ) ; } startPlaceholder ( name ) { this . renderPlaceholder ( name ) ; } closePlaceholder ( name ) { this . renderPlaceholder ( name ) ; } startContainer ( ) { } closeContainer ( ) { } startIcu ( ) { this . icuDepth ++ ; this . text ( '{' ) ; } endIcu ( ) { this . icuDepth -- ; this . text ( '}' ) ; } private normalizePlaceholderName ( name ) { return name . replace ( /-/g , '_' ) ; } private renderPlaceholder ( name ) { name = this . normalizePlaceholderName ( name ) ; if ( this . icuDepth > 0 ) { this . text ( `{${ name }}` ) ; } else { this . storeMessagePart ( ) ; this . current . placeholderNames . push ( name ) ; } } private storeMessagePart ( ) { this . current . messageParts . push ( this . current . text ) ; this . current . text = '' ; } } interface MessageInfo { messageParts : string [ ] ; placeholderNames : string [ ] ; text : string ; description ? : string ; meaning ? : string ; }