use-intl 2.9.2 → 2.10.0-alpha.4

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.
Files changed (128) hide show
  1. package/CHANGELOG.md +471 -0
  2. package/dist/core/use-intl.esm.js +38 -41
  3. package/dist/core/use-intl.esm.js.map +1 -1
  4. package/dist/core/use-intl.esm2.js +41 -38
  5. package/dist/core/use-intl.esm2.js.map +1 -1
  6. package/dist/core/use-intl.esm3.js +2 -2
  7. package/dist/core/use-intl.esm4.js +12 -56
  8. package/dist/core/use-intl.esm4.js.map +1 -1
  9. package/dist/core/use-intl.esm5.js +56 -12
  10. package/dist/core/use-intl.esm5.js.map +1 -1
  11. package/dist/core/use-intl.esm6.js +1 -1
  12. package/dist/core/use-intl.esm7.js +2 -2
  13. package/dist/react/use-intl.esm.js +2 -2
  14. package/dist/react/use-intl.esm2.js +41 -17
  15. package/dist/react/use-intl.esm2.js.map +1 -1
  16. package/dist/react/use-intl.esm3.js +21 -20
  17. package/dist/react/use-intl.esm3.js.map +1 -1
  18. package/dist/react/use-intl.esm4.js +21 -4
  19. package/dist/react/use-intl.esm4.js.map +1 -1
  20. package/dist/react/use-intl.esm5.js +4 -46
  21. package/dist/react/use-intl.esm5.js.map +1 -1
  22. package/dist/react/use-intl.esm6.js +4 -4
  23. package/dist/react/use-intl.esm6.js.map +1 -1
  24. package/dist/react/use-intl.esm7.js +12 -3
  25. package/dist/react/use-intl.esm7.js.map +1 -1
  26. package/dist/react/use-intl.esm8.js +38 -9
  27. package/dist/react/use-intl.esm8.js.map +1 -1
  28. package/dist/react/use-intl.esm9.js +3 -41
  29. package/dist/react/use-intl.esm9.js.map +1 -1
  30. package/dist/src/core/AbstractIntlMessages.d.ts +9 -0
  31. package/dist/src/core/AbstractIntlMessages.js +2 -0
  32. package/dist/src/core/AbstractIntlMessages.js.map +1 -0
  33. package/dist/src/core/DateTimeFormatOptions.d.ts +72 -0
  34. package/dist/src/core/DateTimeFormatOptions.js +3 -0
  35. package/dist/src/core/DateTimeFormatOptions.js.map +1 -0
  36. package/dist/src/core/Formats.d.ts +7 -0
  37. package/dist/src/core/Formats.js +2 -0
  38. package/dist/src/core/Formats.js.map +1 -0
  39. package/dist/src/core/IntlConfiguration.d.ts +46 -0
  40. package/dist/src/core/IntlConfiguration.js +2 -0
  41. package/dist/src/core/IntlConfiguration.js.map +1 -0
  42. package/dist/src/core/IntlError.d.ts +13 -0
  43. package/dist/src/core/IntlError.js +23 -0
  44. package/dist/src/core/IntlError.js.map +1 -0
  45. package/dist/src/core/IntlMessages.d.ts +2 -0
  46. package/dist/src/core/IntlMessages.js +4 -0
  47. package/dist/src/core/IntlMessages.js.map +1 -0
  48. package/dist/src/core/NumberFormatOptions.d.ts +2 -0
  49. package/dist/src/core/NumberFormatOptions.js +2 -0
  50. package/dist/src/core/NumberFormatOptions.js.map +1 -0
  51. package/dist/src/core/TranslationValues.d.ts +5 -0
  52. package/dist/src/core/TranslationValues.js +2 -0
  53. package/dist/src/core/TranslationValues.js.map +1 -0
  54. package/dist/src/core/convertFormatsToIntlMessageFormat.d.ts +10 -0
  55. package/dist/src/core/convertFormatsToIntlMessageFormat.js +31 -0
  56. package/dist/src/core/convertFormatsToIntlMessageFormat.js.map +1 -0
  57. package/dist/src/core/createBaseTranslator.d.ts +26 -0
  58. package/dist/src/core/createBaseTranslator.js +181 -0
  59. package/dist/src/core/createBaseTranslator.js.map +1 -0
  60. package/dist/src/core/createIntl.d.ts +17 -0
  61. package/dist/src/core/createIntl.js +126 -0
  62. package/dist/src/core/createIntl.js.map +1 -0
  63. package/dist/src/core/createTranslator.d.ts +48 -0
  64. package/dist/src/core/createTranslator.js +24 -0
  65. package/dist/src/core/createTranslator.js.map +1 -0
  66. package/dist/src/core/createTranslatorImpl.d.ts +14 -0
  67. package/dist/src/core/createTranslatorImpl.js +43 -0
  68. package/dist/src/core/createTranslatorImpl.js.map +1 -0
  69. package/dist/src/core/defaults.d.ts +11 -0
  70. package/dist/src/core/defaults.js +11 -0
  71. package/dist/src/core/defaults.js.map +1 -0
  72. package/dist/src/core/index.d.ts +8 -0
  73. package/dist/src/core/index.js +4 -0
  74. package/dist/src/core/index.js.map +1 -0
  75. package/dist/src/core/resolveNamespace.d.ts +5 -0
  76. package/dist/src/core/resolveNamespace.js +10 -0
  77. package/dist/src/core/resolveNamespace.js.map +1 -0
  78. package/dist/src/core/utils/MessageKeys.d.ts +5 -0
  79. package/dist/src/core/utils/MessageKeys.js +2 -0
  80. package/dist/src/core/utils/MessageKeys.js.map +1 -0
  81. package/dist/src/core/utils/NamespaceKeys.d.ts +5 -0
  82. package/dist/src/core/utils/NamespaceKeys.js +2 -0
  83. package/dist/src/core/utils/NamespaceKeys.js.map +1 -0
  84. package/dist/src/core/utils/NestedKeyOf.d.ts +4 -0
  85. package/dist/src/core/utils/NestedKeyOf.js +2 -0
  86. package/dist/src/core/utils/NestedKeyOf.js.map +1 -0
  87. package/dist/src/core/utils/NestedValueOf.d.ts +2 -0
  88. package/dist/src/core/utils/NestedValueOf.js +2 -0
  89. package/dist/src/core/utils/NestedValueOf.js.map +1 -0
  90. package/dist/src/core/validateMessages.d.ts +3 -0
  91. package/dist/src/core/validateMessages.js +22 -0
  92. package/dist/src/core/validateMessages.js.map +1 -0
  93. package/dist/src/index.d.ts +2 -0
  94. package/dist/src/index.js +3 -0
  95. package/dist/src/index.js.map +1 -0
  96. package/dist/src/react/IntlContext.d.ts +21 -0
  97. package/dist/src/react/IntlContext.js +4 -0
  98. package/dist/src/react/IntlContext.js.map +1 -0
  99. package/dist/src/react/IntlProvider.d.ts +16 -0
  100. package/dist/src/react/IntlProvider.js +16 -0
  101. package/dist/src/react/IntlProvider.js.map +1 -0
  102. package/dist/src/react/index.d.ts +6 -0
  103. package/dist/src/react/index.js +7 -0
  104. package/dist/src/react/index.js.map +1 -0
  105. package/dist/src/react/useIntl.d.ts +5 -0
  106. package/dist/src/react/useIntl.js +14 -0
  107. package/dist/src/react/useIntl.js.map +1 -0
  108. package/dist/src/react/useIntlContext.d.ts +1 -0
  109. package/dist/src/react/useIntlContext.js +12 -0
  110. package/dist/src/react/useIntlContext.js.map +1 -0
  111. package/dist/src/react/useLocale.d.ts +1 -0
  112. package/dist/src/react/useLocale.js +5 -0
  113. package/dist/src/react/useLocale.js.map +1 -0
  114. package/dist/src/react/useNow.d.ts +23 -0
  115. package/dist/src/react/useNow.js +40 -0
  116. package/dist/src/react/useNow.js.map +1 -0
  117. package/dist/src/react/useTimeZone.d.ts +1 -0
  118. package/dist/src/react/useTimeZone.js +5 -0
  119. package/dist/src/react/useTimeZone.js.map +1 -0
  120. package/dist/src/react/useTranslations.d.ts +44 -0
  121. package/dist/src/react/useTranslations.js +21 -0
  122. package/dist/src/react/useTranslations.js.map +1 -0
  123. package/dist/src/react/useTranslationsImpl.d.ts +8 -0
  124. package/dist/src/react/useTranslationsImpl.js +35 -0
  125. package/dist/src/react/useTranslationsImpl.js.map +1 -0
  126. package/dist/use-intl.esm.js +7 -7
  127. package/package.json +7 -6
  128. package/LICENSE +0 -21
