This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Zaimki/src/translator.js

25 lines
700 B
JavaScript
Raw Normal View History

import translations from '../data/translations.suml';
2020-12-08 14:14:40 -08:00
export default (key, params = {}, warn = true) => {
let value = translations;
for (let part of key.split('.')) {
value = value[part];
if (value === undefined) {
2020-12-08 14:14:40 -08:00
if (warn) {
console.error('Cannot find translation: ' + key);
}
return undefined;
}
}
2020-10-14 12:46:39 -07:00
for (let k in params) {
if (params.hasOwnProperty(k)) {
2020-12-18 02:34:58 -08:00
value = Array.isArray(value)
? value.map(v => v.replace(new RegExp('%' + k + '%', 'g'), params[k]))
: value.replace(new RegExp('%' + k + '%', 'g'), params[k]);
2020-10-14 12:46:39 -07:00
}
}
return value;
}