mirror of
https://github.com/flarum/framework.git
synced 2025-05-23 23:29:57 +08:00
Basic parsing of HTML tags in translations
This allows text to be wrapped with a virtual element: key: "This is a <test>Test</test>" app.trans('key', {test: <a href="#"/>}); closes #574
This commit is contained in:
@ -38,7 +38,7 @@ export default class Translator {
|
|||||||
* @param {VirtualElement} fallback
|
* @param {VirtualElement} fallback
|
||||||
* @return {VirtualElement}
|
* @return {VirtualElement}
|
||||||
*/
|
*/
|
||||||
trans(key, input = {}, fallback) {
|
trans(key, input = {}, fallback = null) {
|
||||||
const parts = key.split('.');
|
const parts = key.split('.');
|
||||||
let translation = this.translations;
|
let translation = this.translations;
|
||||||
|
|
||||||
@ -69,16 +69,32 @@ export default class Translator {
|
|||||||
// If we've found the appropriate translation string, then we'll sub in the
|
// If we've found the appropriate translation string, then we'll sub in the
|
||||||
// input.
|
// input.
|
||||||
if (typeof translation === 'string') {
|
if (typeof translation === 'string') {
|
||||||
translation = translation.split(new RegExp('({[^}]+})', 'gi'));
|
translation = translation.split(new RegExp('({[a-z0-9_]+}|</?[a-z0-9_]+>)', 'gi'));
|
||||||
|
|
||||||
|
const hydrated = [];
|
||||||
|
const open = [hydrated];
|
||||||
|
|
||||||
|
translation.forEach(part => {
|
||||||
|
const match = part.match(new RegExp('{([a-z0-9_]+)}|<(/?)([a-z0-9_]+)>', 'i'));
|
||||||
|
|
||||||
translation.forEach((part, i) => {
|
|
||||||
const match = part.match(/^{(.+)}$/i);
|
|
||||||
if (match) {
|
if (match) {
|
||||||
translation[i] = input[match[1]];
|
if (match[1]) {
|
||||||
|
open[0].push(input[match[1]]);
|
||||||
|
} else if (match[3]) {
|
||||||
|
if (match[2]) {
|
||||||
|
open.shift();
|
||||||
|
} else {
|
||||||
|
let tag = input[match[3]] || [];
|
||||||
|
open[0].push(tag);
|
||||||
|
open.unshift(tag.children || tag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
open[0].push(part);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return translation.filter(part => part);
|
return hydrated.filter(part => part);
|
||||||
}
|
}
|
||||||
|
|
||||||
return fallback || [key];
|
return fallback || [key];
|
||||||
|
Reference in New Issue
Block a user