-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
106 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module.exports = { | ||
extends: ['@gravity-ui/eslint-config'], | ||
root: true, | ||
rules: { | ||
'no-param-reassign': 'off', | ||
}, | ||
overrides: [ | ||
{ | ||
files: ['!src/**/*', '!demo/**/*'], | ||
env: { | ||
node: true, | ||
}, | ||
}, | ||
], | ||
}; |
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,4 @@ | ||
module.exports = { | ||
extends: ['@gravity-ui/eslint-config/prettier', './.eslintrc.js'], | ||
root: true, | ||
}; |
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,44 @@ | ||
import i18n, {TFunction} from 'i18next'; | ||
import {initReactI18next} from 'react-i18next'; | ||
|
||
import en from '../i18n/en.json'; | ||
import ru from '../i18n/ru.json'; | ||
import {Lang} from '../models'; | ||
|
||
export type Loc = Record<string, typeof en>; | ||
|
||
export interface I18NConfig { | ||
lang?: string; | ||
loc?: Loc; | ||
} | ||
|
||
let initializePromise: Promise<TFunction> | null = null; | ||
|
||
export const configureI18N = ({lang, loc}: I18NConfig) => { | ||
if (initializePromise === null) { | ||
lang = lang || Lang.En; | ||
loc = loc || {ru, en}; | ||
|
||
initializePromise = i18n.use(initReactI18next).init({ | ||
lng: lang, | ||
fallbackLng: lang, | ||
ns: Object.keys(loc[lang as Lang]), | ||
resources: loc, | ||
interpolation: { | ||
escapeValue: false, | ||
}, | ||
}); | ||
} else { | ||
if (lang && lang !== i18n.language) { | ||
i18n.changeLanguage(lang); | ||
} | ||
|
||
if (loc) { | ||
for (const [lng, namespaces] of Object.entries(loc)) { | ||
for (const [ns, resources] of Object.entries(namespaces as Loc[Lang])) { | ||
i18n.addResources(lng, ns, resources); | ||
} | ||
} | ||
} | ||
} | ||
}; |
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,34 @@ | ||
import {Config, Lang} from '../models'; | ||
|
||
import {configureI18N} from './i18n'; | ||
|
||
type Subscriber = (config: Config) => void; | ||
|
||
const subs: Set<Subscriber> = new Set(); | ||
|
||
let config: Config = { | ||
lang: Lang.En, | ||
}; | ||
|
||
export const configure = (newConfig: Partial<Config> = {}) => { | ||
config = Object.assign({}, config, newConfig); | ||
|
||
configureI18N({ | ||
lang: config.lang, | ||
loc: config.loc, | ||
}); | ||
|
||
subs.forEach((sub) => { | ||
sub(config); | ||
}); | ||
}; | ||
|
||
export const subscribeConfigure = (sub: Subscriber) => { | ||
subs.add(sub); | ||
|
||
return () => { | ||
subs.delete(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,2 +1,3 @@ | ||
export * from './useForkRef'; | ||
export * from './usePopper'; | ||
export * from './useTranslation'; |
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,8 @@ | ||
import {useTranslation as useTranslationI18N} from 'react-i18next'; | ||
|
||
import {configure} from '../config'; | ||
|
||
export function useTranslation(...args: Parameters<typeof useTranslationI18N>) { | ||
configure(); | ||
return useTranslationI18N(...args); | ||
} |