mirror of
https://github.com/flarum/framework.git
synced 2024-11-25 17:57:04 +08:00
33 lines
726 B
JavaScript
33 lines
726 B
JavaScript
|
export default class Translator {
|
||
|
constructor() {
|
||
|
this.translations = {};
|
||
|
}
|
||
|
|
||
|
plural(count) {
|
||
|
return count == 1 ? 'one' : 'other';
|
||
|
}
|
||
|
|
||
|
translate(key, input) {
|
||
|
var parts = key.split('.');
|
||
|
var translation = this.translations;
|
||
|
|
||
|
parts.forEach(function(part) {
|
||
|
translation = translation && translation[part];
|
||
|
});
|
||
|
|
||
|
if (typeof translation === 'object' && typeof input.count !== 'undefined') {
|
||
|
translation = translation[this.plural(input.count)];
|
||
|
}
|
||
|
|
||
|
if (typeof translation === 'string') {
|
||
|
for (var i in input) {
|
||
|
translation = translation.replace(new RegExp('{'+i+'}', 'gi'), input[i]);
|
||
|
}
|
||
|
|
||
|
return translation;
|
||
|
} else {
|
||
|
return key;
|
||
|
}
|
||
|
}
|
||
|
}
|