next-intl 2.10.0-alpha.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 (53) hide show
  1. package/dist/react-server/index.d.ts +6 -0
  2. package/dist/react-server/useIntl.d.ts +5 -0
  3. package/dist/react-server/useLocale.d.ts +2 -0
  4. package/dist/react-server/useNow.d.ts +1 -0
  5. package/dist/react-server/useTimeZone.d.ts +1 -0
  6. package/dist/react-server/useTranslations.d.ts +2 -0
  7. package/dist/server/NextIntlRequestStorage.d.ts +24 -0
  8. package/dist/server/NextIntlServerProvider.d.ts +3 -0
  9. package/dist/server/NextRequestStorage.d.ts +14 -0
  10. package/dist/server/index.d.ts +1 -0
  11. package/dist/src/react-server/index.d.ts +6 -0
  12. package/dist/src/react-server/index.js +11 -0
  13. package/dist/src/react-server/index.js.map +1 -0
  14. package/dist/src/react-server/useIntl.d.ts +5 -0
  15. package/dist/src/react-server/useIntl.js +5 -0
  16. package/dist/src/react-server/useIntl.js.map +1 -0
  17. package/dist/src/react-server/useLocale.d.ts +2 -0
  18. package/dist/src/react-server/useLocale.js +6 -0
  19. package/dist/src/react-server/useLocale.js.map +1 -0
  20. package/dist/src/react-server/useNow.d.ts +1 -0
  21. package/dist/src/react-server/useNow.js +5 -0
  22. package/dist/src/react-server/useNow.js.map +1 -0
  23. package/dist/src/react-server/useTimeZone.d.ts +1 -0
  24. package/dist/src/react-server/useTimeZone.js +5 -0
  25. package/dist/src/react-server/useTimeZone.js.map +1 -0
  26. package/dist/src/react-server/useTranslations.d.ts +2 -0
  27. package/dist/src/react-server/useTranslations.js +11 -0
  28. package/dist/src/react-server/useTranslations.js.map +1 -0
  29. package/dist/src/server/NextIntlRequestStorage.d.ts +24 -0
  30. package/dist/src/server/NextIntlRequestStorage.js +45 -0
  31. package/dist/src/server/NextIntlRequestStorage.js.map +1 -0
  32. package/dist/src/server/NextIntlServerProvider.d.ts +3 -0
  33. package/dist/src/server/NextIntlServerProvider.js +13 -0
  34. package/dist/src/server/NextIntlServerProvider.js.map +1 -0
  35. package/dist/src/server/NextRequestStorage.d.ts +14 -0
  36. package/dist/src/server/NextRequestStorage.js +28 -0
  37. package/dist/src/server/NextRequestStorage.js.map +1 -0
  38. package/dist/src/server/index.d.ts +1 -0
  39. package/dist/src/server/index.js +1 -0
  40. package/dist/src/server/index.js.map +1 -1
  41. package/package.json +9 -12
  42. package/server.d.ts +1 -0
  43. package/server.js +1 -0
  44. package/src/react-server/index.tsx +12 -0
  45. package/src/react-server/useIntl.tsx +5 -0
  46. package/src/react-server/useLocale.tsx +6 -0
  47. package/src/react-server/useNow.tsx +5 -0
  48. package/src/react-server/useTimeZone.tsx +5 -0
  49. package/src/react-server/useTranslations.tsx +13 -0
  50. package/src/server/NextIntlRequestStorage.tsx +67 -0
  51. package/src/server/NextIntlServerProvider.tsx +17 -0
  52. package/src/server/NextRequestStorage.tsx +35 -0
  53. package/src/server/index.tsx +1 -0
