@snack-uikit/locale 0.13.0 → 0.13.1-preview-9354ceb8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -10
- package/dist/cjs/components/LocaleProvider/LocaleProvider.d.ts +34 -8
- package/dist/cjs/components/LocaleProvider/LocaleProvider.js +76 -35
- package/dist/cjs/components/LocaleProvider/Sample.d.ts +10 -0
- package/dist/cjs/components/LocaleProvider/Sample.js +117 -0
- package/dist/cjs/components/LocaleProvider/index.d.ts +1 -2
- package/dist/cjs/components/LocaleProvider/index.js +8 -3
- package/dist/cjs/locales/index.d.ts +2 -2
- package/dist/cjs/locales/index.js +2 -2
- package/dist/cjs/typeUtils.d.ts +0 -25
- package/dist/cjs/types.d.ts +10 -6
- package/dist/esm/components/LocaleProvider/LocaleProvider.d.ts +34 -8
- package/dist/esm/components/LocaleProvider/LocaleProvider.js +61 -24
- package/dist/esm/components/LocaleProvider/Sample.d.ts +10 -0
- package/dist/esm/components/LocaleProvider/Sample.js +68 -0
- package/dist/esm/components/LocaleProvider/index.d.ts +1 -2
- package/dist/esm/components/LocaleProvider/index.js +1 -2
- package/dist/esm/locales/index.d.ts +2 -2
- package/dist/esm/locales/index.js +2 -2
- package/dist/esm/typeUtils.d.ts +0 -25
- package/dist/esm/types.d.ts +10 -6
- package/package.json +2 -2
- package/src/components/LocaleProvider/LocaleProvider.tsx +111 -32
- package/src/components/LocaleProvider/Sample.tsx +101 -0
- package/src/components/LocaleProvider/index.ts +1 -3
- package/src/locales/en_GB.ts +3 -1
- package/src/locales/index.ts +2 -2
- package/src/typeUtils.ts +0 -49
- package/src/types.ts +17 -9
- package/dist/cjs/components/LocaleProvider/hooks.d.ts +0 -16
- package/dist/cjs/components/LocaleProvider/hooks.js +0 -38
- package/dist/esm/components/LocaleProvider/hooks.d.ts +0 -16
- package/dist/esm/components/LocaleProvider/hooks.js +0 -30
- package/src/components/LocaleProvider/hooks.ts +0 -62
package/README.md
CHANGED
|
@@ -49,20 +49,20 @@ const lang = 'en_GB'; // or 'ru_RU' or 'custom_LANG'
|
|
|
49
49
|
|
|
50
50
|
[//]: DOCUMENTATION_SECTION_START
|
|
51
51
|
[//]: THIS_SECTION_IS_AUTOGENERATED_PLEASE_DONT_EDIT_IT
|
|
52
|
-
|
|
53
52
|
## LocaleProvider
|
|
54
|
-
|
|
55
53
|
### Props
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
|
59
|
-
| lang\* | `string` | - | |
|
|
60
|
-
| locales | `PartialObjectDeep<Record<string, { Table: { searchPlaceholder: string; noData: { title: string; }; noResults: { title: string; description: string; }; errorData: { title: string; description: string; }; rowsOptionsLabel: string; export: string; groupSelectButton: { ...; }; }; ... 7 more ...; ToastUpload: { ...; }; }>>` | - | |
|
|
61
|
-
|
|
54
|
+
| name | type | default value | description |
|
|
55
|
+
|------|------|---------------|-------------|
|
|
56
|
+
| lang* | "en-GB" \| "ru-RU" | - | |
|
|
62
57
|
## useLocale
|
|
63
|
-
|
|
64
|
-
`helper`
|
|
58
|
+
`helper`
|
|
65
59
|
|
|
66
60
|
Inner hook to use translations
|
|
61
|
+
## createLocaleContext
|
|
62
|
+
### Props
|
|
63
|
+
| name | type | default value | description |
|
|
64
|
+
|------|------|---------------|-------------|
|
|
65
|
+
| extendedDictionary | `ExtendedDictionary<D>` | - | |
|
|
66
|
+
|
|
67
67
|
|
|
68
68
|
[//]: DOCUMENTATION_SECTION_END
|
|
@@ -1,15 +1,41 @@
|
|
|
1
1
|
import { ReactNode } from 'react';
|
|
2
|
-
import { LocaleDictionary, LocaleLang
|
|
3
|
-
export declare const DEFAULT_LANG = "
|
|
4
|
-
export type LocaleContextType = {
|
|
2
|
+
import { Dictionary, DottedTranslationKey, ExtendedDictionary, LocaleDictionary, LocaleLang } from '../../types';
|
|
3
|
+
export declare const DEFAULT_LANG = "en-GB";
|
|
4
|
+
export type LocaleContextType<D extends Dictionary> = {
|
|
5
5
|
lang: LocaleLang;
|
|
6
|
-
|
|
7
|
-
localesByLang: LocaleDictionary;
|
|
6
|
+
localesByLang: LocaleDictionary<D>;
|
|
8
7
|
};
|
|
9
|
-
export declare const LocaleContext: import("react").Context<LocaleContextType>;
|
|
10
8
|
export type LocaleProviderProps = {
|
|
11
9
|
lang: LocaleLang;
|
|
12
|
-
locales?: OverrideLocales;
|
|
13
10
|
children: ReactNode;
|
|
14
11
|
};
|
|
15
|
-
export
|
|
12
|
+
export type ContextOptions<D extends Dictionary> = {
|
|
13
|
+
extendedDictionary?: ExtendedDictionary<D>;
|
|
14
|
+
};
|
|
15
|
+
type LocaleComponentName<D extends Dictionary> = keyof LocaleDictionary<D>;
|
|
16
|
+
type GetLocaleText<D extends Dictionary, T extends keyof LocaleDictionary<D> | undefined = undefined> = (key: DottedTranslationKey<D, T>) => string;
|
|
17
|
+
export declare function createLocaleContext<D extends Dictionary>({ extendedDictionary }: ContextOptions<D>): {
|
|
18
|
+
LocaleContext: import("react").Context<LocaleContextType<D>>;
|
|
19
|
+
LocaleProvider: ({ lang, children }: LocaleProviderProps) => import("react/jsx-runtime").JSX.Element;
|
|
20
|
+
useLocale: {
|
|
21
|
+
(): {
|
|
22
|
+
t: GetLocaleText<D>;
|
|
23
|
+
lang: LocaleLang;
|
|
24
|
+
};
|
|
25
|
+
<C extends LocaleComponentName<D> = "Table" | "List" | "Chips" | "Fields" | "SearchPrivate" | "ColorPicker" | "Calendar" | "Toolbar" | "ToastUpload" | keyof D>(componentName: C): {
|
|
26
|
+
t: GetLocaleText<D, C>;
|
|
27
|
+
lang: LocaleLang;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
export declare const LocaleProvider: ({ lang, children }: LocaleProviderProps) => import("react/jsx-runtime").JSX.Element, useLocale: {
|
|
32
|
+
(): {
|
|
33
|
+
t: GetLocaleText<Dictionary>;
|
|
34
|
+
lang: LocaleLang;
|
|
35
|
+
};
|
|
36
|
+
<C extends string = string>(componentName: C): {
|
|
37
|
+
t: GetLocaleText<Dictionary, C>;
|
|
38
|
+
lang: LocaleLang;
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
export {};
|
|
@@ -5,48 +5,89 @@ var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
|
|
|
5
5
|
"default": mod
|
|
6
6
|
};
|
|
7
7
|
};
|
|
8
|
+
var _a;
|
|
8
9
|
Object.defineProperty(exports, "__esModule", {
|
|
9
10
|
value: true
|
|
10
11
|
});
|
|
11
|
-
exports.
|
|
12
|
-
exports.
|
|
12
|
+
exports.useLocale = exports.LocaleProvider = exports.DEFAULT_LANG = void 0;
|
|
13
|
+
exports.createLocaleContext = createLocaleContext;
|
|
13
14
|
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
14
15
|
const lodash_merge_1 = __importDefault(require("lodash.merge"));
|
|
15
16
|
const react_1 = require("react");
|
|
16
17
|
const locales_1 = require("../../locales");
|
|
17
|
-
exports.DEFAULT_LANG = '
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
});
|
|
23
|
-
function LocaleProvider(_ref) {
|
|
18
|
+
exports.DEFAULT_LANG = 'en-GB';
|
|
19
|
+
function mergeLocaleWithExtension(extension) {
|
|
20
|
+
return (0, lodash_merge_1.default)({}, locales_1.LOCALES, extension);
|
|
21
|
+
}
|
|
22
|
+
function createLocaleContext(_ref) {
|
|
24
23
|
let {
|
|
25
|
-
|
|
26
|
-
locales: localesProp,
|
|
27
|
-
children
|
|
24
|
+
extendedDictionary
|
|
28
25
|
} = _ref;
|
|
29
|
-
const locales = (
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
return locales_1.LOCALES;
|
|
34
|
-
}, [localesProp]);
|
|
35
|
-
const lang = (0, react_1.useMemo)(() => langProp.replace('-', '_'), [langProp]);
|
|
36
|
-
const localesByLang = (0, react_1.useMemo)(() => {
|
|
37
|
-
let localesObj = locales[lang];
|
|
38
|
-
if (!localesObj) {
|
|
39
|
-
console.warn(`Snack-uikit: localization for lang ${lang} was not found. Make sure you are using correct lang or passed proper locales to LocaleProvider. For now default language (${exports.DEFAULT_LANG}) will be used`);
|
|
40
|
-
localesObj = locales[exports.DEFAULT_LANG];
|
|
41
|
-
}
|
|
42
|
-
return localesObj;
|
|
43
|
-
}, [lang, locales]);
|
|
44
|
-
return (0, jsx_runtime_1.jsx)(exports.LocaleContext.Provider, {
|
|
45
|
-
value: {
|
|
46
|
-
lang,
|
|
47
|
-
locales,
|
|
48
|
-
localesByLang
|
|
49
|
-
},
|
|
50
|
-
children: children
|
|
26
|
+
const locales = mergeLocaleWithExtension(extendedDictionary);
|
|
27
|
+
const LocaleContext = (0, react_1.createContext)({
|
|
28
|
+
lang: exports.DEFAULT_LANG,
|
|
29
|
+
localesByLang: locales['en-GB']
|
|
51
30
|
});
|
|
52
|
-
|
|
31
|
+
function LocaleProvider(_ref2) {
|
|
32
|
+
let {
|
|
33
|
+
lang,
|
|
34
|
+
children
|
|
35
|
+
} = _ref2;
|
|
36
|
+
const localesByLang = (0, react_1.useMemo)(() => {
|
|
37
|
+
let localesObj = locales[lang];
|
|
38
|
+
if (!localesObj) {
|
|
39
|
+
console.warn(`Snack-uikit: localization for lang ${lang} was not found. Make sure you are using correct lang or passed proper locales to LocaleProvider. For now default language (${exports.DEFAULT_LANG}) will be used`);
|
|
40
|
+
localesObj = locales[exports.DEFAULT_LANG];
|
|
41
|
+
}
|
|
42
|
+
return localesObj;
|
|
43
|
+
}, [lang]);
|
|
44
|
+
return (0, jsx_runtime_1.jsx)(LocaleContext.Provider, {
|
|
45
|
+
value: {
|
|
46
|
+
lang,
|
|
47
|
+
localesByLang
|
|
48
|
+
},
|
|
49
|
+
children: children
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
function useLocale(componentName) {
|
|
53
|
+
const {
|
|
54
|
+
localesByLang,
|
|
55
|
+
lang
|
|
56
|
+
} = (0, react_1.useContext)(LocaleContext);
|
|
57
|
+
const locales = (0, react_1.useMemo)(() => {
|
|
58
|
+
if (!componentName) {
|
|
59
|
+
return localesByLang;
|
|
60
|
+
}
|
|
61
|
+
return localesByLang[componentName] || {};
|
|
62
|
+
}, [componentName, localesByLang]);
|
|
63
|
+
const getLocaleText = (0, react_1.useCallback)(key => {
|
|
64
|
+
let translation = '';
|
|
65
|
+
const complexKey = key.split('.');
|
|
66
|
+
if (complexKey.length === 1) {
|
|
67
|
+
translation = locales[key];
|
|
68
|
+
} else {
|
|
69
|
+
translation = complexKey.reduce((acc, cur) => {
|
|
70
|
+
if (typeof acc === 'string') {
|
|
71
|
+
return acc;
|
|
72
|
+
}
|
|
73
|
+
return acc[cur];
|
|
74
|
+
}, locales);
|
|
75
|
+
}
|
|
76
|
+
if (!(translation === null || translation === void 0 ? void 0 : translation.length)) {
|
|
77
|
+
console.warn(`Snack-uikit: the '${key}' key is not found in the current locale '${lang}'.`);
|
|
78
|
+
return key;
|
|
79
|
+
}
|
|
80
|
+
return translation;
|
|
81
|
+
}, [lang, locales]);
|
|
82
|
+
return {
|
|
83
|
+
t: getLocaleText,
|
|
84
|
+
lang
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
LocaleContext,
|
|
89
|
+
LocaleProvider,
|
|
90
|
+
useLocale
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
_a = createLocaleContext({}), exports.LocaleProvider = _a.LocaleProvider, exports.useLocale = _a.useLocale;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
type SampleWrapperProps = {
|
|
3
|
+
children: ReactNode;
|
|
4
|
+
};
|
|
5
|
+
export declare function SampleWrapper({ children }: SampleWrapperProps): import("react/jsx-runtime").JSX.Element;
|
|
6
|
+
export declare function OldDictionaryItemSample(): import("react/jsx-runtime").JSX.Element;
|
|
7
|
+
export declare function NewDictionarySample(): import("react/jsx-runtime").JSX.Element;
|
|
8
|
+
export declare function NewDictSimpleTranslationComponent(): import("react/jsx-runtime").JSX.Element;
|
|
9
|
+
export declare function NewDictDeepTranslationComponent(): import("react/jsx-runtime").JSX.Element;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.SampleWrapper = SampleWrapper;
|
|
7
|
+
exports.OldDictionaryItemSample = OldDictionaryItemSample;
|
|
8
|
+
exports.NewDictionarySample = NewDictionarySample;
|
|
9
|
+
exports.NewDictSimpleTranslationComponent = NewDictSimpleTranslationComponent;
|
|
10
|
+
exports.NewDictDeepTranslationComponent = NewDictDeepTranslationComponent;
|
|
11
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
12
|
+
const LocaleProvider_1 = require("./LocaleProvider");
|
|
13
|
+
const sampleExtensionEn = {
|
|
14
|
+
Table: {
|
|
15
|
+
searchPlaceholder: 'SearchOverriden'
|
|
16
|
+
},
|
|
17
|
+
ButtonPredefined: {
|
|
18
|
+
create: 'Create',
|
|
19
|
+
cancel: 'Cancel',
|
|
20
|
+
refetch: 'Refetch'
|
|
21
|
+
},
|
|
22
|
+
Site: {
|
|
23
|
+
Section: {
|
|
24
|
+
Basic: {
|
|
25
|
+
ShowMore: 'Show More'
|
|
26
|
+
},
|
|
27
|
+
PersonalManager: {
|
|
28
|
+
title: 'More than regular support',
|
|
29
|
+
subtitle: 'Full support for solving your problems and achieving business results'
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const sampleExtensionRu = {
|
|
35
|
+
Table: {
|
|
36
|
+
searchPlaceholder: 'SearchOverriden'
|
|
37
|
+
},
|
|
38
|
+
ButtonPredefined: {
|
|
39
|
+
create: 'Создать',
|
|
40
|
+
cancel: 'Отмена',
|
|
41
|
+
refetch: 'Обновить'
|
|
42
|
+
},
|
|
43
|
+
Site: {
|
|
44
|
+
Section: {
|
|
45
|
+
Basic: {
|
|
46
|
+
ShowMore: 'Показать еще'
|
|
47
|
+
},
|
|
48
|
+
PersonalManager: {
|
|
49
|
+
title: 'Больше чем просто поддержка',
|
|
50
|
+
subtitle: 'Полное сопровождение для решения ваших задач и понятный бизнес-результат'
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const extension = {
|
|
56
|
+
'en-GB': sampleExtensionEn,
|
|
57
|
+
'ru-RU': sampleExtensionRu
|
|
58
|
+
};
|
|
59
|
+
const {
|
|
60
|
+
LocaleProvider,
|
|
61
|
+
useLocale
|
|
62
|
+
} = (0, LocaleProvider_1.createLocaleContext)({
|
|
63
|
+
extendedDictionary: extension
|
|
64
|
+
});
|
|
65
|
+
function SampleWrapper(_ref) {
|
|
66
|
+
let {
|
|
67
|
+
children
|
|
68
|
+
} = _ref;
|
|
69
|
+
return (0, jsx_runtime_1.jsx)(LocaleProvider, {
|
|
70
|
+
lang: 'ru-RU',
|
|
71
|
+
children: children
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
function OldDictionaryItemSample() {
|
|
75
|
+
const {
|
|
76
|
+
t
|
|
77
|
+
} = useLocale('Table');
|
|
78
|
+
return (0, jsx_runtime_1.jsxs)("div", {
|
|
79
|
+
children: [(0, jsx_runtime_1.jsx)("div", {
|
|
80
|
+
children: t('searchPlaceholder')
|
|
81
|
+
}), (0, jsx_runtime_1.jsx)("div", {
|
|
82
|
+
children: t('noResults.title')
|
|
83
|
+
})]
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
function NewDictionarySample() {
|
|
87
|
+
const {
|
|
88
|
+
t
|
|
89
|
+
} = useLocale();
|
|
90
|
+
return (0, jsx_runtime_1.jsxs)("div", {
|
|
91
|
+
children: [(0, jsx_runtime_1.jsx)("div", {
|
|
92
|
+
children: t('Table.searchPlaceholder')
|
|
93
|
+
}), (0, jsx_runtime_1.jsx)("div", {
|
|
94
|
+
children: t('Table.noResults.title')
|
|
95
|
+
}), (0, jsx_runtime_1.jsx)("div", {
|
|
96
|
+
children: t('ButtonPredefined.create')
|
|
97
|
+
}), (0, jsx_runtime_1.jsx)("div", {
|
|
98
|
+
children: t('Site.Section.PersonalManager.title')
|
|
99
|
+
})]
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
function NewDictSimpleTranslationComponent() {
|
|
103
|
+
const {
|
|
104
|
+
t
|
|
105
|
+
} = useLocale('ButtonPredefined');
|
|
106
|
+
return (0, jsx_runtime_1.jsx)("div", {
|
|
107
|
+
children: t('create')
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
function NewDictDeepTranslationComponent() {
|
|
111
|
+
const {
|
|
112
|
+
t
|
|
113
|
+
} = useLocale('Site');
|
|
114
|
+
return (0, jsx_runtime_1.jsx)("div", {
|
|
115
|
+
children: t('Section.PersonalManager.title')
|
|
116
|
+
});
|
|
117
|
+
}
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export { type LocaleProviderProps, LocaleProvider } from './LocaleProvider';
|
|
2
|
-
export { useLocale } from './hooks';
|
|
1
|
+
export { type LocaleProviderProps, LocaleProvider, useLocale, createLocaleContext } from './LocaleProvider';
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.useLocale = exports.LocaleProvider = void 0;
|
|
6
|
+
exports.createLocaleContext = exports.useLocale = exports.LocaleProvider = void 0;
|
|
7
7
|
var LocaleProvider_1 = require("./LocaleProvider");
|
|
8
8
|
Object.defineProperty(exports, "LocaleProvider", {
|
|
9
9
|
enumerable: true,
|
|
@@ -11,10 +11,15 @@ Object.defineProperty(exports, "LocaleProvider", {
|
|
|
11
11
|
return LocaleProvider_1.LocaleProvider;
|
|
12
12
|
}
|
|
13
13
|
});
|
|
14
|
-
var hooks_1 = require("./hooks");
|
|
15
14
|
Object.defineProperty(exports, "useLocale", {
|
|
16
15
|
enumerable: true,
|
|
17
16
|
get: function () {
|
|
18
|
-
return
|
|
17
|
+
return LocaleProvider_1.useLocale;
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
Object.defineProperty(exports, "createLocaleContext", {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
get: function () {
|
|
23
|
+
return LocaleProvider_1.createLocaleContext;
|
|
19
24
|
}
|
|
20
25
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export declare const LOCALES: {
|
|
2
|
-
readonly
|
|
2
|
+
readonly 'en-GB': {
|
|
3
3
|
Table: {
|
|
4
4
|
searchPlaceholder: string;
|
|
5
5
|
noData: {
|
|
@@ -76,7 +76,7 @@ export declare const LOCALES: {
|
|
|
76
76
|
};
|
|
77
77
|
};
|
|
78
78
|
};
|
|
79
|
-
readonly
|
|
79
|
+
readonly 'ru-RU': {
|
|
80
80
|
Table: {
|
|
81
81
|
searchPlaceholder: string;
|
|
82
82
|
noData: {
|
package/dist/cjs/typeUtils.d.ts
CHANGED
|
@@ -1,28 +1,3 @@
|
|
|
1
|
-
type Primitive = null | undefined | string | number | boolean | symbol | bigint;
|
|
2
|
-
type BuiltIns = Primitive | void | Date | RegExp;
|
|
3
|
-
export type PartialDeep<T> = T extends BuiltIns | ((...arguments_: unknown[]) => unknown) | (new (...arguments_: unknown[]) => unknown) ? T : T extends Map<infer KeyType, infer ValueType> ? PartialMapDeep<KeyType, ValueType> : T extends Set<infer ItemType> ? PartialSetDeep<ItemType> : T extends ReadonlyMap<infer KeyType, infer ValueType> ? PartialReadonlyMapDeep<KeyType, ValueType> : T extends ReadonlySet<infer ItemType> ? PartialReadonlySetDeep<ItemType> : T extends object ? PartialObjectDeep<T> : unknown;
|
|
4
|
-
/**
|
|
5
|
-
Same as `PartialDeep`, but accepts only `Map`s and as inputs. Internal helper for `PartialDeep`.
|
|
6
|
-
*/
|
|
7
|
-
type PartialMapDeep<KeyType, ValueType> = unknown & Map<PartialDeep<KeyType>, PartialDeep<ValueType>>;
|
|
8
|
-
/**
|
|
9
|
-
Same as `PartialDeep`, but accepts only `Set`s as inputs. Internal helper for `PartialDeep`.
|
|
10
|
-
*/
|
|
11
|
-
type PartialSetDeep<T> = unknown & Set<PartialDeep<T>>;
|
|
12
|
-
/**
|
|
13
|
-
Same as `PartialDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `PartialDeep`.
|
|
14
|
-
*/
|
|
15
|
-
type PartialReadonlyMapDeep<KeyType, ValueType> = unknown & ReadonlyMap<PartialDeep<KeyType>, PartialDeep<ValueType>>;
|
|
16
|
-
/**
|
|
17
|
-
Same as `PartialDeep`, but accepts only `ReadonlySet`s as inputs. Internal helper for `PartialDeep`.
|
|
18
|
-
*/
|
|
19
|
-
type PartialReadonlySetDeep<T> = unknown & ReadonlySet<PartialDeep<T>>;
|
|
20
|
-
/**
|
|
21
|
-
Same as `PartialDeep`, but accepts only `object`s as inputs. Internal helper for `PartialDeep`.
|
|
22
|
-
*/
|
|
23
|
-
type PartialObjectDeep<ObjectType extends object> = {
|
|
24
|
-
[KeyType in keyof ObjectType]?: PartialDeep<ObjectType[KeyType]>;
|
|
25
|
-
};
|
|
26
1
|
/**
|
|
27
2
|
* https://stackoverflow.com/a/73179989
|
|
28
3
|
*/
|
package/dist/cjs/types.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { LOCALES } from './locales';
|
|
2
|
-
import {
|
|
3
|
-
export type
|
|
4
|
-
export type
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export type
|
|
2
|
+
import { PathsToProps } from './typeUtils';
|
|
3
|
+
export type LocaleLang = keyof typeof LOCALES;
|
|
4
|
+
export type TranslatedEntity = {
|
|
5
|
+
[key: string]: string | TranslatedEntity;
|
|
6
|
+
};
|
|
7
|
+
export type Dictionary = Record<string, TranslatedEntity>;
|
|
8
|
+
export type LocaleDictionary<D extends Dictionary> = (typeof LOCALES)['en-GB'] & D;
|
|
9
|
+
export type Locales<D extends Dictionary> = Record<LocaleLang, LocaleDictionary<D>>;
|
|
10
|
+
export type DottedTranslationKey<D extends Dictionary, C extends keyof LocaleDictionary<D> | undefined = undefined> = C extends keyof LocaleDictionary<D> ? PathsToProps<LocaleDictionary<D>[C], string> : PathsToProps<LocaleDictionary<D>, string>;
|
|
11
|
+
export type ExtendedDictionary<D> = Record<LocaleLang, D>;
|
|
@@ -1,15 +1,41 @@
|
|
|
1
1
|
import { ReactNode } from 'react';
|
|
2
|
-
import { LocaleDictionary, LocaleLang
|
|
3
|
-
export declare const DEFAULT_LANG = "
|
|
4
|
-
export type LocaleContextType = {
|
|
2
|
+
import { Dictionary, DottedTranslationKey, ExtendedDictionary, LocaleDictionary, LocaleLang } from '../../types';
|
|
3
|
+
export declare const DEFAULT_LANG = "en-GB";
|
|
4
|
+
export type LocaleContextType<D extends Dictionary> = {
|
|
5
5
|
lang: LocaleLang;
|
|
6
|
-
|
|
7
|
-
localesByLang: LocaleDictionary;
|
|
6
|
+
localesByLang: LocaleDictionary<D>;
|
|
8
7
|
};
|
|
9
|
-
export declare const LocaleContext: import("react").Context<LocaleContextType>;
|
|
10
8
|
export type LocaleProviderProps = {
|
|
11
9
|
lang: LocaleLang;
|
|
12
|
-
locales?: OverrideLocales;
|
|
13
10
|
children: ReactNode;
|
|
14
11
|
};
|
|
15
|
-
export
|
|
12
|
+
export type ContextOptions<D extends Dictionary> = {
|
|
13
|
+
extendedDictionary?: ExtendedDictionary<D>;
|
|
14
|
+
};
|
|
15
|
+
type LocaleComponentName<D extends Dictionary> = keyof LocaleDictionary<D>;
|
|
16
|
+
type GetLocaleText<D extends Dictionary, T extends keyof LocaleDictionary<D> | undefined = undefined> = (key: DottedTranslationKey<D, T>) => string;
|
|
17
|
+
export declare function createLocaleContext<D extends Dictionary>({ extendedDictionary }: ContextOptions<D>): {
|
|
18
|
+
LocaleContext: import("react").Context<LocaleContextType<D>>;
|
|
19
|
+
LocaleProvider: ({ lang, children }: LocaleProviderProps) => import("react/jsx-runtime").JSX.Element;
|
|
20
|
+
useLocale: {
|
|
21
|
+
(): {
|
|
22
|
+
t: GetLocaleText<D>;
|
|
23
|
+
lang: LocaleLang;
|
|
24
|
+
};
|
|
25
|
+
<C extends LocaleComponentName<D> = "Table" | "List" | "Chips" | "Fields" | "SearchPrivate" | "ColorPicker" | "Calendar" | "Toolbar" | "ToastUpload" | keyof D>(componentName: C): {
|
|
26
|
+
t: GetLocaleText<D, C>;
|
|
27
|
+
lang: LocaleLang;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
export declare const LocaleProvider: ({ lang, children }: LocaleProviderProps) => import("react/jsx-runtime").JSX.Element, useLocale: {
|
|
32
|
+
(): {
|
|
33
|
+
t: GetLocaleText<Dictionary>;
|
|
34
|
+
lang: LocaleLang;
|
|
35
|
+
};
|
|
36
|
+
<C extends string = string>(componentName: C): {
|
|
37
|
+
t: GetLocaleText<Dictionary, C>;
|
|
38
|
+
lang: LocaleLang;
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
export {};
|
|
@@ -1,28 +1,65 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import merge from 'lodash.merge';
|
|
3
|
-
import { createContext, useMemo } from 'react';
|
|
3
|
+
import { createContext, useCallback, useContext, useMemo } from 'react';
|
|
4
4
|
import { LOCALES } from '../../locales';
|
|
5
|
-
export const DEFAULT_LANG = '
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
locales: LOCALES,
|
|
9
|
-
localesByLang: LOCALES.en_GB,
|
|
10
|
-
});
|
|
11
|
-
export function LocaleProvider({ lang: langProp, locales: localesProp, children }) {
|
|
12
|
-
const locales = useMemo(() => {
|
|
13
|
-
if (localesProp) {
|
|
14
|
-
return merge({}, LOCALES, localesProp);
|
|
15
|
-
}
|
|
16
|
-
return LOCALES;
|
|
17
|
-
}, [localesProp]);
|
|
18
|
-
const lang = useMemo(() => langProp.replace('-', '_'), [langProp]);
|
|
19
|
-
const localesByLang = useMemo(() => {
|
|
20
|
-
let localesObj = locales[lang];
|
|
21
|
-
if (!localesObj) {
|
|
22
|
-
console.warn(`Snack-uikit: localization for lang ${lang} was not found. Make sure you are using correct lang or passed proper locales to LocaleProvider. For now default language (${DEFAULT_LANG}) will be used`);
|
|
23
|
-
localesObj = locales[DEFAULT_LANG];
|
|
24
|
-
}
|
|
25
|
-
return localesObj;
|
|
26
|
-
}, [lang, locales]);
|
|
27
|
-
return _jsx(LocaleContext.Provider, { value: { lang, locales, localesByLang }, children: children });
|
|
5
|
+
export const DEFAULT_LANG = 'en-GB';
|
|
6
|
+
function mergeLocaleWithExtension(extension) {
|
|
7
|
+
return merge({}, LOCALES, extension);
|
|
28
8
|
}
|
|
9
|
+
export function createLocaleContext({ extendedDictionary }) {
|
|
10
|
+
const locales = mergeLocaleWithExtension(extendedDictionary);
|
|
11
|
+
const LocaleContext = createContext({
|
|
12
|
+
lang: DEFAULT_LANG,
|
|
13
|
+
localesByLang: locales['en-GB'],
|
|
14
|
+
});
|
|
15
|
+
function LocaleProvider({ lang, children }) {
|
|
16
|
+
const localesByLang = useMemo(() => {
|
|
17
|
+
let localesObj = locales[lang];
|
|
18
|
+
if (!localesObj) {
|
|
19
|
+
console.warn(`Snack-uikit: localization for lang ${lang} was not found. Make sure you are using correct lang or passed proper locales to LocaleProvider. For now default language (${DEFAULT_LANG}) will be used`);
|
|
20
|
+
localesObj = locales[DEFAULT_LANG];
|
|
21
|
+
}
|
|
22
|
+
return localesObj;
|
|
23
|
+
}, [lang]);
|
|
24
|
+
return _jsx(LocaleContext.Provider, { value: { lang, localesByLang }, children: children });
|
|
25
|
+
}
|
|
26
|
+
function useLocale(componentName) {
|
|
27
|
+
const { localesByLang, lang } = useContext(LocaleContext);
|
|
28
|
+
const locales = useMemo(() => {
|
|
29
|
+
if (!componentName) {
|
|
30
|
+
return localesByLang;
|
|
31
|
+
}
|
|
32
|
+
return (localesByLang[componentName] || {});
|
|
33
|
+
}, [componentName, localesByLang]);
|
|
34
|
+
const getLocaleText = useCallback(key => {
|
|
35
|
+
let translation = '';
|
|
36
|
+
const complexKey = key.split('.');
|
|
37
|
+
if (complexKey.length === 1) {
|
|
38
|
+
translation = locales[key];
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
translation = complexKey.reduce((acc, cur) => {
|
|
42
|
+
if (typeof acc === 'string') {
|
|
43
|
+
return acc;
|
|
44
|
+
}
|
|
45
|
+
return acc[cur];
|
|
46
|
+
}, locales);
|
|
47
|
+
}
|
|
48
|
+
if (!(translation === null || translation === void 0 ? void 0 : translation.length)) {
|
|
49
|
+
console.warn(`Snack-uikit: the '${key}' key is not found in the current locale '${lang}'.`);
|
|
50
|
+
return key;
|
|
51
|
+
}
|
|
52
|
+
return translation;
|
|
53
|
+
}, [lang, locales]);
|
|
54
|
+
return {
|
|
55
|
+
t: getLocaleText,
|
|
56
|
+
lang,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
LocaleContext,
|
|
61
|
+
LocaleProvider,
|
|
62
|
+
useLocale,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
export const { LocaleProvider, useLocale } = createLocaleContext({});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
type SampleWrapperProps = {
|
|
3
|
+
children: ReactNode;
|
|
4
|
+
};
|
|
5
|
+
export declare function SampleWrapper({ children }: SampleWrapperProps): import("react/jsx-runtime").JSX.Element;
|
|
6
|
+
export declare function OldDictionaryItemSample(): import("react/jsx-runtime").JSX.Element;
|
|
7
|
+
export declare function NewDictionarySample(): import("react/jsx-runtime").JSX.Element;
|
|
8
|
+
export declare function NewDictSimpleTranslationComponent(): import("react/jsx-runtime").JSX.Element;
|
|
9
|
+
export declare function NewDictDeepTranslationComponent(): import("react/jsx-runtime").JSX.Element;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { createLocaleContext } from './LocaleProvider';
|
|
3
|
+
const sampleExtensionEn = {
|
|
4
|
+
Table: {
|
|
5
|
+
searchPlaceholder: 'SearchOverriden',
|
|
6
|
+
},
|
|
7
|
+
ButtonPredefined: {
|
|
8
|
+
create: 'Create',
|
|
9
|
+
cancel: 'Cancel',
|
|
10
|
+
refetch: 'Refetch',
|
|
11
|
+
},
|
|
12
|
+
Site: {
|
|
13
|
+
Section: {
|
|
14
|
+
Basic: {
|
|
15
|
+
ShowMore: 'Show More',
|
|
16
|
+
},
|
|
17
|
+
PersonalManager: {
|
|
18
|
+
title: 'More than regular support',
|
|
19
|
+
subtitle: 'Full support for solving your problems and achieving business results',
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
const sampleExtensionRu = {
|
|
25
|
+
Table: {
|
|
26
|
+
searchPlaceholder: 'SearchOverriden',
|
|
27
|
+
},
|
|
28
|
+
ButtonPredefined: {
|
|
29
|
+
create: 'Создать',
|
|
30
|
+
cancel: 'Отмена',
|
|
31
|
+
refetch: 'Обновить',
|
|
32
|
+
},
|
|
33
|
+
Site: {
|
|
34
|
+
Section: {
|
|
35
|
+
Basic: {
|
|
36
|
+
ShowMore: 'Показать еще',
|
|
37
|
+
},
|
|
38
|
+
PersonalManager: {
|
|
39
|
+
title: 'Больше чем просто поддержка',
|
|
40
|
+
subtitle: 'Полное сопровождение для решения ваших задач и понятный бизнес-результат',
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
const extension = {
|
|
46
|
+
'en-GB': sampleExtensionEn,
|
|
47
|
+
'ru-RU': sampleExtensionRu,
|
|
48
|
+
};
|
|
49
|
+
const { LocaleProvider, useLocale } = createLocaleContext({ extendedDictionary: extension });
|
|
50
|
+
export function SampleWrapper({ children }) {
|
|
51
|
+
return _jsx(LocaleProvider, { lang: 'ru-RU', children: children });
|
|
52
|
+
}
|
|
53
|
+
export function OldDictionaryItemSample() {
|
|
54
|
+
const { t } = useLocale('Table');
|
|
55
|
+
return (_jsxs("div", { children: [_jsx("div", { children: t('searchPlaceholder') }), _jsx("div", { children: t('noResults.title') })] }));
|
|
56
|
+
}
|
|
57
|
+
export function NewDictionarySample() {
|
|
58
|
+
const { t } = useLocale();
|
|
59
|
+
return (_jsxs("div", { children: [_jsx("div", { children: t('Table.searchPlaceholder') }), _jsx("div", { children: t('Table.noResults.title') }), _jsx("div", { children: t('ButtonPredefined.create') }), _jsx("div", { children: t('Site.Section.PersonalManager.title') })] }));
|
|
60
|
+
}
|
|
61
|
+
export function NewDictSimpleTranslationComponent() {
|
|
62
|
+
const { t } = useLocale('ButtonPredefined');
|
|
63
|
+
return _jsx("div", { children: t('create') });
|
|
64
|
+
}
|
|
65
|
+
export function NewDictDeepTranslationComponent() {
|
|
66
|
+
const { t } = useLocale('Site');
|
|
67
|
+
return _jsx("div", { children: t('Section.PersonalManager.title') });
|
|
68
|
+
}
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export { type LocaleProviderProps, LocaleProvider } from './LocaleProvider';
|
|
2
|
-
export { useLocale } from './hooks';
|
|
1
|
+
export { type LocaleProviderProps, LocaleProvider, useLocale, createLocaleContext } from './LocaleProvider';
|