@@ -0,0 +1,22 @@
1
+ import IntlError, { IntlErrorCode } from './IntlError';
2
+ function validateMessagesSegment(messages, invalidKeyLabels, parentPath) {
3
+ Object.entries(messages).forEach(([key, messageOrMessages]) => {
4
+ if (key.includes('.')) {
5
+ let keyLabel = key;
6
+ if (parentPath)
7
+ keyLabel += ` (at ${parentPath})`;
8
+ invalidKeyLabels.push(keyLabel);
9
+ }
10
+ if (messageOrMessages != null && typeof messageOrMessages === 'object') {
11
+ validateMessagesSegment(messageOrMessages, invalidKeyLabels, [parentPath, key].filter((part) => part != null).join('.'));
12
+ }
13
+ });
14
+ }
15
+ export default function validateMessages(messages, onError) {
16
+ const invalidKeyLabels = [];
17
+ validateMessagesSegment(messages, invalidKeyLabels);
18
+ if (invalidKeyLabels.length > 0) {
19
+ onError(new IntlError(IntlErrorCode.INVALID_KEY, `Namespace keys can not contain the character "." as this is used to express nesting. Please remove it or replace it with another character.\n\nInvalid ${invalidKeyLabels.length === 1 ? 'key' : 'keys'}: ${invalidKeyLabels.join(', ')}`));
20
+ }
21
+ }
22
+ //# sourceMappingURL=validateMessages.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validateMessages.js","sourceRoot":"","sources":["../../../src/core/validateMessages.tsx"],"names":[],"mappings":"AACA,OAAO,SAAS,EAAE,EAAC,aAAa,EAAC,MAAM,aAAa,CAAC;AAErD,SAAS,uBAAuB,CAC9B,QAA8B,EAC9B,gBAA+B,EAC/B,UAAmB;IAEnB,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,iBAAiB,CAAC,EAAE,EAAE;QAC5D,IAAI,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;YACrB,IAAI,QAAQ,GAAG,GAAG,CAAC;YACnB,IAAI,UAAU;gBAAE,QAAQ,IAAI,QAAQ,UAAU,GAAG,CAAC;YAClD,gBAAgB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;SACjC;QAED,IAAI,iBAAiB,IAAI,IAAI,IAAI,OAAO,iBAAiB,KAAK,QAAQ,EAAE;YACtE,uBAAuB,CACrB,iBAAiB,EACjB,gBAAgB,EAChB,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,IAAI,IAAI,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAC3D,CAAC;SACH;IACH,CAAC,CAAC,CAAC;AACL,CAAC;AAED,MAAM,CAAC,OAAO,UAAU,gBAAgB,CACtC,QAA8B,EAC9B,OAAmC;IAEnC,MAAM,gBAAgB,GAAkB,EAAE,CAAC;IAC3C,uBAAuB,CAAC,QAAQ,EAAE,gBAAgB,CAAC,CAAC;IAEpD,IAAI,gBAAgB,CAAC,MAAM,GAAG,CAAC,EAAE;QAC/B,OAAO,CACL,IAAI,SAAS,CACX,aAAa,CAAC,WAAW,EACzB,0JACE,gBAAgB,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAC1C,KAAK,gBAAgB,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CACnC,CACF,CAAC;KACH;AACH,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from './core';
2
+ export * from './react';
@@ -0,0 +1,3 @@
1
+ export * from './core';
2
+ export * from './react';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC"}
@@ -0,0 +1,21 @@
1
+ /// <reference types="react" />
2
+ import AbstractIntlMessages from '../core/AbstractIntlMessages';
3
+ import Formats from '../core/Formats';
4
+ import IntlError from '../core/IntlError';
5
+ import { RichTranslationValues } from '../core/TranslationValues';
6
+ export declare type IntlContextShape = {
7
+ messages?: AbstractIntlMessages;
8
+ locale: string;
9
+ formats?: Partial<Formats>;
10
+ timeZone?: string;
11
+ onError(error: IntlError): void;
12
+ getMessageFallback(info: {
13
+ error: IntlError;
14
+ key: string;
15
+ namespace?: string;
16
+ }): string;
17
+ now?: Date;
18
+ defaultTranslationValues?: RichTranslationValues;
19
+ };
20
+ declare const IntlContext: import("react").Context<IntlContextShape | undefined>;
21
+ export default IntlContext;
@@ -0,0 +1,4 @@
1
+ import { createContext } from 'react';
2
+ const IntlContext = createContext(undefined);
3
+ export default IntlContext;
4
+ //# sourceMappingURL=IntlContext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IntlContext.js","sourceRoot":"","sources":["../../../src/react/IntlContext.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAC,MAAM,OAAO,CAAC;AAqBpC,MAAM,WAAW,GAAG,aAAa,CAA+B,SAAS,CAAC,CAAC;AAE3E,eAAe,WAAW,CAAC"}
@@ -0,0 +1,16 @@
1
+ import { ReactNode } from 'react';
2
+ import { AbstractIntlMessages } from '../core';
3
+ import IntlConfiguration from '../core/IntlConfiguration';
4
+ import { RichTranslationValues } from '../core/TranslationValues';
5
+ declare type Props = IntlConfiguration & {
6
+ /** All components that use the provided hooks should be within this tree. */
7
+ children: ReactNode;
8
+ /** Global default values for translation values and rich text elements.
9
+ * Can be used for consistent usage or styling of rich text elements.
10
+ * Defaults will be overidden by locally provided values. */
11
+ defaultTranslationValues?: RichTranslationValues;
12
+ /** All messages that will be available in your components. */
13
+ messages?: AbstractIntlMessages;
14
+ };
15
+ export default function IntlProvider({ children, onError, getMessageFallback, messages, ...contextValues }: Props): JSX.Element;
16
+ export {};
@@ -0,0 +1,16 @@
1
+ import React, { useEffect } from 'react';
2
+ import { defaultGetMessageFallback, defaultOnError } from '../core/defaults';
3
+ import validateMessages from '../core/validateMessages';
4
+ import IntlContext from './IntlContext';
5
+ export default function IntlProvider({ children, onError = defaultOnError, getMessageFallback = defaultGetMessageFallback, messages, ...contextValues }) {
6
+ if (__DEV__) {
7
+ // eslint-disable-next-line react-hooks/rules-of-hooks
8
+ useEffect(() => {
9
+ if (messages) {
10
+ validateMessages(messages, onError);
11
+ }
12
+ }, [messages, onError]);
13
+ }
14
+ return (React.createElement(IntlContext.Provider, { value: { ...contextValues, messages, onError, getMessageFallback } }, children));
15
+ }
16
+ //# sourceMappingURL=IntlProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IntlProvider.js","sourceRoot":"","sources":["../../../src/react/IntlProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAY,SAAS,EAAC,MAAM,OAAO,CAAC;AAIlD,OAAO,EAAC,yBAAyB,EAAE,cAAc,EAAC,MAAM,kBAAkB,CAAC;AAC3E,OAAO,gBAAgB,MAAM,0BAA0B,CAAC;AACxD,OAAO,WAAW,MAAM,eAAe,CAAC;AAaxC,MAAM,CAAC,OAAO,UAAU,YAAY,CAAC,EACnC,QAAQ,EACR,OAAO,GAAG,cAAc,EACxB,kBAAkB,GAAG,yBAAyB,EAC9C,QAAQ,EACR,GAAG,aAAa,EACV;IACN,IAAI,OAAO,EAAE;QACX,sDAAsD;QACtD,SAAS,CAAC,GAAG,EAAE;YACb,IAAI,QAAQ,EAAE;gBACZ,gBAAgB,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;aACrC;QACH,CAAC,EAAE,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAC;KACzB;IAED,OAAO,CACL,oBAAC,WAAW,CAAC,QAAQ,IACnB,KAAK,EAAE,EAAC,GAAG,aAAa,EAAE,QAAQ,EAAE,OAAO,EAAE,kBAAkB,EAAC,IAE/D,QAAQ,CACY,CACxB,CAAC;AACJ,CAAC"}
@@ -0,0 +1,6 @@
1
+ export { default as IntlProvider } from './IntlProvider';
2
+ export { default as useTranslations } from './useTranslations';
3
+ export { default as useIntl } from './useIntl';
4
+ export { default as useLocale } from './useLocale';
5
+ export { default as useNow } from './useNow';
6
+ export { default as useTimeZone } from './useTimeZone';
@@ -0,0 +1,7 @@
1
+ export { default as IntlProvider } from './IntlProvider';
2
+ export { default as useTranslations } from './useTranslations';
3
+ export { default as useIntl } from './useIntl';
4
+ export { default as useLocale } from './useLocale';
5
+ export { default as useNow } from './useNow';
6
+ export { default as useTimeZone } from './useTimeZone';
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/react/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,IAAI,YAAY,EAAC,MAAM,gBAAgB,CAAC;AACvD,OAAO,EAAC,OAAO,IAAI,eAAe,EAAC,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAC,OAAO,IAAI,OAAO,EAAC,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAC,OAAO,IAAI,SAAS,EAAC,MAAM,aAAa,CAAC;AACjD,OAAO,EAAC,OAAO,IAAI,MAAM,EAAC,MAAM,UAAU,CAAC;AAC3C,OAAO,EAAC,OAAO,IAAI,WAAW,EAAC,MAAM,eAAe,CAAC"}
@@ -0,0 +1,5 @@
1
+ export default function useIntl(): {
2
+ formatDateTime: (value: number | Date, formatOrOptions?: string | import("../core/DateTimeFormatOptions").default | undefined) => string;
3
+ formatNumber: (value: number, formatOrOptions?: string | import("@formatjs/ecma402-abstract").NumberFormatOptions | undefined) => string;
4
+ formatRelativeTime: (date: number | Date, now?: number | Date | undefined) => string;
5
+ };
@@ -0,0 +1,14 @@
1
+ import { useMemo } from 'react';
2
+ import createIntl from '../core/createIntl';
3
+ import useIntlContext from './useIntlContext';
4
+ export default function useIntl() {
5
+ const { formats, locale, now: globalNow, onError, timeZone } = useIntlContext();
6
+ return useMemo(() => createIntl({
7
+ formats,
8
+ locale,
9
+ now: globalNow,
10
+ onError,
11
+ timeZone
12
+ }), [formats, globalNow, locale, onError, timeZone]);
13
+ }
14
+ //# sourceMappingURL=useIntl.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useIntl.js","sourceRoot":"","sources":["../../../src/react/useIntl.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,EAAC,MAAM,OAAO,CAAC;AAC9B,OAAO,UAAU,MAAM,oBAAoB,CAAC;AAC5C,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAE9C,MAAM,CAAC,OAAO,UAAU,OAAO;IAC7B,MAAM,EAAC,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAC,GAAG,cAAc,EAAE,CAAC;IAE9E,OAAO,OAAO,CACZ,GAAG,EAAE,CACH,UAAU,CAAC;QACT,OAAO;QACP,MAAM;QACN,GAAG,EAAE,SAAS;QACd,OAAO;QACP,QAAQ;KACT,CAAC,EACJ,CAAC,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,CAAC,CAChD,CAAC;AACJ,CAAC"}
@@ -0,0 +1 @@
1
+ export default function useIntlContext(): import("./IntlContext").IntlContextShape;
@@ -0,0 +1,12 @@
1
+ import { useContext } from 'react';
2
+ import IntlContext from './IntlContext';
3
+ export default function useIntlContext() {
4
+ const context = useContext(IntlContext);
5
+ if (!context) {
6
+ throw new Error(__DEV__
7
+ ? 'No intl context found. Have you configured the provider?'
8
+ : undefined);
9
+ }
10
+ return context;
11
+ }
12
+ //# sourceMappingURL=useIntlContext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useIntlContext.js","sourceRoot":"","sources":["../../../src/react/useIntlContext.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,OAAO,CAAC;AACjC,OAAO,WAAW,MAAM,eAAe,CAAC;AAExC,MAAM,CAAC,OAAO,UAAU,cAAc;IACpC,MAAM,OAAO,GAAG,UAAU,CAAC,WAAW,CAAC,CAAC;IAExC,IAAI,CAAC,OAAO,EAAE;QACZ,MAAM,IAAI,KAAK,CACb,OAAO;YACL,CAAC,CAAC,0DAA0D;YAC5D,CAAC,CAAC,SAAS,CACd,CAAC;KACH;IAED,OAAO,OAAO,CAAC;AACjB,CAAC"}
@@ -0,0 +1 @@
1
+ export default function useLocale(): string;
@@ -0,0 +1,5 @@
1
+ import useIntlContext from './useIntlContext';
2
+ export default function useLocale() {
3
+ return useIntlContext().locale;
4
+ }
5
+ //# sourceMappingURL=useLocale.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useLocale.js","sourceRoot":"","sources":["../../../src/react/useLocale.tsx"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAE9C,MAAM,CAAC,OAAO,UAAU,SAAS;IAC/B,OAAO,cAAc,EAAE,CAAC,MAAM,CAAC;AACjC,CAAC"}
@@ -0,0 +1,23 @@
1
+ declare type Options = {
2
+ updateInterval?: number;
3
+ };
4
+ /**
5
+ * Reading the current date via `new Date()` in components should be avoided, as
6
+ * it causes components to be impure and can lead to flaky tests. Instead, this
7
+ * hook can be used.
8
+ *
9
+ * By default, it returns the time when the component mounts. If `updateInterval`
10
+ * is specified, the value will be updated based on the interval.
11
+ *
12
+ * You can however also return a static value from this hook, if you
13
+ * configure the `now` parameter on the context provider. Note however,
14
+ * that if `updateInterval` is configured in this case, the component
15
+ * will initialize with the global value, but will afterwards update
16
+ * continuously based on the interval.
17
+ *
18
+ * For unit tests, this can be mocked to a constant value. For end-to-end
19
+ * testing, an environment parameter can be passed to the `now` parameter
20
+ * of the provider to mock this to a static value.
21
+ */
22
+ export default function useNow(options?: Options): Date;
23
+ export {};
@@ -0,0 +1,40 @@
1
+ import { useState, useEffect } from 'react';
2
+ import useIntlContext from './useIntlContext';
3
+ function getNow() {
4
+ return new Date();
5
+ }
6
+ /**
7
+ * Reading the current date via `new Date()` in components should be avoided, as
8
+ * it causes components to be impure and can lead to flaky tests. Instead, this
9
+ * hook can be used.
10
+ *
11
+ * By default, it returns the time when the component mounts. If `updateInterval`
12
+ * is specified, the value will be updated based on the interval.
13
+ *
14
+ * You can however also return a static value from this hook, if you
15
+ * configure the `now` parameter on the context provider. Note however,
16
+ * that if `updateInterval` is configured in this case, the component
17
+ * will initialize with the global value, but will afterwards update
18
+ * continuously based on the interval.
19
+ *
20
+ * For unit tests, this can be mocked to a constant value. For end-to-end
21
+ * testing, an environment parameter can be passed to the `now` parameter
22
+ * of the provider to mock this to a static value.
23
+ */
24
+ export default function useNow(options) {
25
+ const updateInterval = options?.updateInterval;
26
+ const { now: globalNow } = useIntlContext();
27
+ const [now, setNow] = useState(globalNow || getNow());
28
+ useEffect(() => {
29
+ if (!updateInterval)
30
+ return;
31
+ const intervalId = setInterval(() => {
32
+ setNow(getNow());
33
+ }, updateInterval);
34
+ return () => {
35
+ clearInterval(intervalId);
36
+ };
37
+ }, [globalNow, updateInterval]);
38
+ return now;
39
+ }
40
+ //# sourceMappingURL=useNow.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useNow.js","sourceRoot":"","sources":["../../../src/react/useNow.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAE,SAAS,EAAC,MAAM,OAAO,CAAC;AAC1C,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAM9C,SAAS,MAAM;IACb,OAAO,IAAI,IAAI,EAAE,CAAC;AACpB,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,OAAiB;IAC9C,MAAM,cAAc,GAAG,OAAO,EAAE,cAAc,CAAC;IAE/C,MAAM,EAAC,GAAG,EAAE,SAAS,EAAC,GAAG,cAAc,EAAE,CAAC;IAC1C,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,GAAG,QAAQ,CAAC,SAAS,IAAI,MAAM,EAAE,CAAC,CAAC;IAEtD,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,CAAC,cAAc;YAAE,OAAO;QAE5B,MAAM,UAAU,GAAG,WAAW,CAAC,GAAG,EAAE;YAClC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC;QACnB,CAAC,EAAE,cAAc,CAAC,CAAC;QAEnB,OAAO,GAAG,EAAE;YACV,aAAa,CAAC,UAAU,CAAC,CAAC;QAC5B,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,SAAS,EAAE,cAAc,CAAC,CAAC,CAAC;IAEhC,OAAO,GAAG,CAAC;AACb,CAAC"}
@@ -0,0 +1 @@
1
+ export default function useTimeZone(): string | undefined;
@@ -0,0 +1,5 @@
1
+ import useIntlContext from './useIntlContext';
2
+ export default function useTimeZone() {
3
+ return useIntlContext().timeZone;
4
+ }
5
+ //# sourceMappingURL=useTimeZone.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useTimeZone.js","sourceRoot":"","sources":["../../../src/react/useTimeZone.tsx"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAE9C,MAAM,CAAC,OAAO,UAAU,WAAW;IACjC,OAAO,cAAc,EAAE,CAAC,QAAQ,CAAC;AACnC,CAAC"}
@@ -0,0 +1,44 @@
1
+ import { ReactElement, ReactNodeArray } from 'react';
2
+ import Formats from '../core/Formats';
3
+ import TranslationValues, { RichTranslationValues } from '../core/TranslationValues';
4
+ import MessageKeys from '../core/utils/MessageKeys';
5
+ import NamespaceKeys from '../core/utils/NamespaceKeys';
6
+ import NestedKeyOf from '../core/utils/NestedKeyOf';
7
+ import NestedValueOf from '../core/utils/NestedValueOf';
8
+ /**
9
+ * Translates messages from the given namespace by using the ICU syntax.
10
+ * See https://formatjs.io/docs/core-concepts/icu-syntax.
11
+ *
12
+ * If no namespace is provided, all available messages are returned.
13
+ * The namespace can also indicate nesting by using a dot
14
+ * (e.g. `namespace.Component`).
15
+ */
16
+ export default function useTranslations<NestedKey extends NamespaceKeys<IntlMessages, NestedKeyOf<IntlMessages>> = never>(namespace?: NestedKey): {
17
+ <TargetKey extends MessageKeys<NestedValueOf<{
18
+ '!': IntlMessages;
19
+ }, [
20
+ NestedKey
21
+ ] extends [never] ? '!' : `!.${NestedKey}`>, NestedKeyOf<NestedValueOf<{
22
+ '!': IntlMessages;
23
+ }, [
24
+ NestedKey
25
+ ] extends [never] ? '!' : `!.${NestedKey}`>>>>(key: TargetKey, values?: TranslationValues, formats?: Partial<Formats>): string;
26
+ rich<TargetKey extends MessageKeys<NestedValueOf<{
27
+ '!': IntlMessages;
28
+ }, [
29
+ NestedKey
30
+ ] extends [never] ? '!' : `!.${NestedKey}`>, NestedKeyOf<NestedValueOf<{
31
+ '!': IntlMessages;
32
+ }, [
33
+ NestedKey
34
+ ] extends [never] ? '!' : `!.${NestedKey}`>>>>(key: TargetKey, values?: RichTranslationValues, formats?: Partial<Formats>): string | ReactElement | ReactNodeArray;
35
+ raw<TargetKey extends MessageKeys<NestedValueOf<{
36
+ '!': IntlMessages;
37
+ }, [
38
+ NestedKey
39
+ ] extends [never] ? '!' : `!.${NestedKey}`>, NestedKeyOf<NestedValueOf<{
40
+ '!': IntlMessages;
41
+ }, [
42
+ NestedKey
43
+ ] extends [never] ? '!' : `!.${NestedKey}`>>>>(key: TargetKey): any;
44
+ };
@@ -0,0 +1,21 @@
1
+ import useIntlContext from './useIntlContext';
2
+ import useTranslationsImpl from './useTranslationsImpl';
3
+ /**
4
+ * Translates messages from the given namespace by using the ICU syntax.
5
+ * See https://formatjs.io/docs/core-concepts/icu-syntax.
6
+ *
7
+ * If no namespace is provided, all available messages are returned.
8
+ * The namespace can also indicate nesting by using a dot
9
+ * (e.g. `namespace.Component`).
10
+ */
11
+ export default function useTranslations(namespace) {
12
+ const context = useIntlContext();
13
+ const messages = context.messages;
14
+ // We have to wrap the actual hook so the type inference for the optional
15
+ // namespace works correctly. See https://stackoverflow.com/a/71529575/343045
16
+ // The prefix ("!") is arbitrary.
17
+ return useTranslationsImpl({ '!': messages },
18
+ // @ts-ignore
19
+ namespace ? `!.${namespace}` : '!', '!');
20
+ }
21
+ //# sourceMappingURL=useTranslations.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useTranslations.js","sourceRoot":"","sources":["../../../src/react/useTranslations.tsx"],"names":[],"mappings":"AASA,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAC9C,OAAO,mBAAmB,MAAM,uBAAuB,CAAC;AAExD;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,UAAU,eAAe,CAMrC,SAAqB;IA6DrB,MAAM,OAAO,GAAG,cAAc,EAAE,CAAC;IACjC,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAwB,CAAC;IAElD,yEAAyE;IACzE,6EAA6E;IAC7E,iCAAiC;IACjC,OAAO,mBAAmB,CAIxB,EAAC,GAAG,EAAE,QAAQ,EAAC;IACf,aAAa;IACb,SAAS,CAAC,CAAC,CAAC,KAAK,SAAS,EAAE,CAAC,CAAC,CAAC,GAAG,EAClC,GAAG,CACJ,CAAC;AACJ,CAAC"}
@@ -0,0 +1,8 @@
1
+ /// <reference types="react" />
2
+ import AbstractIntlMessages from '../core/AbstractIntlMessages';
3
+ import NestedKeyOf from '../core/utils/NestedKeyOf';
4
+ export default function useTranslationsImpl<Messages extends AbstractIntlMessages, NestedKey extends NestedKeyOf<Messages>>(allMessages: Messages, namespace: NestedKey, namespacePrefix: string): {
5
+ <TargetKey extends unknown>(key: TargetKey, values?: import("../core/TranslationValues").default | undefined, formats?: Partial<import("../core/Formats").default> | undefined): string;
6
+ rich: (key: string, values?: import("..").RichTranslationValues | undefined, formats?: Partial<import("../core/Formats").default> | undefined) => string | import("react").ReactElement<any, string | import("react").JSXElementConstructor<any>> | import("react").ReactNodeArray;
7
+ raw(key: string): any;
8
+ };
@@ -0,0 +1,35 @@
1
+ import { useMemo, useRef } from 'react';
2
+ import createBaseTranslator, { getMessagesOrError } from '../core/createBaseTranslator';
3
+ import resolveNamespace from '../core/resolveNamespace';
4
+ import useIntlContext from './useIntlContext';
5
+ export default function useTranslationsImpl(allMessages, namespace, namespacePrefix) {
6
+ const { defaultTranslationValues, formats: globalFormats, getMessageFallback, locale, onError, timeZone } = useIntlContext();
7
+ // The `namespacePrefix` is part of the type system.
8
+ // See the comment in the hook invocation.
9
+ allMessages = allMessages[namespacePrefix];
10
+ namespace = resolveNamespace(namespace, namespacePrefix);
11
+ const cachedFormatsByLocaleRef = useRef({});
12
+ const messagesOrError = useMemo(() => getMessagesOrError({ messages: allMessages, namespace, onError }), [allMessages, namespace, onError]);
13
+ const translate = useMemo(() => createBaseTranslator({
14
+ cachedFormatsByLocale: cachedFormatsByLocaleRef.current,
15
+ getMessageFallback,
16
+ messagesOrError,
17
+ defaultTranslationValues,
18
+ namespace,
19
+ onError,
20
+ formats: globalFormats,
21
+ locale,
22
+ timeZone
23
+ }), [
24
+ getMessageFallback,
25
+ messagesOrError,
26
+ defaultTranslationValues,
27
+ namespace,
28
+ onError,
29
+ globalFormats,
30
+ locale,
31
+ timeZone
32
+ ]);
33
+ return translate;
34
+ }
35
+ //# sourceMappingURL=useTranslationsImpl.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useTranslationsImpl.js","sourceRoot":"","sources":["../../../src/react/useTranslationsImpl.tsx"],"names":[],"mappings":"AACA,OAAO,EAAC,OAAO,EAAE,MAAM,EAAC,MAAM,OAAO,CAAC;AAEtC,OAAO,oBAAoB,EAAE,EAC3B,kBAAkB,EACnB,MAAM,8BAA8B,CAAC;AACtC,OAAO,gBAAgB,MAAM,0BAA0B,CAAC;AAExD,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAE9C,MAAM,CAAC,OAAO,UAAU,mBAAmB,CAGzC,WAAqB,EAAE,SAAoB,EAAE,eAAuB;IACpE,MAAM,EACJ,wBAAwB,EACxB,OAAO,EAAE,aAAa,EACtB,kBAAkB,EAClB,MAAM,EACN,OAAO,EACP,QAAQ,EACT,GAAG,cAAc,EAAE,CAAC;IAErB,oDAAoD;IACpD,0CAA0C;IAC1C,WAAW,GAAG,WAAW,CAAC,eAAe,CAAa,CAAC;IACvD,SAAS,GAAG,gBAAgB,CAAC,SAAS,EAAE,eAAe,CAAc,CAAC;IAEtE,MAAM,wBAAwB,GAAG,MAAM,CAErC,EAAE,CAAC,CAAC;IAEN,MAAM,eAAe,GAAG,OAAO,CAC7B,GAAG,EAAE,CAAC,kBAAkB,CAAC,EAAC,QAAQ,EAAE,WAAW,EAAE,SAAS,EAAE,OAAO,EAAC,CAAC,EACrE,CAAC,WAAW,EAAE,SAAS,EAAE,OAAO,CAAC,CAClC,CAAC;IAEF,MAAM,SAAS,GAAG,OAAO,CACvB,GAAG,EAAE,CACH,oBAAoB,CAAC;QACnB,qBAAqB,EAAE,wBAAwB,CAAC,OAAO;QACvD,kBAAkB;QAClB,eAAe;QACf,wBAAwB;QACxB,SAAS;QACT,OAAO;QACP,OAAO,EAAE,aAAa;QACtB,MAAM;QACN,QAAQ;KACT,CAAC,EACJ;QACE,kBAAkB;QAClB,eAAe;QACf,wBAAwB;QACxB,SAAS;QACT,OAAO;QACP,aAAa;QACb,MAAM;QACN,QAAQ;KACT,CACF,CAAC;IAEF,OAAO,SAAS,CAAC;AACnB,CAAC"}
@@ -1,10 +1,10 @@
1
- export { default as IntlError, IntlErrorCode } from './core/use-intl.esm.js';
2
- export { default as createTranslator } from './core/use-intl.esm2.js';
1
+ export { default as IntlError, IntlErrorCode } from './core/use-intl.esm2.js';
2
+ export { default as createTranslator } from './core/use-intl.esm.js';
3
3
  export { default as createIntl } from './core/use-intl.esm3.js';
