-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(I18N): add configure function for manage internal variables like…
… language (#186)
- Loading branch information
Showing
9 changed files
with
72 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import React from 'react'; | ||
import {Story as StoryType, StoryContext} from '@storybook/react'; | ||
import {I18N} from '@yandex-cloud/i18n'; | ||
import {Lang, configure} from '../../src/components/utils/configure'; | ||
|
||
export function withLang(Story: StoryType, context: StoryContext) { | ||
const lang = context.globals.lang; | ||
|
||
I18N.setDefaultLang(lang); | ||
configure({ | ||
lang: lang as Lang, | ||
}); | ||
|
||
return <Story key={lang} {...context} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import {i18n, I18N} from '../../../i18n'; | ||
import {i18n} from '../../../i18n'; | ||
import {Lang} from '../../utils/configure'; | ||
import en from './en.json'; | ||
import ru from './ru.json'; | ||
|
||
const COMPONENT = 'Stories'; | ||
|
||
i18n.registerKeyset(I18N.LANGS.en, COMPONENT, en); | ||
i18n.registerKeyset(I18N.LANGS.ru, COMPONENT, ru); | ||
i18n.registerKeyset(Lang.En, COMPONENT, en); | ||
i18n.registerKeyset(Lang.Ru, COMPONENT, ru); | ||
|
||
export default i18n.keyset(COMPONENT); |
7 changes: 4 additions & 3 deletions
7
src/components/Table/hoc/withTableSettings/TableColumnSetup/i18n/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import {i18n, I18N} from '../../../../../../i18n'; | ||
import {i18n} from '../../../../../../i18n'; | ||
import {Lang} from '../../../../../utils/configure'; | ||
import en from './en.json'; | ||
import ru from './ru.json'; | ||
|
||
const COMPONENT = 'TableColumnSetup'; | ||
|
||
i18n.registerKeyset(I18N.LANGS.en, COMPONENT, en); | ||
i18n.registerKeyset(I18N.LANGS.ru, COMPONENT, ru); | ||
i18n.registerKeyset(Lang.En, COMPONENT, en); | ||
i18n.registerKeyset(Lang.Ru, COMPONENT, ru); | ||
|
||
export default i18n.keyset(COMPONENT); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import {i18n, I18N} from '../../../i18n'; | ||
import {i18n} from '../../../i18n'; | ||
import {Lang} from '../../utils/configure'; | ||
import en from './en.json'; | ||
import ru from './ru.json'; | ||
|
||
const COMPONENT = 'Table'; | ||
|
||
i18n.registerKeyset(I18N.LANGS.en, COMPONENT, en); | ||
i18n.registerKeyset(I18N.LANGS.ru, COMPONENT, ru); | ||
i18n.registerKeyset(Lang.En, COMPONENT, en); | ||
i18n.registerKeyset(Lang.Ru, COMPONENT, ru); | ||
|
||
export default i18n.keyset(COMPONENT); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export enum Lang { | ||
Ru = 'ru', | ||
En = 'en', | ||
} | ||
|
||
interface Config { | ||
lang?: Lang; | ||
} | ||
|
||
type Subscriber = (config: Config) => void; | ||
|
||
let subs: Subscriber[] = []; | ||
|
||
const config: Config = {}; | ||
|
||
export const configure = (newConfig: 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,18 @@ | ||
import {I18N} from '@yandex-cloud/i18n'; | ||
|
||
export {I18N} from '@yandex-cloud/i18n'; | ||
import {getConfig, subscribeConfigure, Lang} from './components/utils/configure'; | ||
|
||
export const i18n = new I18N(); | ||
|
||
// @TODO remove in major version | ||
if (getConfig().lang) { | ||
i18n.setLang(getConfig().lang as Lang); | ||
} | ||
|
||
// @TODO uncomment in major version | ||
// i18n.setLang(getConfig().lang || Lang.En); | ||
|
||
subscribeConfigure((config) => { | ||
if (config.lang) { | ||
i18n.setLang(config.lang); | ||
} | ||
}); |