UNPKG

@larva.io/webcomponents

Version:

Fentrica SmartUnits WebComponents package

147 lines (146 loc) 4.43 kB
import { i18n } from "../../../utils/i18n"; export class LarvaTranslate { constructor() { this.i18n = i18n; // input text this.t = ''; // translated (text this.tt = ''; } changeHandler() { try { const exists = this.i18n.exists(this.t, { lng: this.i18n.language }); const fallback = this.fallback ? this.fallback : this.t; this.tt = exists ? this.i18n.t(this.t, { lng: this.i18n.language }) : fallback; } catch (err) { console.error(this.t); console.error(err); } } componentWillLoad() { this.changeHandler(); this.bindedListener = () => { this.larlangchange.emit(); this.changeHandler(); }; this.i18n.on('languageChanged', this.bindedListener); this.i18n.on('languagechanged', this.bindedListener); } disconnectedCallback() { try { this.i18n.off('languageChanged', this.bindedListener); this.i18n.off('languagechanged', this.bindedListener); } catch (err) { console.error(err); } } fillTranslationValues(translation = '', values) { if (!values) { return translation; } return translation.replace(/{\w+}/g, (key) => { const parsedKey = key.substring(1, key.length - 1); return (values[parsedKey] || 'undefined').toString(); }); } render() { return this.fillTranslationValues(this.tt, this.values); } static get is() { return "lar-translate"; } static get properties() { return { "values": { "type": "any", "mutable": false, "complexType": { "original": "any", "resolved": "any", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "" }, "getter": false, "setter": false, "reflect": false, "attribute": "values" }, "t": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "" }, "getter": false, "setter": false, "reflect": false, "attribute": "t", "defaultValue": "''" }, "fallback": { "type": "string", "mutable": false, "complexType": { "original": "string | null", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "" }, "getter": false, "setter": false, "reflect": false, "attribute": "fallback" } }; } static get states() { return { "tt": {}, "bindedListener": {} }; } static get events() { return [{ "method": "larlangchange", "name": "larlangchange", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [], "text": "Emitted when the value property has changed." }, "complexType": { "original": "void", "resolved": "void", "references": {} } }]; } static get elementRef() { return "el"; } static get watchers() { return [{ "propName": "t", "methodName": "changeHandler" }]; } } //# sourceMappingURL=translate.js.map