4
4
  export { default as IntlProvider } from './react/use-intl.esm.js';
5
- export { default as useTranslations } from './react/use-intl.esm2.js';
6
- export { default as useIntl } from './react/use-intl.esm3.js';
7
- export { default as useLocale } from './react/use-intl.esm4.js';
8
- export { default as useNow } from './react/use-intl.esm5.js';
9
- export { default as useTimeZone } from './react/use-intl.esm6.js';
5
+ export { default as useTranslations } from './react/use-intl.esm3.js';
6
+ export { default as useIntl } from './react/use-intl.esm4.js';
7
+ export { default as useLocale } from './react/use-intl.esm6.js';
8
+ export { default as useNow } from './react/use-intl.esm2.js';
9
+ export { default as useTimeZone } from './react/use-intl.esm5.js';
10
10
  //# sourceMappingURL=use-intl.esm.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "use-intl",
3
- "version": "2.9.2",
3
+ "version": "2.10.0-alpha.4",
4
4
  "sideEffects": false,
5
5
  "author": "Jan Amann <jan@amann.me>",
6
6
  "description": "Minimal, but complete solution for managing internationalization in React apps.",
@@ -12,9 +12,11 @@
12
12
  },
13
13
  "scripts": {
14
14
  "start": "dts watch",
15
- "build": "dts build",
16
- "test": "TZ=Europe/Berlin dts test",
17
- "lint": "eslint src test && tsc",
15
+ "build": "yarn build:default && yarn build:rsc",
16
+ "build:default": "rm -rf dist && dts build",
17
+ "build:rsc": "tsc && rm -rf dist/test",
18
+ "test": "TZ=Europe/Berlin dts test --testPathIgnorePatterns=dist",
19
+ "lint": "eslint src test && tsc --noEmit",
18
20
  "prepublishOnly": "yarn test && yarn lint && yarn build"
19
21
  },
20
22
  "main": "dist/index.js",
@@ -59,6 +61,5 @@
59
61
  },
60
62
  "engines": {
61
63
  "node": ">=10"
62
- },
63
- "gitHead": "81e50507a79f295ab4157f7403e673d343bebc38"
64
+ }
64
65
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2020 Jan Amann
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.