@@ -0,0 +1,6 @@
1
+ export { default as useLocale } from './useLocale';
2
+ export { default as useTranslations } from './useTranslations';
3
+ export { default as useIntl } from './useIntl';
4
+ export { default as useNow } from './useNow';
5
+ export { default as useTimeZone } from './useTimeZone';
6
+ export * from 'use-intl/dist/src/core';
@@ -0,0 +1,5 @@
1
+ export default function useIntl(): {
2
+ formatDateTime: (value: number | Date, formatOrOptions?: string | import("use-intl/dist/src/core/DateTimeFormatOptions").default | undefined) => string;
3
+ formatNumber: (value: number, formatOrOptions?: string | import("use-intl").NumberFormatOptions | undefined) => string;
4
+ formatRelativeTime: (date: number | Date, now?: number | Date | undefined) => string;
5
+ };
@@ -0,0 +1,2 @@
1
+ import 'server-only';
2
+ export default function useLocale(): string;
@@ -0,0 +1 @@
1
+ export default function useNow(): Date | undefined;
@@ -0,0 +1 @@
1
+ export default function useTimeZone(): string | undefined;
@@ -0,0 +1,2 @@
1
+ import 'server-only';
2
+ export default function useTranslations(namespace?: string): (key: string, ...rest: any) => string;
@@ -0,0 +1,24 @@
1
+ import IntlContextValue from 'use-intl/dist/src/react/IntlContextValue';
2
+ declare class NextIntlRequestStorage {
3
+ private key;
4
+ private storage;
5
+ constructor();
6
+ initRequest(opts: IntlContextValue): void;
7
+ isInitialized(): boolean;
8
+ getLocale(): string;
9
+ getTranslator(): {
10
+ <TargetKey extends never>(key: TargetKey, values?: import("use-intl/dist/src/core/TranslationValues").default | undefined, formats?: Partial<import("use-intl/dist/src/core/Formats").default> | undefined): string;
11
+ rich<TargetKey_1 extends never>(key: TargetKey_1, values?: import("use-intl/dist/src/core/createTranslatorImpl").CoreRichTranslationValues | undefined, formats?: Partial<import("use-intl/dist/src/core/Formats").default> | undefined): string;
12
+ raw<TargetKey_2 extends never>(key: TargetKey_2): any;
13
+ };
14
+ getIntl(): {
15
+ formatDateTime: (value: number | Date, formatOrOptions?: string | import("use-intl/dist/src/core/DateTimeFormatOptions").default | undefined) => string;
16
+ formatNumber: (value: number, formatOrOptions?: string | import("@formatjs/ecma402-abstract/types/number").NumberFormatOptions | undefined) => string;
17
+ formatRelativeTime: (date: number | Date, now?: number | Date | undefined) => string;
18
+ };
19
+ getNow(): Date | undefined;
20
+ getTimeZone(): string | undefined;
21
+ private getInitializedStorage;
22
+ }
23
+ declare const instance: NextIntlRequestStorage;
24
+ export default instance;
@@ -0,0 +1,3 @@
1
+ import 'server-only';
2
+ import IntlProviderProps from 'use-intl/dist/src/react/IntlProviderProps';
3
+ export default function NextIntlServerProvider(props: IntlProviderProps): JSX.Element;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Returns the request-level storage of Next.js where typically headers
3
+ * and cookies are stored. This is recreated for every request.
4
+ *
5
+ * This uses internal APIs of Next.js and may break in
6
+ * the future, so we should really move away from this.
7
+ */
8
+ export default class NextRequestStorage<Data> {
9
+ private key;
10
+ constructor(key: string);
11
+ set(value: Data): void;
12
+ get(): Data | undefined;
13
+ private getStorage;
14
+ }
@@ -1 +1,2 @@
1
1
  export { default as createIntlMiddleware } from './createIntlMiddleware';
