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';
|
||||
|
||||
/**
|
||||
@@ -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;
|
||||
|
||||
Object.keys(replace).forEach((k: string) => {
|
||||
translation = translation.replace(`:${k}`, replace[k]);
|
||||
});
|
||||
/**
|
||||
* 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;
|
||||
|
||||
// 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 returnAsString ? translation : Mithril.m.trust(translation);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user