Skip to content

Commit

Permalink
feat: Add configure util
Browse files Browse the repository at this point in the history
  • Loading branch information
3y3 committed Sep 7, 2023
1 parent 12d440a commit a8039e6
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 4 deletions.
4 changes: 0 additions & 4 deletions .eslintrc

This file was deleted.

15 changes: 15 additions & 0 deletions .eslintrc.js
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,
},
},
],
};
4 changes: 4 additions & 0 deletions .eslintrc.publish.js
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,
};
44 changes: 44 additions & 0 deletions src/config/i18n.ts
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);
}
}
}
}
};
34 changes: 34 additions & 0 deletions src/config/index.ts
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;
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './useForkRef';
export * from './usePopper';
export * from './useTranslation';
8 changes: 8 additions & 0 deletions src/hooks/useTranslation.ts
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);
}

0 comments on commit a8039e6

Please sign in to comment.