Files
mixtape/zero.Backoffice.UI/app/services/localization.ts
T
2021-12-06 16:18:58 +01:00

60 lines
1.1 KiB
TypeScript

import { extendObject } from '../utils/objects';
import { useTranslationStore } from '../stores/translations';
export interface LocalizeOptions
{
force: boolean,
tokens: object,
hideEmpty: boolean
}
export const localize = (key: string, options?: LocalizeOptions): string =>
{
let params = extendObject({
force: false,
tokens: {},
hideEmpty: false
}, options || {});
if (!key)
{
return '';
}
const hasAtSign = key.indexOf('@') === 0;
if (!params.force && !hasAtSign)
{
return replaceTokens(key, params.tokens);
}
key = hasAtSign ? key.slice(1) : key;
const store = useTranslationStore();
const value = store.find(key);
// TODO only return key if in debug mode
if (!params.hideEmpty && (!value || typeof value !== 'string'))
{
return '[' + key + ']';
}
return replaceTokens(value, params.tokens);
};
export const replaceTokens = (value: string, tokens: Record<string, string>): string =>
{
if (!value || value.indexOf('{') < 0)
{
return value;
}
for (const key in tokens)
{
value = value.replace("{" + key + "}", tokens[key]);
}
return value;
};