2
+ export { default as NextIntlServerProvider } from './NextIntlServerProvider';
@@ -0,0 +1,6 @@
1
+ export { default as useLocale } from './useLocale';
2
+ export { default as useTranslations } from './useTranslations';
3
+ export { default as useIntl } from './useIntl';
4
+ export { default as useNow } from './useNow';
5
+ export { default as useTimeZone } from './useTimeZone';
6
+ export * from 'use-intl/dist/src/core';
@@ -0,0 +1,11 @@
1
+ // This is the main entry file when the 'react-server' environment imports
2
+ // from 'next-intl'. Make sure this mirrors the API from the main entry.
3
+ // Replaced exports from the `react` package
4
+ export { default as useLocale } from './useLocale';
5
+ export { default as useTranslations } from './useTranslations';
6
+ export { default as useIntl } from './useIntl';
7
+ export { default as useNow } from './useNow';
8
+ export { default as useTimeZone } from './useTimeZone';
9
+ // Everything from `core`
10
+ export * from 'use-intl/dist/src/core';
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/react-server/index.tsx"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,wEAAwE;AAExE,4CAA4C;AAC5C,OAAO,EAAC,OAAO,IAAI,SAAS,EAAC,MAAM,aAAa,CAAC;AACjD,OAAO,EAAC,OAAO,IAAI,eAAe,EAAC,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAC,OAAO,IAAI,OAAO,EAAC,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAC,OAAO,IAAI,MAAM,EAAC,MAAM,UAAU,CAAC;AAC3C,OAAO,EAAC,OAAO,IAAI,WAAW,EAAC,MAAM,eAAe,CAAC;AAErD,yBAAyB;AACzB,cAAc,wBAAwB,CAAC"}
@@ -0,0 +1,5 @@
1
+ export default function useIntl(): {
2
+ formatDateTime: (value: number | Date, formatOrOptions?: string | import("use-intl/dist/src/core/DateTimeFormatOptions").default | undefined) => string;
3
+ formatNumber: (value: number, formatOrOptions?: string | import("use-intl").NumberFormatOptions | undefined) => string;
4
+ formatRelativeTime: (date: number | Date, now?: number | Date | undefined) => string;
5
+ };
@@ -0,0 +1,5 @@
1
+ import NextIntlRequestStorage from '../server/NextIntlRequestStorage';
2
+ export default function useIntl() {
3
+ return NextIntlRequestStorage.getIntl();
4
+ }
5
+ //# sourceMappingURL=useIntl.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useIntl.js","sourceRoot":"","sources":["../../../src/react-server/useIntl.tsx"],"names":[],"mappings":"AAAA,OAAO,sBAAsB,MAAM,kCAAkC,CAAC;AAEtE,MAAM,CAAC,OAAO,UAAU,OAAO;IAC7B,OAAO,sBAAsB,CAAC,OAAO,EAAE,CAAC;AAC1C,CAAC"}
@@ -0,0 +1,2 @@
1
+ import 'server-only';
2
+ export default function useLocale(): string;
@@ -0,0 +1,6 @@
1
+ import 'server-only';
2
+ import NextIntlRequestStorage from '../server/NextIntlRequestStorage';
3
+ export default function useLocale() {
4
+ return NextIntlRequestStorage.getLocale();
5
+ }
6
+ //# sourceMappingURL=useLocale.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useLocale.js","sourceRoot":"","sources":["../../../src/react-server/useLocale.tsx"],"names":[],"mappings":"AAAA,OAAO,aAAa,CAAC;AACrB,OAAO,sBAAsB,MAAM,kCAAkC,CAAC;AAEtE,MAAM,CAAC,OAAO,UAAU,SAAS;IAC/B,OAAO,sBAAsB,CAAC,SAAS,EAAE,CAAC;AAC5C,CAAC"}
@@ -0,0 +1 @@
1
+ export default function useNow(): Date | undefined;
@@ -0,0 +1,5 @@
1
+ import NextIntlRequestStorage from '../server/NextIntlRequestStorage';
2
+ export default function useNow() {
3
+ return NextIntlRequestStorage.getNow();
4
+ }
5
+ //# sourceMappingURL=useNow.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useNow.js","sourceRoot":"","sources":["../../../src/react-server/useNow.tsx"],"names":[],"mappings":"AAAA,OAAO,sBAAsB,MAAM,kCAAkC,CAAC;AAEtE,MAAM,CAAC,OAAO,UAAU,MAAM;IAC5B,OAAO,sBAAsB,CAAC,MAAM,EAAE,CAAC;AACzC,CAAC"}
@@ -0,0 +1 @@
1
+ export default function useTimeZone(): string | undefined;
@@ -0,0 +1,5 @@
1
+ import NextIntlRequestStorage from '../server/NextIntlRequestStorage';
2
+ export default function useTimeZone() {
3
+ return NextIntlRequestStorage.getTimeZone();
4
+ }
5
+ //# sourceMappingURL=useTimeZone.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useTimeZone.js","sourceRoot":"","sources":["../../../src/react-server/useTimeZone.tsx"],"names":[],"mappings":"AAAA,OAAO,sBAAsB,MAAM,kCAAkC,CAAC;AAEtE,MAAM,CAAC,OAAO,UAAU,WAAW;IACjC,OAAO,sBAAsB,CAAC,WAAW,EAAE,CAAC;AAC9C,CAAC"}
@@ -0,0 +1,2 @@
1
+ import 'server-only';
2
+ export default function useTranslations(namespace?: string): (key: string, ...rest: any) => string;
@@ -0,0 +1,11 @@
1
+ import 'server-only';
2
+ import NextIntlRequestStorage from '../server/NextIntlRequestStorage';
3
+ export default function useTranslations(namespace) {
4
+ const translator = NextIntlRequestStorage.getTranslator();
5
+ return function translate(key, ...rest) {
6
+ const path = [namespace, key].filter((part) => part != null).join('.');
7
+ // @ts-expect-error We're using the types from the real `useTranslations` anyway
8
+ return translator(path, ...rest);
9
+ };
10
+ }
11
+ //# sourceMappingURL=useTranslations.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useTranslations.js","sourceRoot":"","sources":["../../../src/react-server/useTranslations.tsx"],"names":[],"mappings":"AAAA,OAAO,aAAa,CAAC;AACrB,OAAO,sBAAsB,MAAM,kCAAkC,CAAC;AAEtE,MAAM,CAAC,OAAO,UAAU,eAAe,CAAC,SAAkB;IACxD,MAAM,UAAU,GAAG,sBAAsB,CAAC,aAAa,EAAE,CAAC;IAE1D,OAAO,SAAS,SAAS,CAAC,GAAW,EAAE,GAAG,IAAS;QACjD,MAAM,IAAI,GAAG,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,IAAI,IAAI,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAEvE,gFAAgF;QAChF,OAAO,UAAU,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,CAAC;IACnC,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,24 @@
1
+ import IntlContextValue from 'use-intl/dist/src/react/IntlContextValue';
2
+ declare class NextIntlRequestStorage {
3
+ private key;
4
+ private storage;
5
+ constructor();
6
+ initRequest(opts: IntlContextValue): void;
7
+ isInitialized(): boolean;
8
+ getLocale(): string;
9
+ getTranslator(): {
10
+ <TargetKey extends never>(key: TargetKey, values?: import("use-intl/dist/src/core/TranslationValues").default | undefined, formats?: Partial<import("use-intl/dist/src/core/Formats").default> | undefined): string;
11
+ rich<TargetKey_1 extends never>(key: TargetKey_1, values?: import("use-intl/dist/src/core/createTranslatorImpl").CoreRichTranslationValues | undefined, formats?: Partial<import("use-intl/dist/src/core/Formats").default> | undefined): string;
12
+ raw<TargetKey_2 extends never>(key: TargetKey_2): any;
13
+ };
14
+ getIntl(): {
15
+ formatDateTime: (value: number | Date, formatOrOptions?: string | import("use-intl/dist/src/core/DateTimeFormatOptions").default | undefined) => string;
16
+ formatNumber: (value: number, formatOrOptions?: string | import("@formatjs/ecma402-abstract/types/number").NumberFormatOptions | undefined) => string;
17
+ formatRelativeTime: (date: number | Date, now?: number | Date | undefined) => string;
18
+ };
19
+ getNow(): Date | undefined;
20
+ getTimeZone(): string | undefined;
21
+ private getInitializedStorage;
22
+ }
23
+ declare const instance: NextIntlRequestStorage;
24
+ export default instance;
@@ -0,0 +1,45 @@
1
+ import { createTranslator, createIntl } from 'use-intl/dist/src/core';
2
+ import NextRequestStorage from './NextRequestStorage';
3
+ class NextIntlRequestStorage {
4
+ constructor() {
5
+ this.key = '__next-intl';
6
+ this.storage = new NextRequestStorage(this.key);
7
+ }
8
+ initRequest(opts) {
9
+ const translator = createTranslator({
10
+ ...opts,
11
+ messages: opts.messages
12
+ });
13
+ const intl = createIntl(opts);
14
+ const now = opts.now || new Date();
15
+ this.storage.set({ ...opts, translator, intl, now });
16
+ }
17
+ isInitialized() {
18
+ return this.storage.get() != null;
19
+ }
20
+ getLocale() {
21
+ return this.getInitializedStorage().locale;
22
+ }
23
+ getTranslator() {
24
+ return this.getInitializedStorage().translator;
25
+ }
26
+ getIntl() {
27
+ return this.getInitializedStorage().intl;
28
+ }
29
+ getNow() {
30
+ return this.getInitializedStorage().now;
31
+ }
32
+ getTimeZone() {
33
+ return this.getInitializedStorage().timeZone;
34
+ }
35
+ getInitializedStorage() {
36
+ if (!this.isInitialized()) {
37
+ throw new Error('`NextIntlServerProvider` was not initialized.');
38
+ }
39
+ return this.storage.get();
40
+ }
41
+ }
42
+ // Provide a singleton-instance of the storage.
43
+ const instance = new NextIntlRequestStorage();
44
+ export default instance;
45
+ //# sourceMappingURL=NextIntlRequestStorage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NextIntlRequestStorage.js","sourceRoot":"","sources":["../../../src/server/NextIntlRequestStorage.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,gBAAgB,EAAE,UAAU,EAAC,MAAM,wBAAwB,CAAC;AAGpE,OAAO,kBAAkB,MAAM,sBAAsB,CAAC;AAEtD,MAAM,sBAAsB;IAU1B;QATQ,QAAG,GAAG,aAAa,CAAC;QAU1B,IAAI,CAAC,OAAO,GAAG,IAAI,kBAAkB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAClD,CAAC;IAEM,WAAW,CAAC,IAAsB;QACvC,MAAM,UAAU,GAAG,gBAAgB,CAAC;YAClC,GAAG,IAAI;YACP,QAAQ,EAAE,IAAI,CAAC,QAAQ;SACxB,CAAC,CAAC;QACH,MAAM,IAAI,GAAG,UAAU,CAAC,IAAI,CAAC,CAAC;QAC9B,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,IAAI,IAAI,IAAI,EAAE,CAAC;QAEnC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,EAAC,GAAG,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,GAAG,EAAC,CAAC,CAAC;IACrD,CAAC;IAEM,aAAa;QAClB,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,IAAI,IAAI,CAAC;IACpC,CAAC;IAEM,SAAS;QACd,OAAO,IAAI,CAAC,qBAAqB,EAAE,CAAC,MAAM,CAAC;IAC7C,CAAC;IAEM,aAAa;QAClB,OAAO,IAAI,CAAC,qBAAqB,EAAE,CAAC,UAAU,CAAC;IACjD,CAAC;IAEM,OAAO;QACZ,OAAO,IAAI,CAAC,qBAAqB,EAAE,CAAC,IAAI,CAAC;IAC3C,CAAC;IAEM,MAAM;QACX,OAAO,IAAI,CAAC,qBAAqB,EAAE,CAAC,GAAG,CAAC;IAC1C,CAAC;IAEM,WAAW;QAChB,OAAO,IAAI,CAAC,qBAAqB,EAAE,CAAC,QAAQ,CAAC;IAC/C,CAAC;IAEO,qBAAqB;QAC3B,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,EAAE;YACzB,MAAM,IAAI,KAAK,CAAC,+CAA+C,CAAC,CAAC;SAClE;QAED,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,EAAG,CAAC;IAC7B,CAAC;CACF;AAED,+CAA+C;AAC/C,MAAM,QAAQ,GAAG,IAAI,sBAAsB,EAAE,CAAC;AAE9C,eAAe,QAAQ,CAAC"}
@@ -0,0 +1,3 @@
1
+ import 'server-only';
2
+ import IntlProviderProps from 'use-intl/dist/src/react/IntlProviderProps';
3
+ export default function NextIntlServerProvider(props: IntlProviderProps): JSX.Element;
@@ -0,0 +1,13 @@
1
+ import 'server-only';
2
+ import React from 'react';
3
+ import getIntlContextValue from 'use-intl/dist/src/react/getIntlContextValue';
4
+ import NextIntlRequestStorage from './NextIntlRequestStorage';
5
+ export default function NextIntlServerProvider(props) {
6
+ // This provider must only be rendered a single time per request.
7
+ if (NextIntlRequestStorage.isInitialized()) {
8
+ console.error(`\`NextIntlServerProvider\` was already initialized.`);
9
+ }
10
+ NextIntlRequestStorage.initRequest(getIntlContextValue(props));
11
+ return React.createElement(React.Fragment, null, props.children);
12
+ }
13
+ //# sourceMappingURL=NextIntlServerProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NextIntlServerProvider.js","sourceRoot":"","sources":["../../../src/server/NextIntlServerProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,aAAa,CAAC;AACrB,OAAO,KAAK,MAAM,OAAO,CAAC;AAG1B,OAAO,mBAAmB,MAAM,6CAA6C,CAAC;AAC9E,OAAO,sBAAsB,MAAM,0BAA0B,CAAC;AAE9D,MAAM,CAAC,OAAO,UAAU,sBAAsB,CAAC,KAAwB;IACrE,iEAAiE;IACjE,IAAI,sBAAsB,CAAC,aAAa,EAAE,EAAE;QAC1C,OAAO,CAAC,KAAK,CAAC,qDAAqD,CAAC,CAAC;KACtE;IAED,sBAAsB,CAAC,WAAW,CAAC,mBAAmB,CAAC,KAAK,CAAC,CAAC,CAAC;IAE/D,OAAO,0CAAG,KAAK,CAAC,QAAQ,CAAI,CAAC;AAC/B,CAAC"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Returns the request-level storage of Next.js where typically headers
3
+ * and cookies are stored. This is recreated for every request.
4
+ *
5
+ * This uses internal APIs of Next.js and may break in
6
+ * the future, so we should really move away from this.
7
+ */
8
+ export default class NextRequestStorage<Data> {
9
+ private key;
10
+ constructor(key: string);
11
+ set(value: Data): void;
12
+ get(): Data | undefined;
13
+ private getStorage;
14
+ }
@@ -0,0 +1,28 @@
1
+ import { requestAsyncStorage } from 'next/dist/client/components/request-async-storage';
2
+ /**
3
+ * Returns the request-level storage of Next.js where typically headers
4
+ * and cookies are stored. This is recreated for every request.
5
+ *
6
+ * This uses internal APIs of Next.js and may break in
7
+ * the future, so we should really move away from this.
8
+ */
9
+ export default class NextRequestStorage {
10
+ constructor(key) {
11
+ this.key = key;
12
+ }
13
+ set(value) {
14
+ const storage = this.getStorage();
15
+ storage[this.key] = value;
16
+ }
17
+ get() {
18
+ const storage = this.getStorage();
19
+ return storage[this.key];
20
+ }
21
+ getStorage() {
22
+ const requestStore = requestAsyncStorage && 'getStore' in requestAsyncStorage
23
+ ? requestAsyncStorage.getStore()
24
+ : requestAsyncStorage;
25
+ return requestStore;
26
+ }
27
+ }
28
+ //# sourceMappingURL=NextRequestStorage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NextRequestStorage.js","sourceRoot":"","sources":["../../../src/server/NextRequestStorage.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,mBAAmB,EAAC,MAAM,mDAAmD,CAAC;AAEtF;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,OAAO,kBAAkB;IAGrC,YAAY,GAAW;QACrB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IACjB,CAAC;IAEM,GAAG,CAAC,KAAW;QACpB,MAAM,OAAO,GAAG,IAAI,CAAC,UAAU,EAAE,CAAC;QACjC,OAAe,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IACrC,CAAC;IAEM,GAAG;QACR,MAAM,OAAO,GAAG,IAAI,CAAC,UAAU,EAAE,CAAC;QAClC,OAAQ,OAAe,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACpC,CAAC;IAEO,UAAU;QAChB,MAAM,YAAY,GAChB,mBAAmB,IAAI,UAAU,IAAI,mBAAmB;YACtD,CAAC,CAAC,mBAAmB,CAAC,QAAQ,EAAE;YAChC,CAAC,CAAC,mBAAmB,CAAC;QAE1B,OAAO,YAAY,CAAC;IACtB,CAAC;CACF"}
@@ -1 +1,2 @@
1
1
  export { default as createIntlMiddleware } from './createIntlMiddleware';
