UNPKG

@cloarec/transloco-mf2

Version:

The implementation of the TranslocoTranspiler interface in this project uses [messageformat V4](https://messageformat.github.io/documents/Project_Overview.html). Since V4, the library [messageformat](https://github.com/messageformat/messageformat) provi

80 lines (74 loc) 3.04 kB
import * as i0 from '@angular/core'; import { InjectionToken, inject, Injectable, makeEnvironmentProviders } from '@angular/core'; import { DefaultTranspiler, isObject, getValue, setValue, provideTranslocoTranspiler } from '@jsverse/transloco'; import { MessageFormat } from 'messageformat'; import { DraftFunctions } from 'messageformat/functions'; const TRANSLOCO_MF2_CONFIG = new InjectionToken('TRANSLOCO_MF2_CONFIG'); class TranslocoMF2Transpiler extends DefaultTranspiler { mf2Config = inject(TRANSLOCO_MF2_CONFIG, { optional: true }); locale; constructor() { super(); } onLangChanged(lang) { this.locale = lang; } // eslint-disable-next-line @typescript-eslint/no-explicit-any transpile(transpileParams) { let value = transpileParams.value; if (!value) { return value; } const { params, translation, key } = transpileParams; if (isObject(value) && params) { Object.keys(params).forEach((p) => { const param = getValue(params, p); const transpiled = super.transpile({ value: getValue(value, p), params: param, translation, key, }); const mf = new MessageFormat(this.locale, transpiled, this.mf2Config || {}); value = setValue(value, p, mf.format(param)); }); return value; } if (!Array.isArray(value)) { const transpiled = super.transpile(transpileParams); const mf = new MessageFormat(this.locale, transpiled, this.mf2Config || {}); return mf.format(params); } return value; } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.7", ngImport: i0, type: TranslocoMF2Transpiler, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.7", ngImport: i0, type: TranslocoMF2Transpiler }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.7", ngImport: i0, type: TranslocoMF2Transpiler, decorators: [{ type: Injectable }], ctorParameters: () => [] }); function provideTranslocoMF2Transpiler(config) { return makeEnvironmentProviders([ { provide: TRANSLOCO_MF2_CONFIG, useValue: { ...config, functions: config?.includeDraftFunctions ? { ...DraftFunctions, ...config?.functions, } : config?.functions, }, }, provideTranslocoTranspiler(TranslocoMF2Transpiler), ]); } /* * Public API Surface of transloco-mf2 */ /** * Generated bundle index. Do not edit. */ export { TRANSLOCO_MF2_CONFIG, TranslocoMF2Transpiler, provideTranslocoMF2Transpiler }; //# sourceMappingURL=cloarec-transloco-mf2.mjs.map