diff --git a/resources/js/Components/Page.js b/resources/js/Components/Page.js index eef0919c2..33948ee27 100644 --- a/resources/js/Components/Page.js +++ b/resources/js/Components/Page.js @@ -1,3 +1,5 @@ +import * as Mithril from 'mithril'; +import * as render from 'mithril-node-render'; import Component from './Component'; /** @@ -16,15 +18,37 @@ export default class Page extends Component { ... } = JSON.parse($('#app').attr('data-page')); - __(key: string, replace: Object = {}) { - let translation = this.page.translations[key] - ? this.page.translations[key] - : key; + /** + * Ritorna una traduzione + * + * @param {string|Mithril.Vnode} key Stringa di cui prelevare la traduzione + * @param {Object|boolean} replace Eventuali parametri da rimpiazzare. + * Se il parametro è "true" (valore booleano), verrà ritornato il valore come stringa + * (stesso funzionamento del parametro dedicato (sotto ↓)) + * @param {boolean} returnAsString Se impostato a "true" vien ritornata una stringa invece di + * un Vnode di Mithril + * @returns {Mithril.Vnode} + * + * @protected + */ + __( + key: string | Mithril.Vnode, + replace: Object | boolean = {}, + returnAsString: boolean = false + ): Mithril.Vnode { + let translation = (this.page.translations && this.page.translations[key]) + ? this.page.translations[key] : key; - Object.keys(replace).forEach((k: string) => { - translation = translation.replace(`:${k}`, replace[k]); + // Ritorna la traduzione come stringa (senza sostituzione di parametri) + if (replace === true) { + return translation; + } + + Object.keys(replace).forEach(async (k: string) => { + // "'attrs' in replace[k]" controlla se replace[k] è un vnode di Mithril + translation = translation.replace(`:${k}`, ((typeof replace[k] === 'object' && 'attrs' in replace[k]) ? render.sync(replace[k]) : replace[k])); }); - return translation; + return returnAsString ? translation : Mithril.m.trust(translation); } }