Skip to content

Commit

Permalink
feat(i18n): add i18n support
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Chernitsyn authored Mar 10, 2023
2 parents 80bf6a3 + 6753e6b commit 5cbc143
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
"build": "tsc",
"start": "TS_NODE_PROJECT=.storybook/tsconfig.json start-storybook"
},
"dependencies": {},
"dependencies": {
"@gravity-ui/i18n": "^1.0.0"
},
"devDependencies": {
"@commitlint/cli": "^17.0.0",
"@commitlint/config-conventional": "^17.0.0",
Expand Down
33 changes: 33 additions & 0 deletions src/components/utils/configure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export enum Lang {
Ru = 'ru',
En = 'en',
}

interface Config {
lang: `${Lang}`;
}

type Subscriber = (config: Config) => void;

let subs: Subscriber[] = [];

const config: Config = {
lang: Lang.En,
};

export const configure = (newConfig: Partial<Config>) => {
Object.assign(config, newConfig);
subs.forEach((sub) => {
sub(config);
});
};

export const subscribeConfigure = (sub: Subscriber) => {
subs.push(sub);

return () => {
subs = subs.filter((item) => item !== sub);
};
};

export const getConfig = () => config;
10 changes: 10 additions & 0 deletions src/components/utils/registerKeyset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {i18n} from '../../i18n';
import {Lang} from './configure';

type KeysData = Parameters<typeof i18n.registerKeyset>[2];

export function registerKeyset<T extends KeysData>(data: Record<Lang, T>, keysetName: string) {
Object.entries(data).forEach(([lang, keys]) => i18n.registerKeyset(lang, keysetName, keys));

return i18n.keyset(keysetName);
}
10 changes: 10 additions & 0 deletions src/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {I18N} from '@gravity-ui/i18n';
import {getConfig, subscribeConfigure} from './components/utils/configure';

export const i18n = new I18N();

i18n.setLang(getConfig().lang);

subscribeConfigure((config) => {
i18n.setLang(config.lang);
});

0 comments on commit 5cbc143

Please sign in to comment.