We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
src/locales/en.json
{ "title": "Hello" }
src/utils/l10n.ts
import en from '../locales/en.json'; type ReplacerType = { pattern: string; replacer: any; }; const MESSAGES_ALL = { en, }; export type SupportedMessagesType = keyof typeof MESSAGES_ALL; export class Localization { private locale: string; private bundle: { [id: string]: string }; constructor() { const language = window.navigator.language.slice(0, 2).toLowerCase(); this.setLocale(language as any); } setLocale = (locale: SupportedMessagesType) => { this.locale = locale && MESSAGES_ALL[locale] ? locale : 'en'; this.bundle = MESSAGES_ALL[this.locale]; }; getLocale = () => this.locale; getString(id: string, replacer?: ReplacerType): string | any[] { const value = this.bundle[id]; if (!value) { return ''; } if (!replacer) { return value; } const splitted = value.split(replacer.pattern); if (splitted.length > 1) { return [ splitted[0], replacer.replacer, splitted[1], ]; } return value; } } export const l10n = new Localization(); export const getString = l10n.getString.bind(l10n);
Usage:
import { getString } from'../utils/l10n'; getString('title'); // => 'Hello'
The text was updated successfully, but these errors were encountered:
donskov
No branches or pull requests
src/locales/en.json
src/utils/l10n.ts
Usage:
The text was updated successfully, but these errors were encountered: