Files
mixtape/zero.Web.UI/App/services/localization.js
T

52 lines
982 B
JavaScript
Raw Normal View History

import { extend as _extend, each as _each } from 'underscore';
2020-04-06 13:24:46 +02:00
export default {
localize(key, options)
2020-04-06 13:24:46 +02:00
{
let params = _extend({
force: false,
2020-06-23 12:23:23 +02:00
tokens: {},
hideEmpty: false
}, options || {});
2020-04-06 13:24:46 +02:00
if (!key)
{
return '';
}
const hasAtSign = key.indexOf('@') === 0;
if (!params.force && !hasAtSign)
2020-04-06 13:24:46 +02:00
{
return this.replaceTokens(key, params.tokens);
2020-04-06 13:24:46 +02:00
}
key = hasAtSign ? key.slice(1) : key;
2020-10-16 14:02:10 +02:00
const value = __zero.translations[key.toLowerCase()];
2020-04-06 13:24:46 +02:00
2020-04-30 14:21:43 +02:00
// TODO only return key if in debug mode
2020-06-23 12:23:23 +02:00
if (!params.hideEmpty && (!value || typeof value !== 'string'))
2020-04-06 13:24:46 +02:00
{
2020-05-14 13:52:49 +02:00
return '[' + key + ']';
2020-04-06 13:24:46 +02:00
}
return this.replaceTokens(value, params.tokens);
},
replaceTokens(value, tokens)
{
if (!value || value.indexOf('{') < 0)
{
return value;
}
_each(tokens, (replacement, key) =>
{
value = value.replace("{" + key + "}", replacement);
});
2020-04-06 13:24:46 +02:00
return value;
}
};