Skip to content

Commit abc9298

Browse files
committed
feat: add i18n
1 parent f506bb8 commit abc9298

File tree

11 files changed

+68
-5
lines changed

11 files changed

+68
-5
lines changed

.storybook/decorators/withLang.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react';
2+
import {Story as StoryType, StoryContext} from '@storybook/react';
3+
import {I18N} from '@yandex-cloud/i18n';
4+
5+
export function withLang(Story: StoryType, context: StoryContext) {
6+
const lang = context.globals.lang;
7+
8+
I18N.setDefaultLang(lang);
9+
10+
return <Story key={lang} {...context} />;
11+
}

.storybook/preview.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import '@yandex-cloud/uikit/styles/styles.scss';
22

33
import React from 'react';
4+
import {I18N} from '@yandex-cloud/i18n';
45
import {ThemeProvider} from '@yandex-cloud/uikit';
56
import {withTheme} from './decorators/withTheme';
7+
import {withLang} from './decorators/withLang';
8+
9+
I18N.setDefaultLang(I18N.LANGS.ru);
610

711
const withContextProvider = (Story, context) => {
812
const theme = context.globals.theme;
@@ -21,7 +25,7 @@ const withContextProvider = (Story, context) => {
2125
);
2226
};
2327

24-
export const decorators = [withTheme, withContextProvider];
28+
export const decorators = [withTheme, withLang, withContextProvider];
2529

2630
export const parameters = {
2731
jsx: {showFunctions: true},
@@ -46,4 +50,16 @@ export const globalTypes = {
4650
],
4751
},
4852
},
53+
lang: {
54+
name: 'Language',
55+
description: 'Global language for components',
56+
defaultValue: 'ru',
57+
toolbar: {
58+
icon: 'globe',
59+
items: [
60+
{value: 'ru', right: '🇷🇺', title: 'Русский'},
61+
{value: 'en', right: '🇬🇧', title: 'English'},
62+
],
63+
},
64+
},
4965
};

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@
55
"license": "MIT",
66
"main": "build/index.js",
77
"types": "build/index.d.ts",
8-
"sideEffects": ["*.css", "*.scss"],
9-
"files": ["build"],
8+
"sideEffects": [
9+
"*.css",
10+
"*.scss"
11+
],
12+
"files": [
13+
"build"
14+
],
1015
"scripts": {
1116
"clean": "gulp clean",
1217
"build": "gulp",
1318
"test": "echo \"Error: no test specified\" && exit 0",
1419
"start": "start-storybook -p 7002"
1520
},
1621
"dependencies": {
22+
"@yandex-cloud/i18n": "^0.2.2",
1723
"bem-cn-lite": "^4.1.0"
1824
},
1925
"devDependencies": {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import React from 'react';
22
import block from 'bem-cn-lite';
33

4+
import i18n from '../i18n';
45
import {TreeView} from '../../TreeView/TreeView';
56

67
import './EmptyView.scss';
78

89
const b = block('ydb-navigation-tree-view-empty');
910

1011
export function EmptyView() {
11-
return <TreeView name={<span className={b()}>Нет данных</span>} />;
12+
return <TreeView name={<span className={b()}>{i18n('label_empty')}</span>} />;
1213
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import React from 'react';
22
import block from 'bem-cn-lite';
33

4+
import i18n from '../i18n';
45
import {TreeView} from '../../TreeView/TreeView';
56

67
import './ErrorView.scss';
78

89
const b = block('ydb-navigation-tree-view-error');
910

1011
export function ErrorView() {
11-
return <TreeView name={<span className={b()}>Ошибка</span>} />;
12+
return <TreeView name={<span className={b()}>{i18n('label_error')}</span>} />;
1213
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"label_error": "Error",
3+
"label_empty": "No data"
4+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {i18n, I18N} from '../../i18n';
2+
import en from './en.json';
3+
import ru from './ru.json';
4+
5+
const COMPONENT = 'ydb-navigation-tree';
6+
7+
i18n.registerKeyset(I18N.LANGS.en, COMPONENT, en);
8+
i18n.registerKeyset(I18N.LANGS.ru, COMPONENT, ru);
9+
10+
export default i18n.keyset(COMPONENT);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"label_error": "Ошибка",
3+
"label_empty": "Нет данных"
4+
}

src/components/i18n.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {I18N} from '@yandex-cloud/i18n';
2+
3+
export const i18n = new I18N();
4+
export {I18N} from '@yandex-cloud/i18n';

0 commit comments

Comments
 (0)