@jetshop/intl 5.14.0-alpha.49df897e → 5.14.1
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/bin/extract.js +2 -0
- package/{config.js → config.ts} +38 -10
- package/global.d.ts +2 -0
- package/package.json +6 -1
package/bin/extract.js
CHANGED
package/{config.js → config.ts}
RENAMED
|
@@ -1,8 +1,26 @@
|
|
|
1
1
|
import formatMessage from 'format-message';
|
|
2
2
|
import underscoredCrc32 from 'format-message-generate-id/underscored_crc32';
|
|
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';
|
|
3
21
|
|
|
4
22
|
// Cache translation files
|
|
5
|
-
const cachedTranslationPromises = {};
|
|
23
|
+
const cachedTranslationPromises: { [key: string]: Promise<any> } = {};
|
|
6
24
|
|
|
7
25
|
const createTranslator = ({
|
|
8
26
|
locale,
|
|
@@ -10,13 +28,15 @@ const createTranslator = ({
|
|
|
10
28
|
messages,
|
|
11
29
|
fallbackMessages,
|
|
12
30
|
options
|
|
13
|
-
}) => {
|
|
14
|
-
const translator =
|
|
31
|
+
}: createTranslatorProps) => {
|
|
32
|
+
const translator = isServer ? formatMessage.namespace() : formatMessage;
|
|
15
33
|
|
|
34
|
+
// We need to create a shallow copy for messages and fallbackMessages,
|
|
35
|
+
// so B2B doesn't fail due to non-extensible type error
|
|
16
36
|
translator.setup({
|
|
17
37
|
translations: {
|
|
18
|
-
[locale]: messages,
|
|
19
|
-
[fallback]: fallbackMessages
|
|
38
|
+
[locale]: { ...messages },
|
|
39
|
+
[fallback]: { ...fallbackMessages }
|
|
20
40
|
},
|
|
21
41
|
locale: [locale, fallback],
|
|
22
42
|
generateId: underscoredCrc32,
|
|
@@ -32,7 +52,10 @@ const createTranslator = ({
|
|
|
32
52
|
* @param {object} translations
|
|
33
53
|
* @return {string} Example: `sv`
|
|
34
54
|
*/
|
|
35
|
-
export const getLocaleName = (
|
|
55
|
+
export const getLocaleName = (
|
|
56
|
+
locale: string,
|
|
57
|
+
translations: IntlOptions['translations']
|
|
58
|
+
) => {
|
|
36
59
|
if (!locale) {
|
|
37
60
|
return null;
|
|
38
61
|
}
|
|
@@ -58,7 +81,7 @@ export const getLocaleWithFallback = ({
|
|
|
58
81
|
locale,
|
|
59
82
|
defaultLocale,
|
|
60
83
|
translations
|
|
61
|
-
}) => {
|
|
84
|
+
}: Partial<setupIntlProps>) => {
|
|
62
85
|
const fallback = getLocaleName(defaultLocale, translations);
|
|
63
86
|
const loc = getLocaleName(locale, translations);
|
|
64
87
|
|
|
@@ -73,8 +96,13 @@ export const getLocaleWithFallback = ({
|
|
|
73
96
|
};
|
|
74
97
|
};
|
|
75
98
|
|
|
76
|
-
export const loadLocaleMessages = (
|
|
77
|
-
|
|
99
|
+
export const loadLocaleMessages = (
|
|
100
|
+
locale: string,
|
|
101
|
+
translations: IntlOptions['translations']
|
|
102
|
+
):
|
|
103
|
+
| Promise<any>
|
|
104
|
+
| { locale: string; translator: typeof formatMessage; fallback: string } => {
|
|
105
|
+
if (!isServer) {
|
|
78
106
|
return translations[locale]();
|
|
79
107
|
}
|
|
80
108
|
if (!cachedTranslationPromises[locale]) {
|
|
@@ -84,7 +112,7 @@ export const loadLocaleMessages = (locale, translations) => {
|
|
|
84
112
|
return cachedTranslationPromises[locale];
|
|
85
113
|
};
|
|
86
114
|
|
|
87
|
-
export const setupIntl = async config => {
|
|
115
|
+
export const setupIntl = async (config: setupIntlProps) => {
|
|
88
116
|
if (!config.translations) {
|
|
89
117
|
throw new Error('No translation data provided to translation setup.');
|
|
90
118
|
}
|
package/global.d.ts
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jetshop/intl",
|
|
3
|
-
"version": "5.14.
|
|
3
|
+
"version": "5.14.1",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"bin": {
|
|
@@ -20,6 +20,11 @@
|
|
|
20
20
|
"git add"
|
|
21
21
|
]
|
|
22
22
|
},
|
|
23
|
+
"jest": {
|
|
24
|
+
"globals": {
|
|
25
|
+
"__IN_SERVER__": true
|
|
26
|
+
}
|
|
27
|
+
},
|
|
23
28
|
"dependencies": {
|
|
24
29
|
"@typescript-eslint/eslint-plugin": "^4.31.0",
|
|
25
30
|
"@typescript-eslint/parser": "^4.31.0",
|