| 20 | * A container for translated messages |
| 21 | */ |
| 22 | export class TranslationBundle { |
| 23 | private _i18nToHtml: I18nToHtmlVisitor; |
| 24 | |
| 25 | constructor( |
| 26 | private _i18nNodesByMsgId: {[msgId: string]: i18n.Node[]} = {}, |
| 27 | locale: string | null, |
| 28 | public digest: (m: i18n.Message) => string, |
| 29 | public mapperFactory?: (m: i18n.Message) => PlaceholderMapper, |
| 30 | missingTranslationStrategy: MissingTranslationStrategy = MissingTranslationStrategy.Warning, |
| 31 | console?: Console, |
| 32 | ) { |
| 33 | this._i18nToHtml = new I18nToHtmlVisitor( |
| 34 | _i18nNodesByMsgId, |
| 35 | locale, |
| 36 | digest, |
| 37 | mapperFactory!, |
| 38 | missingTranslationStrategy, |
| 39 | console, |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | // Creates a `TranslationBundle` by parsing the given `content` with the `serializer`. |
| 44 | static load( |
| 45 | content: string, |
| 46 | url: string, |
| 47 | serializer: Serializer, |
| 48 | missingTranslationStrategy: MissingTranslationStrategy, |
| 49 | console?: Console, |
| 50 | ): TranslationBundle { |
| 51 | const {locale, i18nNodesByMsgId} = serializer.load(content, url); |
| 52 | const digestFn = (m: i18n.Message) => serializer.digest(m); |
| 53 | const mapperFactory = (m: i18n.Message) => serializer.createNameMapper(m)!; |
| 54 | return new TranslationBundle( |
| 55 | i18nNodesByMsgId, |
| 56 | locale, |
| 57 | digestFn, |
| 58 | mapperFactory, |
| 59 | missingTranslationStrategy, |
| 60 | console, |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | // Returns the translation as HTML nodes from the given source message. |
| 65 | get(srcMsg: i18n.Message): html.Node[] { |
| 66 | const html = this._i18nToHtml.convert(srcMsg); |
| 67 | |
| 68 | if (html.errors.length) { |
| 69 | throw new Error(html.errors.join('\n')); |
| 70 | } |
| 71 | |
| 72 | return html.nodes; |
| 73 | } |
| 74 | |
| 75 | has(srcMsg: i18n.Message): boolean { |
| 76 | return this.digest(srcMsg) in this._i18nNodesByMsgId; |
| 77 | } |
| 78 | } |
| 79 |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…