UNPKG

ngx-translate-formatjs-compiler

Version:

[Format.js](https://formatjs.io/) based [@ngx/translate](https://github.com/ngx-translate/core) compiler.

60 lines (55 loc) 2.18 kB
import * as i0 from '@angular/core'; import { Injectable } from '@angular/core'; import { TranslateCompiler } from '@ngx-translate/core'; import { IntlMessageFormat } from 'intl-messageformat'; class TranslateFormatJsCompiler extends TranslateCompiler { /** Compiles single translation */ compile(value, lang) { return (params) => { try { return new IntlMessageFormat(value, lang).format(params); } catch (e) { // eslint-disable-next-line no-console console.error(e); return `ERROR in: "${value}"`; } }; } /** Compile translations object */ compileTranslations(translations, lang) { this.compileRecursively(translations, lang); return translations; } /** * Traverse through object and replace translations strings with compile function. * @param obj to be traversed * @param lang from @ngx-translate * @returns translation object with compile functions. */ compileRecursively(obj, lang) { Object.keys(obj).forEach((key) => { const value = obj[key]; if (typeof value === 'string') { // eslint-disable-next-line no-param-reassign obj[key] = this.compile(value, lang); } else { this.compileRecursively(value, lang); } }); } } TranslateFormatJsCompiler.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: TranslateFormatJsCompiler, deps: null, target: i0.ɵɵFactoryTarget.Injectable }); TranslateFormatJsCompiler.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: TranslateFormatJsCompiler }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: TranslateFormatJsCompiler, decorators: [{ type: Injectable }] }); /* * Public API Surface of lib */ /** * Generated bundle index. Do not edit. */ export { TranslateFormatJsCompiler }; //# sourceMappingURL=ngx-translate-formatjs-compiler.mjs.map