This repository has been archived on 2024-07-27. You can view files and clone it, but cannot push or open issues or pull requests.
keksAccountGUI/node_modulesOLD/vuetify/lib/services/lang/index.js

50 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2019-08-11 18:48:02 +00:00
// Extensions
import { Service } from '../service'; // Language
import en from '../../locale/en'; // Utilities
import { getObjectValueByPath } from '../../util/helpers';
import { consoleError, consoleWarn } from '../../util/console';
const LANG_PREFIX = '$vuetify.';
const fallback = Symbol('Lang fallback');
function getTranslation(locale, key, usingFallback = false) {
const shortKey = key.replace(LANG_PREFIX, '');
let translation = getObjectValueByPath(locale, shortKey, fallback);
if (translation === fallback) {
if (usingFallback) {
consoleError(`Translation key "${shortKey}" not found in fallback`);
translation = key;
} else {
consoleWarn(`Translation key "${shortKey}" not found, falling back to default`);
translation = getTranslation(en, key, true);
}
}
return translation;
}
export class Lang extends Service {
constructor(options = {}) {
super();
this.current = options.current || 'en';
this.locales = Object.assign({
en
}, options.locales);
this.translator = options.t;
}
t(key, ...params) {
if (!key.startsWith(LANG_PREFIX)) return key;
if (this.translator) return this.translator(key, ...params);
const translation = getTranslation(this.locales[this.current], key);
return translation.replace(/\{(\d+)\}/g, (match, index) => {
/* istanbul ignore next */
return String(params[+index]);
});
}
}
Lang.property = 'lang';
//# sourceMappingURL=index.js.map