2
+ export { default as NextIntlServerProvider } from './NextIntlServerProvider';
@@ -1,2 +1,3 @@
1
1
  export { default as createIntlMiddleware } from './createIntlMiddleware';
2
+ export { default as NextIntlServerProvider } from './NextIntlServerProvider';
2
3
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/server/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,IAAI,oBAAoB,EAAC,MAAM,wBAAwB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/server/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,IAAI,oBAAoB,EAAC,MAAM,wBAAwB,CAAC;AACvE,OAAO,EAAC,OAAO,IAAI,sBAAsB,EAAC,MAAM,0BAA0B,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-intl",
3
- "version": "2.9.1",
3
+ "version": "2.10.0-alpha.4",
4
4
  "sideEffects": false,
5
5
  "author": "Jan Amann <jan@amann.me>",
6
6
  "description": "A minimal, but complete solution for internationalization in Next.js apps.",
@@ -11,33 +11,29 @@
11
11
  "url": "https://github.com/amannn/next-intl"
12
12
  },
13
13
  "scripts": {
14
- "start": "dts watch",
15
14
  "build": "yarn build:default && yarn build:rsc",
16
15
  "build:default": "rm -rf dist && dts build",
17
16
  "build:rsc": "tsc && rm -rf dist/test",
18
17
  "test": "TZ=Europe/Berlin dts test --testPathIgnorePatterns=dist",
19
18
  "lint": "eslint src test && tsc --noEmit",
20
- "prepublishOnly": "yarn test && yarn lint && yarn build && cp ../../README.md . && git checkout .",
21
- "postpublish": "rm ./README.md"
19
+ "prepublishOnly": "yarn test && yarn lint && yarn build && cp ../../README.md .",
20
+ "postpublish": "git checkout . && rm ./README.md"
22
21
  },
