@jetshop/intl 5.17.0-alpha.f7018eb5 → 6.0.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/config.ts +12 -40
- package/index.ts +2 -3
- package/package.json +6 -6
package/config.ts
CHANGED
|
@@ -1,26 +1,8 @@
|
|
|
1
1
|
import formatMessage from 'format-message';
|
|
2
|
-
import
|
|
3
|
-
import type { IntlOptions } from '@jetshop/core/components/ConfigProvider';
|
|
4
|
-
|
|
5
|
-
interface setupIntlProps extends IntlOptions {
|
|
6
|
-
locale: string;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
interface createTranslatorProps {
|
|
10
|
-
locale: string;
|
|
11
|
-
fallback: string;
|
|
12
|
-
messages: any;
|
|
13
|
-
fallbackMessages: any;
|
|
14
|
-
options: any;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const isServer =
|
|
18
|
-
typeof __IN_SERVER__ !== 'undefined'
|
|
19
|
-
? __IN_SERVER__
|
|
20
|
-
: typeof window === 'undefined';
|
|
2
|
+
import underscoredCrc32 from 'format-message-generate-id/underscored_crc32';
|
|
21
3
|
|
|
22
4
|
// Cache translation files
|
|
23
|
-
const cachedTranslationPromises
|
|
5
|
+
const cachedTranslationPromises = {};
|
|
24
6
|
|
|
25
7
|
const createTranslator = ({
|
|
26
8
|
locale,
|
|
@@ -28,18 +10,16 @@ const createTranslator = ({
|
|
|
28
10
|
messages,
|
|
29
11
|
fallbackMessages,
|
|
30
12
|
options
|
|
31
|
-
}
|
|
32
|
-
const translator =
|
|
13
|
+
}) => {
|
|
14
|
+
const translator = __IN_SERVER__ ? formatMessage.namespace() : formatMessage;
|
|
33
15
|
|
|
34
|
-
// We need to create a shallow copy for messages and fallbackMessages,
|
|
35
|
-
// so B2B doesn't fail due to non-extensible type error
|
|
36
16
|
translator.setup({
|
|
37
17
|
translations: {
|
|
38
|
-
[locale]:
|
|
39
|
-
[fallback]:
|
|
18
|
+
[locale]: messages,
|
|
19
|
+
[fallback]: fallbackMessages
|
|
40
20
|
},
|
|
41
21
|
locale: [locale, fallback],
|
|
42
|
-
generateId:
|
|
22
|
+
generateId: underscoredCrc32,
|
|
43
23
|
...options
|
|
44
24
|
});
|
|
45
25
|
|
|
@@ -52,10 +32,7 @@ const createTranslator = ({
|
|
|
52
32
|
* @param {object} translations
|
|
53
33
|
* @return {string} Example: `sv`
|
|
54
34
|
*/
|
|
55
|
-
export const getLocaleName = (
|
|
56
|
-
locale: string,
|
|
57
|
-
translations: IntlOptions['translations']
|
|
58
|
-
) => {
|
|
35
|
+
export const getLocaleName = (locale, translations) => {
|
|
59
36
|
if (!locale) {
|
|
60
37
|
return null;
|
|
61
38
|
}
|
|
@@ -81,7 +58,7 @@ export const getLocaleWithFallback = ({
|
|
|
81
58
|
locale,
|
|
82
59
|
defaultLocale,
|
|
83
60
|
translations
|
|
84
|
-
}
|
|
61
|
+
}) => {
|
|
85
62
|
const fallback = getLocaleName(defaultLocale, translations);
|
|
86
63
|
const loc = getLocaleName(locale, translations);
|
|
87
64
|
|
|
@@ -96,13 +73,8 @@ export const getLocaleWithFallback = ({
|
|
|
96
73
|
};
|
|
97
74
|
};
|
|
98
75
|
|
|
99
|
-
export const loadLocaleMessages = (
|
|
100
|
-
|
|
101
|
-
translations: IntlOptions['translations']
|
|
102
|
-
):
|
|
103
|
-
| Promise<any>
|
|
104
|
-
| { locale: string; translator: typeof formatMessage; fallback: string } => {
|
|
105
|
-
if (!isServer) {
|
|
76
|
+
export const loadLocaleMessages = (locale, translations) => {
|
|
77
|
+
if (!__IN_SERVER__) {
|
|
106
78
|
return translations[locale]();
|
|
107
79
|
}
|
|
108
80
|
if (!cachedTranslationPromises[locale]) {
|
|
@@ -112,7 +84,7 @@ export const loadLocaleMessages = (
|
|
|
112
84
|
return cachedTranslationPromises[locale];
|
|
113
85
|
};
|
|
114
86
|
|
|
115
|
-
export const setupIntl = async (config
|
|
87
|
+
export const setupIntl = async (config) => {
|
|
116
88
|
if (!config.translations) {
|
|
117
89
|
throw new Error('No translation data provided to translation setup.');
|
|
118
90
|
}
|
package/index.ts
CHANGED
|
@@ -48,9 +48,8 @@ interface TranslateType extends WithContextFormatMessage {
|
|
|
48
48
|
const withContextFormatMessage =
|
|
49
49
|
(callback: (t: FormatMsgType) => ReturnType<FormatMsgTypes>) =>
|
|
50
50
|
(...args: Parameters<FormatMsgTypes>) =>
|
|
51
|
-
React.createElement(IntlContext.Consumer, null, (t: FormatMsgType) =>
|
|
52
|
-
callback(t)(...args)
|
|
53
|
-
);
|
|
51
|
+
React.createElement(IntlContext.Consumer, null, ((t: FormatMsgType) =>
|
|
52
|
+
callback(t)(...args)) as unknown as React.ReactNode);
|
|
54
53
|
|
|
55
54
|
const Translate = withContextFormatMessage((t) => t) as TranslateType;
|
|
56
55
|
Translate.rich = withContextFormatMessage((t) => t.rich);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jetshop/intl",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"bin": {
|
|
@@ -26,13 +26,13 @@
|
|
|
26
26
|
}
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
30
|
-
"@typescript-eslint/parser": "^5.
|
|
31
|
-
"babel-plugin-transform-format-message": "^6.2.
|
|
29
|
+
"@typescript-eslint/eslint-plugin": "^5.59.2",
|
|
30
|
+
"@typescript-eslint/parser": "^5.59.2",
|
|
31
|
+
"babel-plugin-transform-format-message": "^6.2.4",
|
|
32
32
|
"cross-spawn": "^6.0.5",
|
|
33
33
|
"eslint-plugin-format-message": "^6.2.3",
|
|
34
|
-
"format-message": "^6.2.
|
|
35
|
-
"format-message-cli": "^6.2.
|
|
34
|
+
"format-message": "^6.2.4",
|
|
35
|
+
"format-message-cli": "^6.2.4"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"babel-core": "^6.26.0",
|