@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 CHANGED
@@ -9,6 +9,8 @@ program.parse(
9
9
  .concat([
10
10
  'extract',
11
11
  'src/**/*.js',
12
+ 'src/**/*.ts',
13
+ 'src/**/*.tsx',
12
14
  'node_modules/@jetshop/ui/**/*.js',
13
15
  'node_modules/@jetshop/core/**/*.js',
14
16
  '../ui/**/*.js',
@@ -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 = __IN_SERVER__ ? formatMessage.namespace() : formatMessage;
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 = (locale, translations) => {
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 = (locale, translations) => {
77
- if (!__IN_SERVER__) {
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
@@ -0,0 +1,2 @@
1
+ declare let __IN_SERVER__: boolean;
2
+ declare module '*';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jetshop/intl",
3
- "version": "5.14.0-alpha.49df897e",
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",