@jetshop/intl 5.15.0-alpha.705c3635 → 5.15.0-alpha.8d4d704d

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.d.ts ADDED
@@ -0,0 +1,27 @@
1
+ import formatMessage from 'format-message';
2
+ import type { IntlOptions } from '@jetshop/core/components/ConfigProvider';
3
+ interface setupIntlProps extends IntlOptions {
4
+ locale: string;
5
+ }
6
+ /**
7
+ * Takes a locale, e.g. 'sv-SE' and resolves it to the corresponding key in the translations
8
+ * @param {string} locale Example: `sv-SE`
9
+ * @param {object} translations
10
+ * @return {string} Example: `sv`
11
+ */
12
+ export declare const getLocaleName: (locale: string, translations: IntlOptions['translations']) => string;
13
+ export declare const getLocaleWithFallback: ({ locale, defaultLocale, translations }: Partial<setupIntlProps>) => {
14
+ locale: string;
15
+ fallback: string;
16
+ };
17
+ export declare const loadLocaleMessages: (locale: string, translations: IntlOptions['translations']) => Promise<any> | {
18
+ locale: string;
19
+ translator: typeof formatMessage;
20
+ fallback: string;
21
+ };
22
+ export declare const setupIntl: (config: setupIntlProps) => Promise<{
23
+ locale: string;
24
+ translator: typeof formatMessage;
25
+ fallback: string;
26
+ }>;
27
+ export {};
package/config.js ADDED
@@ -0,0 +1,97 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import formatMessage from 'format-message';
11
+ import underscoredCrc32 from 'format-message-generate-id/underscored_crc32';
12
+ const isServer = typeof __IN_SERVER__ !== 'undefined'
13
+ ? __IN_SERVER__
14
+ : typeof window === 'undefined';
15
+ // Cache translation files
16
+ const cachedTranslationPromises = {};
17
+ const createTranslator = ({ locale, fallback, messages, fallbackMessages, options }) => {
18
+ const translator = isServer ? formatMessage.namespace() : formatMessage;
19
+ // We need to create a shallow copy for messages and fallbackMessages,
20
+ // so B2B doesn't fail due to non-extensible type error
21
+ translator.setup(Object.assign({ translations: {
22
+ [locale]: Object.assign({}, messages),
23
+ [fallback]: Object.assign({}, fallbackMessages)
24
+ }, locale: [locale, fallback], generateId: underscoredCrc32 }, options));
25
+ return translator;
26
+ };
27
+ /**
28
+ * Takes a locale, e.g. 'sv-SE' and resolves it to the corresponding key in the translations
29
+ * @param {string} locale Example: `sv-SE`
30
+ * @param {object} translations
31
+ * @return {string} Example: `sv`
32
+ */
33
+ export const getLocaleName = (locale, translations) => {
34
+ if (!locale) {
35
+ return null;
36
+ }
37
+ if (locale in translations) {
38
+ return locale;
39
+ }
40
+ const lowerCase = locale.toLowerCase();
41
+ if (lowerCase in translations) {
42
+ return lowerCase;
43
+ }
44
+ const baseLocale = lowerCase.split(/[_-]/)[0];
45
+ if (baseLocale in translations) {
46
+ return baseLocale;
47
+ }
48
+ return null;
49
+ };
50
+ export const getLocaleWithFallback = ({ locale, defaultLocale, translations }) => {
51
+ const fallback = getLocaleName(defaultLocale, translations);
52
+ const loc = getLocaleName(locale, translations);
53
+ if (!fallback) {
54
+ throw new Error(`Default locale "${defaultLocale}" could not be found in translations`);
55
+ }
56
+ return {
57
+ locale: loc || fallback,
58
+ fallback
59
+ };
60
+ };
61
+ export const loadLocaleMessages = (locale, translations) => {
62
+ if (!isServer) {
63
+ return translations[locale]();
64
+ }
65
+ if (!cachedTranslationPromises[locale]) {
66
+ cachedTranslationPromises[locale] = translations[locale]();
67
+ }
68
+ return cachedTranslationPromises[locale];
69
+ };
70
+ export const setupIntl = (config) => __awaiter(void 0, void 0, void 0, function* () {
71
+ if (!config.translations) {
72
+ throw new Error('No translation data provided to translation setup.');
73
+ }
74
+ if (!config.defaultLocale) {
75
+ throw new Error('No default locale provided to translation setup.');
76
+ }
77
+ const translations = config.translations;
78
+ const options = config.options || {};
79
+ const { locale, fallback } = getLocaleWithFallback({
80
+ locale: config.locale,
81
+ defaultLocale: config.defaultLocale,
82
+ translations
83
+ });
84
+ const [messages, fallbackMessages] = yield Promise.all([
85
+ loadLocaleMessages(locale, translations),
86
+ loadLocaleMessages(fallback, translations)
87
+ ]);
88
+ const translator = createTranslator({
89
+ locale,
90
+ fallback,
91
+ messages,
92
+ fallbackMessages,
93
+ options
94
+ });
95
+ return { locale, translator, fallback };
96
+ });
97
+ //# sourceMappingURL=config.js.map
package/config.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["config.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,aAAa,MAAM,gBAAgB,CAAC;AAC3C,OAAO,gBAAgB,MAAM,8CAA8C,CAAC;AAe5E,MAAM,QAAQ,GACZ,OAAO,aAAa,KAAK,WAAW;IAClC,CAAC,CAAC,aAAa;IACf,CAAC,CAAC,OAAO,MAAM,KAAK,WAAW,CAAC;AAEpC,0BAA0B;AAC1B,MAAM,yBAAyB,GAAoC,EAAE,CAAC;AAEtE,MAAM,gBAAgB,GAAG,CAAC,EACxB,MAAM,EACN,QAAQ,EACR,QAAQ,EACR,gBAAgB,EAChB,OAAO,EACe,EAAE,EAAE;IAC1B,MAAM,UAAU,GAAG,QAAQ,CAAC,CAAC,CAAC,aAAa,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC;IAExE,sEAAsE;IACtE,uDAAuD;IACvD,UAAU,CAAC,KAAK,iBACd,YAAY,EAAE;YACZ,CAAC,MAAM,CAAC,oBAAO,QAAQ,CAAE;YACzB,CAAC,QAAQ,CAAC,oBAAO,gBAAgB,CAAE;SACpC,EACD,MAAM,EAAE,CAAC,MAAM,EAAE,QAAQ,CAAC,EAC1B,UAAU,EAA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tC,yBAAyB,CAAC,MAAM,CAAC,GAAG,YAAY,CAAC,MAAM,CAAC,EAAE,CAAC;KAC5D;IAED,OAAO,yBAAyB,CAAC,MAAM,CAAC,CAAC;AAC3C,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,SAAS,GAAG,CAAO,MAAsB,EAAE,EAAE;IACxD,IAAI,CAAC,MAAM,CAAC,YAAY,EAAE;QACxB,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;KACvE;IAED,IAAI,CAAC,MAAM,CAAC,aAAa,EAAE;QACzB,MAAM,IAAI,KAAK,CAAC,kDAAkD,CAAC,CAAC;KACrE;IAED,MAAM,YAAY,GAAG,MAAM,CAAC,YAAY,CAAC;IACzC,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,IAAI,EAAE,CAAC;IAErC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,qBAAqB,CAAC;QACjD,MAAM,EAAE,MAAM,CAAC,MAAM;QACrB,aAAa,EAAE,MAAM,CAAC,aAAa;QACnC,YAAY;KACb,CAAC,CAAC;IAEH,MAAM,CAAC,QAAQ,EAAE,gBAAgB,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;QACrD,kBAAkB,CAAC,MAAM,EAAE,YAAY,CAAC;QACxC,kBAAkB,CAAC,QAAQ,EAAE,YAAY,CAAC;KAC3C,CAAC,CAAC;IAEH,MAAM,UAAU,GAAG,gBAAgB,CAAC;QAClC,MAAM;QACN,QAAQ;QACR,QAAQ;QACR,gBAAgB;QAChB,OAAO;KACR,CAAC,CAAC;IAEH,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC;AAC1C,CAAC,CAAA,CAAC"}
package/context.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ /// <reference types="react" />
2
+ import formatMessage from 'format-message';
3
+ declare const IntlContext: import("react").Context<typeof formatMessage>;
4
+ export default IntlContext;
package/context.js ADDED
@@ -0,0 +1,5 @@
1
+ import { createContext } from 'react';
2
+ import formatMessage from 'format-message';
3
+ const IntlContext = createContext(formatMessage);
4
+ export default IntlContext;
5
+ //# sourceMappingURL=context.js.map
package/context.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.js","sourceRoot":"","sources":["context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AACtC,OAAO,aAAa,MAAM,gBAAgB,CAAC;AAE3C,MAAM,WAAW,GAAG,aAAa,CAAC,aAAa,CAAC,CAAC;AACjD,eAAe,WAAW,CAAC"}
package/index.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ import React from 'react';
2
+ import formatMessage from 'format-message/types';
3
+ export declare const useIntl: () => typeof formatMessage;
4
+ declare type FormatMsgType = typeof formatMessage;
5
+ declare type FormatMsgTypes = FormatMsgType | FormatMsgType['rich'] | FormatMsgType['number'] | FormatMsgType['date'] | FormatMsgType['time'] | FormatMsgType['select'] | FormatMsgType['plural'] | FormatMsgType['selectordinal'] | FormatMsgType['custom'];
6
+ declare type WithContextFormatMessage = ReturnType<typeof withContextFormatMessage>;
7
+ interface TranslateType extends WithContextFormatMessage {
8
+ rich: (...args: Parameters<FormatMsgType['rich']>) => ReturnType<WithContextFormatMessage>;
9
+ number: (...args: Parameters<FormatMsgType['number']>) => ReturnType<WithContextFormatMessage>;
10
+ date: (...args: Parameters<FormatMsgType['date']>) => ReturnType<WithContextFormatMessage>;
11
+ time: (...args: Parameters<FormatMsgType['time']>) => ReturnType<WithContextFormatMessage>;
12
+ select: (...args: Parameters<FormatMsgType['select']>) => ReturnType<WithContextFormatMessage>;
13
+ plural: (...args: Parameters<FormatMsgType['plural']>) => ReturnType<WithContextFormatMessage>;
14
+ selectordinal: (...args: Parameters<FormatMsgType['selectordinal']>) => ReturnType<WithContextFormatMessage>;
15
+ custom: (...args: Parameters<FormatMsgType['custom']>) => ReturnType<WithContextFormatMessage>;
16
+ }
17
+ declare const withContextFormatMessage: (callback: (t: FormatMsgType) => ReturnType<FormatMsgTypes>) => (...args: Parameters<FormatMsgTypes>) => React.FunctionComponentElement<React.ConsumerProps<typeof formatMessage>>;
18
+ declare const Translate: TranslateType;
19
+ export declare const Intl: React.Consumer<typeof formatMessage>;
20
+ export default Translate;
package/index.js ADDED
@@ -0,0 +1,16 @@
1
+ import React, { useContext } from 'react';
2
+ import IntlContext from '@jetshop/intl/context';
3
+ export const useIntl = () => useContext(IntlContext);
4
+ const withContextFormatMessage = (callback) => (...args) => React.createElement(IntlContext.Consumer, null, (t) => callback(t)(...args));
5
+ const Translate = withContextFormatMessage((t) => t);
6
+ Translate.rich = withContextFormatMessage((t) => t.rich);
7
+ Translate.number = withContextFormatMessage((t) => t.number);
8
+ Translate.date = withContextFormatMessage((t) => t.date);
9
+ Translate.time = withContextFormatMessage((t) => t.time);
10
+ Translate.select = withContextFormatMessage((t) => t.select);
11
+ Translate.plural = withContextFormatMessage((t) => t.plural);
12
+ Translate.selectordinal = withContextFormatMessage((t) => t.selectordinal);
13
+ Translate.custom = withContextFormatMessage((t) => t.custom);
14
+ export const Intl = IntlContext.Consumer;
15
+ export default Translate;
16
+ //# sourceMappingURL=index.js.map
package/index.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAC1C,OAAO,WAAW,MAAM,uBAAuB,CAAC;AAGhD,MAAM,CAAC,MAAM,OAAO,GAAG,GAAG,EAAE,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC;AA2CrD,MAAM,wBAAwB,GAC5B,CAAC,QAA0D,EAAE,EAAE,CAC/D,CAAC,GAAG,IAAgC,EAAE,EAAE,CACtC,KAAK,CAAC,aAAa,CAAC,WAAW,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAgB,EAAE,EAAE,CACnE,QAAQ,CAAC,CAAC,CAAC,CAAC,GAA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,10 @@
8
8
  "intl": "./bin/intl.js"
9
9
  },
10
10
  "scripts": {
11
+ "build": "tsc --noEmit false",
11
12
  "test": "jest",
12
- "precommit": "lint-staged"
13
+ "precommit": "lint-staged",
14
+ "preversion": "yarn build"
13
15
  },
14
16
  "lint-staged": {
15
17
  "*.{js,jsx,ts,tsx,json}": [