mirror of
https://github.com/devcode-it/openstamanager.git
synced 2025-06-05 22:09:38 +02:00
improved(frontend): Migliorato ottenimento traduzione nel frontend
Introdotta la gestione dei parametri JSX o Vnode di Mithril
This commit is contained in:
40
resources/js/Components/Page.js
vendored
40
resources/js/Components/Page.js
vendored
@@ -1,3 +1,5 @@
|
|||||||
|
import * as Mithril from 'mithril';
|
||||||
|
import * as render from 'mithril-node-render';
|
||||||
import Component from './Component';
|
import Component from './Component';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -16,15 +18,37 @@ export default class Page extends Component {
|
|||||||
...
|
...
|
||||||
} = JSON.parse($('#app').attr('data-page'));
|
} = JSON.parse($('#app').attr('data-page'));
|
||||||
|
|
||||||
__(key: string, replace: Object = {}) {
|
/**
|
||||||
let translation = this.page.translations[key]
|
* Ritorna una traduzione
|
||||||
? this.page.translations[key]
|
*
|
||||||
: key;
|
* @param {string|Mithril.Vnode} key Stringa di cui prelevare la traduzione
|
||||||
|
* @param {Object|boolean} replace Eventuali parametri da rimpiazzare.
|
||||||
Object.keys(replace).forEach((k: string) => {
|
* Se il parametro è "true" (valore booleano), verrà ritornato il valore come stringa
|
||||||
translation = translation.replace(`:${k}`, replace[k]);
|
* (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;
|
||||||
|
|
||||||
|
// Ritorna la traduzione come stringa (senza sostituzione di parametri)
|
||||||
|
if (replace === true) {
|
||||||
return translation;
|
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 returnAsString ? translation : Mithril.m.trust(translation);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user