23
22
  "main": "dist/index.js",
24
23
  "module": "dist/src/index.esm.js",
25
24
  "typings": "dist/src/index.d.ts",
26
25
  "exports": {
27
26
  ".": {
28
- "react-server": "./dist/src/index.js",
27
+ "react-server": "./dist/src/react-server/index.js",
29
28
  "default": "./dist/index.js"
30
29
  },
31
- "./server": {
32
- "react-server": "./dist/src/index.js",
33
- "default": "./dist/src/server/index.js"
34
- }
30
+ "./server": "./dist/src/server/index.js"
35
31
  },
36
32
  "files": [
37
33
  "dist",
38
34
  "src",
39
- "./server.js",
40
- "./server.d.ts"
35
+ "server.js",
36
+ "server.d.ts"
41
37
  ],
42
38
  "keywords": [
43
39
  "react",
@@ -57,7 +53,8 @@
57
53
  },
58
54
  "dependencies": {
59
55
  "accept-language-parser": "^1.5.0",
60
- "use-intl": "^2.9.1"
56
+ "server-only": "0.0.1",
57
+ "use-intl": "2.10.0-alpha.3"
61
58
  },
62
59
  "peerDependencies": {
63
60
  "next": "^10.0.0 || ^11.0.0 || ^12.0.0 || ^13.0.0",
package/server.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/src/server';
package/server.js ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/src/server';
@@ -0,0 +1,12 @@
1
+ // This is the main entry file when the 'react-server' environment imports
2
+ // from 'next-intl'. Make sure this mirrors the API from the main entry.
3
+
4
+ // Replaced exports from the `react` package
5
+ export {default as useLocale} from './useLocale';
6
+ export {default as useTranslations} from './useTranslations';
7
+ export {default as useIntl} from './useIntl';
8
+ export {default as useNow} from './useNow';
9
+ export {default as useTimeZone} from './useTimeZone';
10
+
11
+ // Everything from `core`
12
+ export * from 'use-intl/dist/src/core';
@@ -0,0 +1,5 @@
1
+ import NextIntlRequestStorage from '../server/NextIntlRequestStorage';
2
+
3
+ export default function useIntl() {
4
+ return NextIntlRequestStorage.getIntl();
5
+ }
@@ -0,0 +1,6 @@
1
+ import 'server-only';
2
+ import NextIntlRequestStorage from '../server/NextIntlRequestStorage';
3
+
4
+ export default function useLocale() {
5
+ return NextIntlRequestStorage.getLocale();
6
+ }
@@ -0,0 +1,5 @@
1
+ import NextIntlRequestStorage from '../server/NextIntlRequestStorage';
2
+
3
+ export default function useNow() {
4
+ return NextIntlRequestStorage.getNow();
5
+ }
@@ -0,0 +1,5 @@
1
+ import NextIntlRequestStorage from '../server/NextIntlRequestStorage';
2
+
3
+ export default function useTimeZone() {
4
+ return NextIntlRequestStorage.getTimeZone();
5
+ }
@@ -0,0 +1,13 @@
1
+ import 'server-only';
2
+ import NextIntlRequestStorage from '../server/NextIntlRequestStorage';
3
+
4
+ export default function useTranslations(namespace?: string) {
5
+ const translator = NextIntlRequestStorage.getTranslator();
6
+
7
+ return function translate(key: string, ...rest: any) {
8
+ const path = [namespace, key].filter((part) => part != null).join('.');
9
+
10
+ // @ts-expect-error We're using the types from the real `useTranslations` anyway
11
+ return translator(path, ...rest);
12
+ };
13
+ }
@@ -0,0 +1,67 @@
1
+ import {createTranslator, createIntl} from 'use-intl/dist/src/core';
2
+ // eslint-disable-next-line import/default -- False positive
3
+ import IntlContextValue from 'use-intl/dist/src/react/IntlContextValue';
4
+ import NextRequestStorage from './NextRequestStorage';
5
+
6
+ class NextIntlRequestStorage {
7
+ private key = '__next-intl';
8
+
9
+ private storage: NextRequestStorage<
10
+ IntlContextValue & {
11
+ translator: ReturnType<typeof createTranslator>;
12
+ intl: ReturnType<typeof createIntl>;
13
+ }
14
+ >;
15
+
16
+ constructor() {
17
+ this.storage = new NextRequestStorage(this.key);
18
+ }
19
+
20
+ public initRequest(opts: IntlContextValue) {
21
+ const translator = createTranslator({
22
+ ...opts,
23
+ messages: opts.messages
24
+ });
25
+ const intl = createIntl(opts);
26
+ const now = opts.now || new Date();
27
+
28
+ this.storage.set({...opts, translator, intl, now});
29
+ }
30
+
31
+ public isInitialized() {
32
+ return this.storage.get() != null;
33
+ }
34
+
35
+ public getLocale() {
36
+ return this.getInitializedStorage().locale;
37
+ }
38
+
39
+ public getTranslator() {
40
+ return this.getInitializedStorage().translator;
41
+ }
42
+
43
+ public getIntl() {
44
+ return this.getInitializedStorage().intl;
45
+ }
46
+
47
+ public getNow() {
48
+ return this.getInitializedStorage().now;
49
+ }
50
+
51
+ public getTimeZone() {
52
+ return this.getInitializedStorage().timeZone;
53
+ }
54
+
55
+ private getInitializedStorage() {
56
+ if (!this.isInitialized()) {
57
+ throw new Error('`NextIntlServerProvider` was not initialized.');
58
+ }
59
+
60
+ return this.storage.get()!;
61
+ }
62
+ }
63
+
64
+ // Provide a singleton-instance of the storage.
65
+ const instance = new NextIntlRequestStorage();
66
+
67
+ export default instance;
@@ -0,0 +1,17 @@
1
+ import 'server-only';
2
+ import React from 'react';
3
+ // eslint-disable-next-line import/default -- False positive
4
+ import IntlProviderProps from 'use-intl/dist/src/react/IntlProviderProps';
5
+ import getIntlContextValue from 'use-intl/dist/src/react/getIntlContextValue';
6
+ import NextIntlRequestStorage from './NextIntlRequestStorage';
7
+
8
+ export default function NextIntlServerProvider(props: IntlProviderProps) {
9
+ // This provider must only be rendered a single time per request.
10
+ if (NextIntlRequestStorage.isInitialized()) {
11
+ console.error(`\`NextIntlServerProvider\` was already initialized.`);
12
+ }
13
+
14
+ NextIntlRequestStorage.initRequest(getIntlContextValue(props));
15
+
16
+ return <>{props.children}</>;
17
+ }
@@ -0,0 +1,35 @@
1
+ import {requestAsyncStorage} from 'next/dist/client/components/request-async-storage';
2
+
3
+ /**
4
+ * Returns the request-level storage of Next.js where typically headers
5
+ * and cookies are stored. This is recreated for every request.
6
+ *
7
+ * This uses internal APIs of Next.js and may break in
8
+ * the future, so we should really move away from this.
9
+ */
10
+ export default class NextRequestStorage<Data> {
11
+ private key: string;
12
+
13
+ constructor(key: string) {
14
+ this.key = key;
15
+ }
16
+
17
+ public set(value: Data) {
18
+ const storage = this.getStorage();
19
+ (storage as any)[this.key] = value;
20
+ }
21
+
22
+ public get(): Data | undefined {
23
+ const storage = this.getStorage();
24
+ return (storage as any)[this.key];
25
+ }
26
+
27
+ private getStorage() {
28
+ const requestStore =
29
+ requestAsyncStorage && 'getStore' in requestAsyncStorage
30
+ ? requestAsyncStorage.getStore()
31
+ : requestAsyncStorage;
32
+
33
+ return requestStore;
34
+ }
35
+ }
@@ -1 +1,2 @@
1
1
  export {default as createIntlMiddleware} from './createIntlMiddleware';
2
+ export {default as NextIntlServerProvider} from './NextIntlServerProvider';