From c02bb5a6771df6034ba00ddc0b4ed70238f87d9b Mon Sep 17 00:00:00 2001 From: Angelina Gadzhieva Date: Thu, 1 Feb 2024 16:36:37 +0300 Subject: [PATCH] feat: remove local configure, use uikit/i18n (#177) --- .storybook/decorators/withLang.tsx | 14 +++++---- .storybook/preview.tsx | 4 --- src/components/AllPagesPanel/i18n/index.ts | 5 ++-- src/components/AsideHeader/i18n/index.ts | 5 ++-- src/components/MobileHeader/i18n/index.ts | 5 ++-- src/components/Settings/i18n/index.ts | 5 ++-- src/components/Title/i18n/index.ts | 5 ++-- src/components/index.ts | 2 -- src/components/utils/configure.ts | 33 ---------------------- src/components/utils/registerKeyset.ts | 11 -------- src/i18n.ts | 11 -------- 11 files changed, 24 insertions(+), 76 deletions(-) delete mode 100644 src/components/utils/configure.ts delete mode 100644 src/components/utils/registerKeyset.ts delete mode 100644 src/i18n.ts diff --git a/.storybook/decorators/withLang.tsx b/.storybook/decorators/withLang.tsx index d97d115..5cf16b8 100644 --- a/.storybook/decorators/withLang.tsx +++ b/.storybook/decorators/withLang.tsx @@ -1,13 +1,17 @@ import React from 'react'; import type {Decorator} from '@storybook/react'; -import {Lang, configure} from '../../src'; +import {Lang, configure} from '@gravity-ui/uikit'; export const withLang: Decorator = (Story, context) => { const lang = context.globals.lang; + const [key, forceRender] = React.useState(0); - configure({ - lang: lang as Lang, - }); + React.useEffect(() => { + configure({ + lang: lang as Lang, + }); - return ; + forceRender((c) => c + 1); + }, [lang]); + return ; }; diff --git a/.storybook/preview.tsx b/.storybook/preview.tsx index 95a5f0b..dadf55f 100644 --- a/.storybook/preview.tsx +++ b/.storybook/preview.tsx @@ -5,13 +5,9 @@ import React from 'react'; import type {Decorator, Preview} from '@storybook/react'; import {ThemeProvider, MobileProvider, Lang, configure as uiKitConfigure} from '@gravity-ui/uikit'; import {configure as componentsConfigure} from '@gravity-ui/components'; -import {configure} from '../src'; import {withMobile} from './decorators/withMobile'; import {withLang} from './decorators/withLang'; -configure({ - lang: Lang.En, -}); uiKitConfigure({ lang: Lang.En, }); diff --git a/src/components/AllPagesPanel/i18n/index.ts b/src/components/AllPagesPanel/i18n/index.ts index 060b5d2..bc11d93 100644 --- a/src/components/AllPagesPanel/i18n/index.ts +++ b/src/components/AllPagesPanel/i18n/index.ts @@ -1,7 +1,8 @@ -import {registerKeyset} from '../../utils/registerKeyset'; +import {addComponentKeysets} from '@gravity-ui/uikit/i18n'; +import {NAMESPACE} from '../../utils/cn'; import en from './en.json'; import ru from './ru.json'; const COMPONENT = 'AllPagesPanel'; -export default registerKeyset({en, ru}, COMPONENT); +export default addComponentKeysets({en, ru}, `${NAMESPACE}${COMPONENT}`); diff --git a/src/components/AsideHeader/i18n/index.ts b/src/components/AsideHeader/i18n/index.ts index 92d1330..0f10afc 100644 --- a/src/components/AsideHeader/i18n/index.ts +++ b/src/components/AsideHeader/i18n/index.ts @@ -1,7 +1,8 @@ -import {registerKeyset} from '../../utils/registerKeyset'; +import {addComponentKeysets} from '@gravity-ui/uikit/i18n'; +import {NAMESPACE} from '../../utils/cn'; import en from './en.json'; import ru from './ru.json'; const COMPONENT = 'AsideHeader'; -export default registerKeyset({en, ru}, COMPONENT); +export default addComponentKeysets({en, ru}, `${NAMESPACE}${COMPONENT}`); diff --git a/src/components/MobileHeader/i18n/index.ts b/src/components/MobileHeader/i18n/index.ts index fee0a0a..07bedef 100644 --- a/src/components/MobileHeader/i18n/index.ts +++ b/src/components/MobileHeader/i18n/index.ts @@ -1,7 +1,8 @@ -import {registerKeyset} from '../../utils/registerKeyset'; +import {addComponentKeysets} from '@gravity-ui/uikit/i18n'; +import {NAMESPACE} from '../../utils/cn'; import en from './en.json'; import ru from './ru.json'; const COMPONENT = 'MobileHeader'; -export default registerKeyset({en, ru}, COMPONENT); +export default addComponentKeysets({en, ru}, `${NAMESPACE}${COMPONENT}`); diff --git a/src/components/Settings/i18n/index.ts b/src/components/Settings/i18n/index.ts index af9cc85..8872aee 100644 --- a/src/components/Settings/i18n/index.ts +++ b/src/components/Settings/i18n/index.ts @@ -1,7 +1,8 @@ -import {registerKeyset} from '../../utils/registerKeyset'; +import {addComponentKeysets} from '@gravity-ui/uikit/i18n'; +import {NAMESPACE} from '../../utils/cn'; import en from './en.json'; import ru from './ru.json'; const COMPONENT = 'Settings'; -export default registerKeyset({en, ru}, COMPONENT); +export default addComponentKeysets({en, ru}, `${NAMESPACE}${COMPONENT}`); diff --git a/src/components/Title/i18n/index.ts b/src/components/Title/i18n/index.ts index a09e572..0c5d929 100644 --- a/src/components/Title/i18n/index.ts +++ b/src/components/Title/i18n/index.ts @@ -1,7 +1,8 @@ -import {registerKeyset} from '../../utils/registerKeyset'; +import {addComponentKeysets} from '@gravity-ui/uikit/i18n'; +import {NAMESPACE} from '../../utils/cn'; import en from './en.json'; import ru from './ru.json'; const COMPONENT = 'Title'; -export default registerKeyset({en, ru}, COMPONENT); +export default addComponentKeysets({en, ru}, `${NAMESPACE}${COMPONENT}`); diff --git a/src/components/index.ts b/src/components/index.ts index 9da241b..db57f1b 100644 --- a/src/components/index.ts +++ b/src/components/index.ts @@ -12,5 +12,3 @@ export * from './HotkeysPanel'; export * from './Settings'; export * from './MobileHeader'; export * from './types'; - -export {Lang, configure} from './utils/configure'; diff --git a/src/components/utils/configure.ts b/src/components/utils/configure.ts deleted file mode 100644 index 89f4f2c..0000000 --- a/src/components/utils/configure.ts +++ /dev/null @@ -1,33 +0,0 @@ -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) => { - 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; diff --git a/src/components/utils/registerKeyset.ts b/src/components/utils/registerKeyset.ts deleted file mode 100644 index b5d97a6..0000000 --- a/src/components/utils/registerKeyset.ts +++ /dev/null @@ -1,11 +0,0 @@ -import {i18n} from '../../i18n'; - -import {Lang} from './configure'; - -type KeysData = Parameters[2]; - -export function registerKeyset(data: Record, keysetName: string) { - Object.entries(data).forEach(([lang, keys]) => i18n.registerKeyset(lang, keysetName, keys)); - - return i18n.keyset(keysetName); -} diff --git a/src/i18n.ts b/src/i18n.ts deleted file mode 100644 index 90f2b77..0000000 --- a/src/i18n.ts +++ /dev/null @@ -1,11 +0,0 @@ -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); -});