Skip to content

Commit 4e2c49c

Browse files
committed
feat: add i18n
1 parent d73eb8e commit 4e2c49c

File tree

7 files changed

+87
-5
lines changed

7 files changed

+87
-5
lines changed

.storybook/preview.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {withTheme, withThemeProvider, backgrounds} from '../demo/utils/preview';
1+
import {withTheme, withThemeProvider, withLang, backgrounds} from '../demo/utils/preview';
22

33
export const parameters = {
44
actions: {argTypesRegex: '^on[A-Z].*'},
@@ -22,4 +22,18 @@ export const parameters = {
2222
},
2323
};
2424

25-
export const decorators = [withTheme, withThemeProvider];
25+
export const globalTypes = {
26+
lang: {
27+
name: 'Language',
28+
defaultValue: 'en',
29+
toolbar: {
30+
icon: 'globe',
31+
items: [
32+
{value: 'ru', right: '🇷🇺', title: 'Ru'},
33+
{value: 'en', right: '🇺🇸', title: 'En'},
34+
],
35+
},
36+
},
37+
};
38+
39+
export const decorators = [withTheme, withThemeProvider, withLang];

demo/utils/preview.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import {Story, StoryContext} from '@storybook/react/types-6-0';
3-
import {useTheme, ThemeProvider, Theme} from '@gravity-ui/uikit';
3+
import {configure as configureUikit, useTheme, ThemeProvider, Theme} from '@gravity-ui/uikit';
4+
import {configure as configureYfmEditor} from '../../src';
45

56
import '@gravity-ui/uikit/styles/styles.scss';
67

@@ -35,3 +36,11 @@ export function withThemeProvider(StoryItem: Story, context: StoryContext) {
3536
</ThemeProvider>
3637
);
3738
}
39+
40+
export function withLang(StoryItem: Story, context: StoryContext) {
41+
const lang = context.globals.lang;
42+
configureUikit({lang});
43+
configureYfmEditor({lang});
44+
45+
return <StoryItem {...context} />;
46+
}

package-lock.json

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"license": "MIT",
4040
"dependencies": {
4141
"@bem-react/classname": "^1.5.12",
42+
"@gravity-ui/i18n": "1.0.0",
4243
"@types/is-number": "^7.0.1",
4344
"@types/markdown-it": "^12.2.3",
4445
"is-number": "^7.0.0",

src/configure.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export enum Lang {
2+
Ru = 'ru',
3+
En = 'en',
4+
}
5+
6+
interface Config {
7+
lang?: `${Lang}`;
8+
}
9+
10+
type Subscriber = (config: Readonly<Config>) => void;
11+
12+
let subs: Subscriber[] = [];
13+
14+
const config: Config = {};
15+
16+
export const configure = (newConfig: Config) => {
17+
Object.assign(config, newConfig);
18+
subs.forEach((sub) => {
19+
sub(config);
20+
});
21+
};
22+
23+
export const subscribeConfigure = (sub: Subscriber) => {
24+
subs.push(sub);
25+
26+
return () => {
27+
subs = subs.filter((item) => item !== sub);
28+
};
29+
};
30+
31+
export const getConfig = (): Readonly<Config> => config;

src/i18n/i18n.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {I18N, I18NFn} from '@gravity-ui/i18n';
2+
import {getConfig, Lang, subscribeConfigure} from '../configure';
3+
4+
type KeysData = Record<string, string | string[]>; // @gravity-ui/i18n inner type
5+
6+
const i18n = new I18N();
7+
8+
// en – default lang for editor
9+
i18n.setLang(getConfig().lang || Lang.En);
10+
11+
subscribeConfigure((config) => {
12+
if (config.lang) {
13+
i18n.setLang(config.lang);
14+
}
15+
});
16+
17+
export {i18n};
18+
export function registerKeyset<K extends string, D extends KeysData>(
19+
keyset: K,
20+
data: Record<'en' | 'ru', D>,
21+
) {
22+
i18n.registerKeyset(Lang.En, keyset, data.en);
23+
i18n.registerKeyset(Lang.Ru, keyset, data.ru);
24+
25+
return (i18n as unknown as I18NFn<Record<K, D>>).keyset(keyset);
26+
}

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export * from './logger';
88
export * from './extensions';
99
export * from './extensions/specs';
1010

11+
export {Lang, configure} from './configure';
12+
1113
export {isMac} from './utils/platform';
1214
export {markInputRule, nodeInputRule, inlineNodeInputRule} from './utils/inputrules';
1315
export {findMark, isMarkActive} from './utils/marks';

0 commit comments

Comments
 (0)