react-intlayer 1.0.0 → 1.2.0

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 (169) hide show
  1. package/dist/cjs/ContentEditor/ContentEditor.cjs +36 -39
  2. package/dist/cjs/ContentEditor/ContentEditor.d.ts +2 -2
  3. package/dist/cjs/ContentEditor/contentRender.cjs +10 -18
  4. package/dist/cjs/ContentEditor/index.cjs +11 -27
  5. package/dist/cjs/ContentEditor/index.d.ts +3 -3
  6. package/dist/cjs/IntlayerProvider.cjs +37 -0
  7. package/dist/cjs/IntlayerProvider.cjs.map +1 -0
  8. package/dist/cjs/IntlayerProvider.d.ts +9 -0
  9. package/dist/cjs/client/IntlayerClientProvider.cjs +45 -0
  10. package/dist/cjs/client/IntlayerClientProvider.cjs.map +1 -0
  11. package/dist/cjs/client/IntlayerClientProvider.d.ts +24 -0
  12. package/dist/cjs/client/index.cjs +24 -31
  13. package/dist/cjs/client/index.cjs.map +1 -1
  14. package/dist/cjs/client/index.d.ts +10 -19
  15. package/dist/cjs/client/useContent.cjs +11 -16
  16. package/dist/cjs/client/useContent.cjs.map +1 -1
  17. package/dist/cjs/client/useContent.d.ts +10 -8
  18. package/dist/cjs/client/useIntlayer.cjs +11 -18
  19. package/dist/cjs/client/useIntlayer.cjs.map +1 -1
  20. package/dist/cjs/client/useIntlayer.d.ts +8 -3
  21. package/dist/cjs/client/useLocale.cjs +12 -20
  22. package/dist/cjs/client/useLocale.cjs.map +1 -1
  23. package/dist/cjs/client/useLocale.d.ts +8 -5
  24. package/dist/cjs/client/useLocaleCookie.cjs +20 -30
  25. package/dist/cjs/client/useLocaleCookie.cjs.map +1 -1
  26. package/dist/cjs/client/useLocaleCookie.d.ts +12 -3
  27. package/dist/cjs/client/useTraduction.cjs +11 -18
  28. package/dist/cjs/client/useTraduction.cjs.map +1 -1
  29. package/dist/cjs/client/useTraduction.d.ts +22 -4
  30. package/dist/cjs/getEnumeration.cjs +9 -14
  31. package/dist/cjs/getEnumeration.cjs.map +1 -1
  32. package/dist/cjs/getEnumeration.d.ts +27 -5
  33. package/dist/cjs/getTranslation.cjs +9 -14
  34. package/dist/cjs/getTranslation.cjs.map +1 -1
  35. package/dist/cjs/getTranslation.d.ts +23 -6
  36. package/dist/cjs/index.cjs +20 -27
  37. package/dist/cjs/index.cjs.map +1 -1
  38. package/dist/cjs/index.d.ts +11 -20
  39. package/dist/cjs/processDictionary/contentDictionary.d.cjs +4 -8
  40. package/dist/cjs/processDictionary/contentDictionary.d.d.ts +6 -9
  41. package/dist/cjs/processDictionary/index.cjs +15 -17
  42. package/dist/cjs/processDictionary/index.cjs.map +1 -1
  43. package/dist/cjs/processDictionary/index.d.ts +6 -17
  44. package/dist/cjs/server/IntlayerServerProvider.cjs +46 -0
  45. package/dist/cjs/server/IntlayerServerProvider.cjs.map +1 -0
  46. package/dist/cjs/server/IntlayerServerProvider.d.ts +37 -0
  47. package/dist/cjs/server/getLocaleTranslation.cjs +12 -19
  48. package/dist/cjs/server/getLocaleTranslation.cjs.map +1 -1
  49. package/dist/cjs/server/getLocaleTranslation.d.ts +8 -7
  50. package/dist/cjs/server/index.cjs +20 -27
  51. package/dist/cjs/server/index.cjs.map +1 -1
  52. package/dist/cjs/server/index.d.ts +9 -13
  53. package/dist/cjs/server/serverContext.cjs +25 -31
  54. package/dist/cjs/server/serverContext.cjs.map +1 -1
  55. package/dist/cjs/server/serverContext.d.ts +16 -24
  56. package/dist/cjs/server/useIntlayer.cjs +12 -21
  57. package/dist/cjs/server/useIntlayer.cjs.map +1 -1
  58. package/dist/cjs/server/useIntlayer.d.ts +8 -3
  59. package/dist/cjs/server/useTraduction.cjs +12 -19
  60. package/dist/cjs/server/useTraduction.cjs.map +1 -1
  61. package/dist/cjs/server/useTraduction.d.ts +22 -4
  62. package/dist/cjs/useIntlayerBase.cjs +22 -31
  63. package/dist/cjs/useIntlayerBase.cjs.map +1 -1
  64. package/dist/cjs/useIntlayerBase.d.ts +7 -18
  65. package/dist/esm/ContentEditor/ContentEditor.d.mts +2 -2
  66. package/dist/esm/ContentEditor/ContentEditor.mjs +38 -29
  67. package/dist/esm/ContentEditor/contentRender.mjs +5 -3
  68. package/dist/esm/ContentEditor/index.d.mts +3 -3
  69. package/dist/esm/ContentEditor/index.mjs +3 -3
  70. package/dist/esm/IntlayerProvider.d.mts +9 -0
  71. package/dist/esm/IntlayerProvider.mjs +17 -0
  72. package/dist/esm/IntlayerProvider.mjs.map +1 -0
  73. package/dist/esm/client/IntlayerClientProvider.d.mts +24 -0
  74. package/dist/esm/client/IntlayerClientProvider.mjs +24 -0
  75. package/dist/esm/client/IntlayerClientProvider.mjs.map +1 -0
  76. package/dist/esm/client/index.d.mts +10 -19
  77. package/dist/esm/client/index.mjs +14 -14
  78. package/dist/esm/client/index.mjs.map +1 -1
  79. package/dist/esm/client/useContent.d.mts +10 -8
  80. package/dist/esm/client/useContent.mjs +7 -5
  81. package/dist/esm/client/useContent.mjs.map +1 -1
  82. package/dist/esm/client/useIntlayer.d.mts +8 -3
  83. package/dist/esm/client/useIntlayer.mjs +9 -5
  84. package/dist/esm/client/useIntlayer.mjs.map +1 -1
  85. package/dist/esm/client/useLocale.d.mts +8 -5
  86. package/dist/esm/client/useLocale.mjs +7 -5
  87. package/dist/esm/client/useLocale.mjs.map +1 -1
  88. package/dist/esm/client/useLocaleCookie.d.mts +12 -3
  89. package/dist/esm/client/useLocaleCookie.mjs +11 -5
  90. package/dist/esm/client/useLocaleCookie.mjs.map +1 -1
  91. package/dist/esm/client/useTraduction.d.mts +22 -4
  92. package/dist/esm/client/useTraduction.mjs +7 -5
  93. package/dist/esm/client/useTraduction.mjs.map +1 -1
  94. package/dist/esm/getEnumeration.d.mts +27 -5
  95. package/dist/esm/getEnumeration.mjs +9 -4
  96. package/dist/esm/getEnumeration.mjs.map +1 -1
  97. package/dist/esm/getTranslation.d.mts +23 -6
  98. package/dist/esm/getTranslation.mjs +8 -4
  99. package/dist/esm/getTranslation.mjs.map +1 -1
  100. package/dist/esm/index.d.mts +11 -20
  101. package/dist/esm/index.mjs +9 -11
  102. package/dist/esm/index.mjs.map +1 -1
  103. package/dist/esm/processDictionary/contentDictionary.d.d.mts +6 -9
  104. package/dist/esm/processDictionary/contentDictionary.d.mjs +1 -1
  105. package/dist/esm/processDictionary/index.d.mts +6 -17
  106. package/dist/esm/processDictionary/index.mjs +25 -9
  107. package/dist/esm/processDictionary/index.mjs.map +1 -1
  108. package/dist/esm/server/IntlayerServerProvider.d.mts +37 -0
  109. package/dist/esm/server/IntlayerServerProvider.mjs +21 -0
  110. package/dist/esm/server/IntlayerServerProvider.mjs.map +1 -0
  111. package/dist/esm/server/getLocaleTranslation.d.mts +8 -7
  112. package/dist/esm/server/getLocaleTranslation.mjs +8 -6
  113. package/dist/esm/server/getLocaleTranslation.mjs.map +1 -1
  114. package/dist/esm/server/index.d.mts +9 -13
  115. package/dist/esm/server/index.mjs +10 -10
  116. package/dist/esm/server/index.mjs.map +1 -1
  117. package/dist/esm/server/serverContext.d.mts +16 -24
  118. package/dist/esm/server/serverContext.mjs +13 -6
  119. package/dist/esm/server/serverContext.mjs.map +1 -1
  120. package/dist/esm/server/useIntlayer.d.mts +8 -3
  121. package/dist/esm/server/useIntlayer.mjs +10 -6
  122. package/dist/esm/server/useIntlayer.mjs.map +1 -1
  123. package/dist/esm/server/useTraduction.d.mts +22 -4
  124. package/dist/esm/server/useTraduction.mjs +8 -6
  125. package/dist/esm/server/useTraduction.mjs.map +1 -1
  126. package/dist/esm/useIntlayerBase.d.mts +7 -18
  127. package/dist/esm/useIntlayerBase.mjs +9 -4
  128. package/dist/esm/useIntlayerBase.mjs.map +1 -1
  129. package/package.json +10 -10
  130. package/src/IntlayerProvider.tsx +24 -0
  131. package/src/client/IntlayerClientProvider.tsx +45 -0
  132. package/src/client/index.ts +5 -5
  133. package/src/client/useContent.ts +3 -0
  134. package/src/client/useIntlayer.ts +7 -2
  135. package/src/client/useLocale.ts +5 -2
  136. package/src/client/useLocaleCookie.ts +9 -1
  137. package/src/client/useTraduction.ts +23 -3
  138. package/src/getEnumeration.ts +25 -0
  139. package/src/getTranslation.ts +21 -1
  140. package/src/index.ts +3 -4
  141. package/src/processDictionary/index.ts +1 -1
  142. package/src/server/IntlayerServerProvider.tsx +39 -0
  143. package/src/server/getLocaleTranslation.ts +5 -2
  144. package/src/server/index.ts +3 -3
  145. package/src/server/serverContext.ts +4 -4
  146. package/src/server/useIntlayer.ts +8 -2
  147. package/src/server/useTraduction.ts +23 -3
  148. package/src/useIntlayerBase.ts +4 -1
  149. package/dist/cjs/LocaleContextProvider.cjs +0 -49
  150. package/dist/cjs/LocaleContextProvider.cjs.map +0 -1
  151. package/dist/cjs/LocaleContextProvider.d.ts +0 -10
  152. package/dist/cjs/client/LocaleClientContextProvider.cjs +0 -54
  153. package/dist/cjs/client/LocaleClientContextProvider.cjs.map +0 -1
  154. package/dist/cjs/client/LocaleClientContextProvider.d.ts +0 -20
  155. package/dist/cjs/server/LocaleServerContextProvider.cjs +0 -49
  156. package/dist/cjs/server/LocaleServerContextProvider.cjs.map +0 -1
  157. package/dist/cjs/server/LocaleServerContextProvider.d.ts +0 -34
  158. package/dist/esm/LocaleContextProvider.d.mts +0 -10
  159. package/dist/esm/LocaleContextProvider.mjs +0 -15
  160. package/dist/esm/LocaleContextProvider.mjs.map +0 -1
  161. package/dist/esm/client/LocaleClientContextProvider.d.mts +0 -20
  162. package/dist/esm/client/LocaleClientContextProvider.mjs +0 -16
  163. package/dist/esm/client/LocaleClientContextProvider.mjs.map +0 -1
  164. package/dist/esm/server/LocaleServerContextProvider.d.mts +0 -34
  165. package/dist/esm/server/LocaleServerContextProvider.mjs +0 -14
  166. package/dist/esm/server/LocaleServerContextProvider.mjs.map +0 -1
  167. package/src/LocaleContextProvider.tsx +0 -26
  168. package/src/client/LocaleClientContextProvider.tsx +0 -35
  169. package/src/server/LocaleServerContextProvider.tsx +0 -24
@@ -1,13 +1,9 @@
1
- export { getLocaleTranslation as getLocaleContent } from "./getLocaleTranslation.mjs";
2
- export { useTraduction } from "./useTraduction.mjs";
3
- export {
4
- LocaleServerContext,
5
- LocaleServerContextProvider,
6
- locale,
7
- } from "./LocaleServerContextProvider.mjs";
8
- export { useIntlayer } from "./useIntlayer.mjs";
9
- import "@intlayer/config/client";
10
- import "@intlayer/core";
11
- import "react";
12
- import "../useIntlayerBase.mjs";
13
- import "intlayer";
1
+ export { getLocaleTranslation as getLocaleContent } from './getLocaleTranslation.mjs';
2
+ export { useTraduction } from './useTraduction.mjs';
3
+ export { IntlayerServerContext as IntlayerServer, IntlayerServerProvider, locale } from './IntlayerServerProvider.mjs';
4
+ export { useIntlayer } from './useIntlayer.mjs';
5
+ import '@intlayer/config/client';
6
+ import '@intlayer/core';
7
+ import 'react';
8
+ import '../useIntlayerBase.mjs';
9
+ import 'intlayer';
@@ -1,17 +1,17 @@
1
- import { getLocaleTranslation } from "./getLocaleTranslation.mjs";
2
- import { useTraduction } from "./useTraduction.mjs";
1
+ import { getLocaleTranslation } from './getLocaleTranslation.mjs';
2
+ import { useTraduction } from './useTraduction.mjs';
3
3
  import {
4
- LocaleServerContext,
4
+ IntlayerServerContext,
5
5
  locale,
6
- LocaleServerContextProvider,
7
- } from "./LocaleServerContextProvider.mjs";
8
- import { useIntlayer } from "./useIntlayer.mjs";
6
+ IntlayerServerProvider
7
+ } from './IntlayerServerProvider.mjs';
8
+ import { useIntlayer } from './useIntlayer.mjs';
9
9
  export {
10
- LocaleServerContext,
11
- LocaleServerContextProvider,
10
+ IntlayerServerContext as IntlayerServer,
11
+ IntlayerServerProvider,
12
12
  getLocaleTranslation as getLocaleContent,
13
13
  locale,
14
14
  useIntlayer,
15
- useTraduction,
15
+ useTraduction
16
16
  };
17
- //# sourceMappingURL=index.mjs.map
17
+ //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/server/index.ts"],"sourcesContent":["export { getLocaleTranslation as getLocaleContent } from './getLocaleTranslation';\nexport { useTraduction } from './useTraduction';\nexport {\n LocaleServerContext,\n locale,\n LocaleServerContextProvider,\n} from './LocaleServerContextProvider';\nexport { useIntlayer } from './useIntlayer';\n"],"mappings":"AAAA,SAAiC,4BAAwB;AACzD,SAAS,qBAAqB;AAC9B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,mBAAmB;","names":[]}
1
+ {"version":3,"sources":["../../../src/server/index.ts"],"sourcesContent":["export { getLocaleTranslation as getLocaleContent } from './getLocaleTranslation';\nexport { useTraduction } from './useTraduction';\nexport {\n IntlayerServerContext as IntlayerServer,\n locale,\n IntlayerServerProvider,\n} from './IntlayerServerProvider';\nexport { useIntlayer } from './useIntlayer';\n"],"mappings":"AAAA,SAAiC,4BAAwB;AACzD,SAAS,qBAAqB;AAC9B;AAAA,EAC2B;AAAA,EACzB;AAAA,EACA;AAAA,OACK;AACP,SAAS,mBAAmB;","names":[]}
@@ -3,11 +3,11 @@
3
3
  * Attempts to closely mimic the `createContext` API.
4
4
  *
5
5
  * @example
6
- * const LocaleServerContext = createServerContext<string | null>(null);
6
+ * const IntlayerServer = createServerContext<string | null>(null);
7
7
  *
8
- * <LocaleServerContext.Provider value={locale}>
8
+ * <IntlayerServer.Provider value={locale}>
9
9
  * {children}
10
- * </LocaleServerContext.Provider>
10
+ * </IntlayerServer.Provider>
11
11
  */
12
12
  declare const createServerContext: <T>(defaultValue?: T) => ServerContext<T>;
13
13
  /**
@@ -15,29 +15,21 @@ declare const createServerContext: <T>(defaultValue?: T) => ServerContext<T>;
15
15
  * Attempts to closely mimic the `useContext` API.
16
16
  *
17
17
  * @example
18
- * getServerContext(LocaleServerContext);
18
+ * getServerContext(IntlayerServer);
19
19
  */
20
- declare const getServerContext: <T>({
21
- _storage,
22
- _defaultValue,
23
- }: ServerContext<T>) => T | undefined;
20
+ declare const getServerContext: <T>({ _storage, _defaultValue, }: ServerContext<T>) => T | undefined;
24
21
  type ServerContext<T> = {
25
- Provider: ({
26
- children,
27
- value,
28
- }: {
29
- children: React.ReactNode;
30
- value: T;
31
- }) => React.ReactNode;
32
- Consumer: ({
33
- children,
34
- }: {
35
- children: (context: T | undefined) => React.ReactNode;
36
- }) => React.ReactNode;
37
- _storage: () => {
38
- value: T | undefined;
39
- };
40
- _defaultValue: T | undefined;
22
+ Provider: ({ children, value, }: {
23
+ children: React.ReactNode;
24
+ value: T;
25
+ }) => React.ReactNode;
26
+ Consumer: ({ children, }: {
27
+ children: (context: T | undefined) => React.ReactNode;
28
+ }) => React.ReactNode;
29
+ _storage: () => {
30
+ value: T | undefined;
31
+ };
32
+ _defaultValue: T | undefined;
41
33
  };
42
34
 
43
35
  export { createServerContext, getServerContext };
@@ -4,7 +4,7 @@ const createServerContext = (defaultValue) => {
4
4
  throwInClient();
5
5
  const cache = react.cache ?? cacheFallback;
6
6
  const getCache = cache(() => ({
7
- value: void 0,
7
+ value: void 0
8
8
  }));
9
9
  return {
10
10
  Provider: ({ children, value }) => {
@@ -16,12 +16,16 @@ const createServerContext = (defaultValue) => {
16
16
  return children(store ? store.value : defaultValue);
17
17
  },
18
18
  _storage: getCache,
19
- _defaultValue: defaultValue,
19
+ _defaultValue: defaultValue
20
20
  };
21
21
  };
22
- const getServerContext = ({ _storage, _defaultValue }) => {
22
+ const getServerContext = ({
23
+ _storage,
24
+ _defaultValue
25
+ }) => {
23
26
  const store = _storage();
24
- if (!store) return _defaultValue;
27
+ if (!store)
28
+ return _defaultValue;
25
29
  return store.value;
26
30
  };
27
31
  const throwInClient = () => {
@@ -29,5 +33,8 @@ const throwInClient = () => {
29
33
  throw new Error(`createServerContext only works in Server Components`);
30
34
  }
31
35
  };
32
- export { createServerContext, getServerContext };
33
- //# sourceMappingURL=serverContext.mjs.map
36
+ export {
37
+ createServerContext,
38
+ getServerContext
39
+ };
40
+ //# sourceMappingURL=serverContext.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/server/serverContext.ts"],"sourcesContent":["/**\n * Creates a new datastore for a given server context.\n * Attempts to closely mimic the `createContext` API.\n *\n * @example\n * const LocaleServerContext = createServerContext<string | null>(null);\n *\n * <LocaleServerContext.Provider value={locale}>\n * {children}\n * </LocaleServerContext.Provider>\n */\n\nimport react from 'react';\n\ntype CacheType<T> = () => { value: T | undefined };\n\nconst cacheFallback = () => () => ({ value: undefined });\n\nexport const createServerContext = <T>(defaultValue?: T): ServerContext<T> => {\n throwInClient();\n\n const cache = react.cache<CacheType<T>> ?? cacheFallback;\n\n const getCache = cache(() => ({\n value: undefined,\n }));\n\n return {\n Provider: ({ children, value }) => {\n getCache().value = value;\n return children;\n },\n Consumer: ({ children }) => {\n const store = getCache();\n return children(store ? store.value : defaultValue);\n },\n _storage: getCache,\n _defaultValue: defaultValue,\n };\n};\n\n/**\n * Fetches a value present in a given server context.\n * Attempts to closely mimic the `useContext` API.\n *\n * @example\n * getServerContext(LocaleServerContext);\n */\nexport const getServerContext = <T>({\n _storage,\n _defaultValue,\n}: ServerContext<T>) => {\n // throwInClient();\n const store = _storage();\n if (!store) return _defaultValue;\n return store.value;\n};\n\ntype ServerContext<T> = {\n Provider: ({\n children,\n value,\n }: {\n children: React.ReactNode;\n value: T;\n }) => React.ReactNode;\n Consumer: ({\n children,\n }: {\n children: (context: T | undefined) => React.ReactNode;\n }) => React.ReactNode;\n _storage: () => { value: T | undefined };\n _defaultValue: T | undefined;\n};\n\n/**\n * Throws if called within a client component environment.\n * Useful to help prevent mistakes.\n */\nconst throwInClient = (): void | never => {\n // If window.document is defined we're in a client component\n if (typeof window !== 'undefined') {\n throw new Error(`createServerContext only works in Server Components`);\n }\n};\n"],"mappings":"AAYA,OAAO,WAAW;AAIlB,MAAM,gBAAgB,MAAM,OAAO,EAAE,OAAO,OAAU;AAE/C,MAAM,sBAAsB,CAAI,iBAAuC;AAC5E,gBAAc;AAEd,QAAM,QAAQ,MAAM,SAAuB;AAE3C,QAAM,WAAW,MAAM,OAAO;AAAA,IAC5B,OAAO;AAAA,EACT,EAAE;AAEF,SAAO;AAAA,IACL,UAAU,CAAC,EAAE,UAAU,MAAM,MAAM;AACjC,eAAS,EAAE,QAAQ;AACnB,aAAO;AAAA,IACT;AAAA,IACA,UAAU,CAAC,EAAE,SAAS,MAAM;AAC1B,YAAM,QAAQ,SAAS;AACvB,aAAO,SAAS,QAAQ,MAAM,QAAQ,YAAY;AAAA,IACpD;AAAA,IACA,UAAU;AAAA,IACV,eAAe;AAAA,EACjB;AACF;AASO,MAAM,mBAAmB,CAAI;AAAA,EAClC;AAAA,EACA;AACF,MAAwB;AAEtB,QAAM,QAAQ,SAAS;AACvB,MAAI,CAAC;AAAO,WAAO;AACnB,SAAO,MAAM;AACf;AAuBA,MAAM,gBAAgB,MAAoB;AAExC,MAAI,OAAO,WAAW,aAAa;AACjC,UAAM,IAAI,MAAM,qDAAqD;AAAA,EACvE;AACF;","names":[]}
1
+ {"version":3,"sources":["../../../src/server/serverContext.ts"],"sourcesContent":["/**\n * Creates a new datastore for a given server context.\n * Attempts to closely mimic the `createContext` API.\n *\n * @example\n * const IntlayerServer = createServerContext<string | null>(null);\n *\n * <IntlayerServer.Provider value={locale}>\n * {children}\n * </IntlayerServer.Provider>\n */\n\nimport react from 'react';\n\ntype CacheType<T> = () => { value: T | undefined };\n\nconst cacheFallback = () => () => ({ value: undefined });\n\nexport const createServerContext = <T>(defaultValue?: T): ServerContext<T> => {\n throwInClient();\n\n const cache = react.cache<CacheType<T>> ?? cacheFallback;\n\n const getCache = cache(() => ({\n value: undefined,\n }));\n\n return {\n Provider: ({ children, value }) => {\n getCache().value = value;\n return children;\n },\n Consumer: ({ children }) => {\n const store = getCache();\n return children(store ? store.value : defaultValue);\n },\n _storage: getCache,\n _defaultValue: defaultValue,\n };\n};\n\n/**\n * Fetches a value present in a given server context.\n * Attempts to closely mimic the `useContext` API.\n *\n * @example\n * getServerContext(IntlayerServer);\n */\nexport const getServerContext = <T>({\n _storage,\n _defaultValue,\n}: ServerContext<T>) => {\n // throwInClient();\n const store = _storage();\n if (!store) return _defaultValue;\n return store.value;\n};\n\ntype ServerContext<T> = {\n Provider: ({\n children,\n value,\n }: {\n children: React.ReactNode;\n value: T;\n }) => React.ReactNode;\n Consumer: ({\n children,\n }: {\n children: (context: T | undefined) => React.ReactNode;\n }) => React.ReactNode;\n _storage: () => { value: T | undefined };\n _defaultValue: T | undefined;\n};\n\n/**\n * Throws if called within a client component environment.\n * Useful to help prevent mistakes.\n */\nconst throwInClient = (): void | never => {\n // If window.document is defined we're in a client component\n if (typeof window !== 'undefined') {\n throw new Error(`createServerContext only works in Server Components`);\n }\n};\n"],"mappings":"AAYA,OAAO,WAAW;AAIlB,MAAM,gBAAgB,MAAM,OAAO,EAAE,OAAO,OAAU;AAE/C,MAAM,sBAAsB,CAAI,iBAAuC;AAC5E,gBAAc;AAEd,QAAM,QAAQ,MAAM,SAAuB;AAE3C,QAAM,WAAW,MAAM,OAAO;AAAA,IAC5B,OAAO;AAAA,EACT,EAAE;AAEF,SAAO;AAAA,IACL,UAAU,CAAC,EAAE,UAAU,MAAM,MAAM;AACjC,eAAS,EAAE,QAAQ;AACnB,aAAO;AAAA,IACT;AAAA,IACA,UAAU,CAAC,EAAE,SAAS,MAAM;AAC1B,YAAM,QAAQ,SAAS;AACvB,aAAO,SAAS,QAAQ,MAAM,QAAQ,YAAY;AAAA,IACpD;AAAA,IACA,UAAU;AAAA,IACV,eAAe;AAAA,EACjB;AACF;AASO,MAAM,mBAAmB,CAAI;AAAA,EAClC;AAAA,EACA;AACF,MAAwB;AAEtB,QAAM,QAAQ,SAAS;AACvB,MAAI,CAAC;AAAO,WAAO;AACnB,SAAO,MAAM;AACf;AAuBA,MAAM,gBAAgB,MAAoB;AAExC,MAAI,OAAO,WAAW,aAAa;AACjC,UAAM,IAAI,MAAM,qDAAqD;AAAA,EACvE;AACF;","names":[]}
@@ -1,7 +1,12 @@
1
- import { UseIntlayer } from "../useIntlayerBase.mjs";
2
- import "@intlayer/config/client";
3
- import "intlayer";
1
+ import { UseIntlayer } from '../useIntlayerBase.mjs';
2
+ import '@intlayer/config/client';
3
+ import 'intlayer';
4
4
 
5
+ /**
6
+ * On the server side, Hook that picking one dictionary by its id and return the content
7
+ *
8
+ * If the locale is not provided, it will use the locale from the server context
9
+ */
5
10
  declare const useIntlayer: UseIntlayer;
6
11
 
7
12
  export { useIntlayer };
@@ -1,9 +1,13 @@
1
- import { useIntlayerBase } from "../useIntlayerBase.mjs";
2
- import { LocaleServerContext } from "./LocaleServerContextProvider.mjs";
3
- import { getServerContext } from "./serverContext.mjs";
1
+ import {
2
+ useIntlayerBase
3
+ } from '../useIntlayerBase.mjs';
4
+ import { IntlayerServerContext } from './IntlayerServerProvider.mjs';
5
+ import { getServerContext } from './serverContext.mjs';
4
6
  const useIntlayer = (id, locale) => {
5
- const localeTarget = locale ?? getServerContext(LocaleServerContext);
7
+ const localeTarget = locale ?? getServerContext(IntlayerServerContext);
6
8
  return useIntlayerBase(id, localeTarget);
7
9
  };
8
- export { useIntlayer };
9
- //# sourceMappingURL=useIntlayer.mjs.map
10
+ export {
11
+ useIntlayer
12
+ };
13
+ //# sourceMappingURL=useIntlayer.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/server/useIntlayer.ts"],"sourcesContent":["import type { Locales } from '@intlayer/config/client';\nimport {\n type DictionaryKeys,\n useIntlayerBase,\n type UseIntlayer,\n} from '../useIntlayerBase';\nimport { LocaleServerContext } from './LocaleServerContextProvider';\nimport { getServerContext } from './serverContext';\n\nexport const useIntlayer: UseIntlayer = <T extends DictionaryKeys>(\n id: T,\n locale?: Locales\n) => {\n const localeTarget = locale ?? getServerContext<Locales>(LocaleServerContext);\n\n return useIntlayerBase(id, localeTarget);\n};\n"],"mappings":"AACA;AAAA,EAEE;AAAA,OAEK;AACP,SAAS,2BAA2B;AACpC,SAAS,wBAAwB;AAE1B,MAAM,cAA2B,CACtC,IACA,WACG;AACH,QAAM,eAAe,UAAU,iBAA0B,mBAAmB;AAE5E,SAAO,gBAAgB,IAAI,YAAY;AACzC;","names":[]}
1
+ {"version":3,"sources":["../../../src/server/useIntlayer.ts"],"sourcesContent":["import type { Locales } from '@intlayer/config/client';\nimport {\n type DictionaryKeys,\n useIntlayerBase,\n type UseIntlayer,\n} from '../useIntlayerBase';\nimport { IntlayerServerContext } from './IntlayerServerProvider';\nimport { getServerContext } from './serverContext';\n\n/**\n * On the server side, Hook that picking one dictionary by its id and return the content\n *\n * If the locale is not provided, it will use the locale from the server context\n */\nexport const useIntlayer: UseIntlayer = <T extends DictionaryKeys>(\n id: T,\n locale?: Locales\n) => {\n const localeTarget =\n locale ?? getServerContext<Locales>(IntlayerServerContext);\n\n return useIntlayerBase(id, localeTarget);\n};\n"],"mappings":"AACA;AAAA,EAEE;AAAA,OAEK;AACP,SAAS,6BAA6B;AACtC,SAAS,wBAAwB;AAO1B,MAAM,cAA2B,CACtC,IACA,WACG;AACH,QAAM,eACJ,UAAU,iBAA0B,qBAAqB;AAE3D,SAAO,gBAAgB,IAAI,YAAY;AACzC;","names":[]}
@@ -1,7 +1,25 @@
1
- import { LanguageContent } from "@intlayer/core";
1
+ import { LanguageContent } from '@intlayer/core';
2
2
 
3
- declare const useTraduction: <Content>(
4
- languageContent: LanguageContent<Content>
5
- ) => Content;
3
+ /**
4
+ * On the server side, hook to get the translation content based on the locale
5
+ *
6
+ * If not locale found, it will return the content related to the default locale.
7
+ *
8
+ * Return either the content editor, or the content itself depending on the configuration.
9
+ *
10
+ * Usage:
11
+ *
12
+ * const content = getTranslation<string>({
13
+ * en: 'Hello',
14
+ * fr: 'Bonjour',
15
+ * },
16
+ * 'fr');
17
+ * // 'Bonjour'
18
+ *
19
+ * Using TypeScript:
20
+ * - this function will require each locale to be defined if defined in the project configuration.
21
+ * - If a locale is missing, it will make each existing locale optional and raise an error if the locale is not found.
22
+ */
23
+ declare const useTraduction: <Content = string>(languageContent: LanguageContent<Content>) => Content;
6
24
 
7
25
  export { useTraduction };
@@ -1,9 +1,11 @@
1
- import { getTranslation } from "../getTranslation.mjs";
2
- import { LocaleServerContext } from "./LocaleServerContextProvider.mjs";
3
- import { getServerContext } from "./serverContext.mjs";
1
+ import { getTranslation } from '../getTranslation.mjs';
2
+ import { IntlayerServerContext } from './IntlayerServerProvider.mjs';
3
+ import { getServerContext } from './serverContext.mjs';
4
4
  const useTraduction = (languageContent) => {
5
- const locale = getServerContext(LocaleServerContext);
5
+ const locale = getServerContext(IntlayerServerContext);
6
6
  return getTranslation(languageContent, locale);
7
7
  };
8
- export { useTraduction };
9
- //# sourceMappingURL=useTraduction.mjs.map
8
+ export {
9
+ useTraduction
10
+ };
11
+ //# sourceMappingURL=useTraduction.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/server/useTraduction.ts"],"sourcesContent":["import type { Locales } from '@intlayer/config/client';\nimport type { LanguageContent } from '@intlayer/core';\nimport { getTranslation } from '../getTranslation';\nimport { LocaleServerContext } from './LocaleServerContextProvider';\nimport { getServerContext } from './serverContext';\n\nexport const useTraduction = <Content>(\n languageContent: LanguageContent<Content>\n) => {\n const locale = getServerContext<Locales>(LocaleServerContext);\n\n return getTranslation(languageContent, locale);\n};\n"],"mappings":"AAEA,SAAS,sBAAsB;AAC/B,SAAS,2BAA2B;AACpC,SAAS,wBAAwB;AAE1B,MAAM,gBAAgB,CAC3B,oBACG;AACH,QAAM,SAAS,iBAA0B,mBAAmB;AAE5D,SAAO,eAAe,iBAAiB,MAAM;AAC/C;","names":[]}
1
+ {"version":3,"sources":["../../../src/server/useTraduction.ts"],"sourcesContent":["import type { Locales } from '@intlayer/config/client';\nimport type { LanguageContent } from '@intlayer/core';\nimport { getTranslation } from '../getTranslation';\nimport { IntlayerServerContext } from './IntlayerServerProvider';\nimport { getServerContext } from './serverContext';\n\n/**\n * On the server side, hook to get the translation content based on the locale\n *\n * If not locale found, it will return the content related to the default locale.\n *\n * Return either the content editor, or the content itself depending on the configuration.\n *\n * Usage:\n *\n * const content = getTranslation<string>({\n * en: 'Hello',\n * fr: 'Bonjour',\n * },\n * 'fr');\n * // 'Bonjour'\n *\n * Using TypeScript:\n * - this function will require each locale to be defined if defined in the project configuration.\n * - If a locale is missing, it will make each existing locale optional and raise an error if the locale is not found.\n */\nexport const useTraduction = <Content = string>(\n languageContent: LanguageContent<Content>\n) => {\n const locale = getServerContext<Locales>(IntlayerServerContext);\n\n return getTranslation(languageContent, locale);\n};\n"],"mappings":"AAEA,SAAS,sBAAsB;AAC/B,SAAS,6BAA6B;AACtC,SAAS,wBAAwB;AAsB1B,MAAM,gBAAgB,CAC3B,oBACG;AACH,QAAM,SAAS,iBAA0B,qBAAqB;AAE9D,SAAO,eAAe,iBAAiB,MAAM;AAC/C;","names":[]}
@@ -1,23 +1,12 @@
1
- import { Locales } from "@intlayer/config/client";
2
- import { IntlayerDictionaryTypesConnector } from "intlayer";
3
-
4
- /**
5
- * @intlayer/dictionaries-entry is a package that only returns the dictionary entry path.
6
- * Using an external package allow to alias it in the bundle configuration (such as webpack).
7
- * The alias allow hot reload the app (such as nextjs) on any dictionary change.
8
- */
1
+ import { Locales } from '@intlayer/config/client';
2
+ import { IntlayerDictionaryTypesConnector } from 'intlayer';
9
3
 
10
4
  type StringFallback<T> = T extends never ? string : T;
11
5
  type DictionaryKeys = StringFallback<keyof IntlayerDictionaryTypesConnector>;
12
- type UseIntlayer = <T extends DictionaryKeys>(
13
- id: T,
14
- locale?: Locales
15
- ) => IntlayerDictionaryTypesConnector[T];
6
+ type UseIntlayer = <T extends DictionaryKeys>(id: T, locale?: Locales) => IntlayerDictionaryTypesConnector[T];
7
+ /**
8
+ * Hook that picking one dictionary by its id and return the content.
9
+ */
16
10
  declare const useIntlayerBase: UseIntlayer;
17
11
 
18
- export {
19
- type DictionaryKeys,
20
- type StringFallback,
21
- type UseIntlayer,
22
- useIntlayerBase,
23
- };
12
+ export { type DictionaryKeys, type StringFallback, type UseIntlayer, useIntlayerBase };
@@ -1,8 +1,13 @@
1
1
  import dictionaries from "@intlayer/dictionaries-entry";
2
- import { processDictionary } from "./processDictionary/index.mjs";
2
+ import { processDictionary } from './processDictionary/index.mjs';
3
3
  const useIntlayerBase = (id, locale) => {
4
4
  const dictionary = dictionaries[id];
5
- return processDictionary(dictionary, locale);
5
+ return processDictionary(
6
+ dictionary,
7
+ locale
8
+ );
6
9
  };
7
- export { useIntlayerBase };
8
- //# sourceMappingURL=useIntlayerBase.mjs.map
10
+ export {
11
+ useIntlayerBase
12
+ };
13
+ //# sourceMappingURL=useIntlayerBase.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/useIntlayerBase.ts"],"sourcesContent":["/**\n * @intlayer/dictionaries-entry is a package that only returns the dictionary entry path.\n * Using an external package allow to alias it in the bundle configuration (such as webpack).\n * The alias allow hot reload the app (such as nextjs) on any dictionary change.\n */\nimport type { Locales } from '@intlayer/config/client';\nimport dictionaries from '@intlayer/dictionaries-entry';\nimport type { IntlayerDictionaryTypesConnector } from 'intlayer';\nimport type { Content } from './processDictionary/contentDictionary';\nimport { processDictionary } from './processDictionary/index';\n\nexport type StringFallback<T> = T extends never ? string : T; // If no keys are found, return string to disable error, and accept any string as dictionary key\nexport type DictionaryKeys = StringFallback<\n keyof IntlayerDictionaryTypesConnector\n>;\n\nexport type UseIntlayer = <T extends DictionaryKeys>(\n id: T,\n locale?: Locales\n) => IntlayerDictionaryTypesConnector[T];\n\nexport const useIntlayerBase: UseIntlayer = <T extends DictionaryKeys>(\n id: T,\n locale?: Locales\n) => {\n const dictionary: Content = dictionaries[id];\n\n return processDictionary(\n dictionary,\n locale\n ) as IntlayerDictionaryTypesConnector[T];\n};\n"],"mappings":"AAMA,OAAO,kBAAkB;AAGzB,SAAS,yBAAyB;AAY3B,MAAM,kBAA+B,CAC1C,IACA,WACG;AACH,QAAM,aAAsB,aAAa,EAAE;AAE3C,SAAO;AAAA,IACL;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/useIntlayerBase.ts"],"sourcesContent":["import type { Locales } from '@intlayer/config/client';\n/**\n * @intlayer/dictionaries-entry is a package that only returns the dictionary entry path.\n * Using an external package allow to alias it in the bundle configuration (such as webpack).\n * The alias allow hot reload the app (such as nextjs) on any dictionary change.\n */\nimport dictionaries from '@intlayer/dictionaries-entry';\nimport type { IntlayerDictionaryTypesConnector } from 'intlayer';\nimport type { Content } from './processDictionary/contentDictionary';\nimport { processDictionary } from './processDictionary/index';\n\nexport type StringFallback<T> = T extends never ? string : T; // If no keys are found, return string to disable error, and accept any string as dictionary key\nexport type DictionaryKeys = StringFallback<\n keyof IntlayerDictionaryTypesConnector\n>;\n\nexport type UseIntlayer = <T extends DictionaryKeys>(\n id: T,\n locale?: Locales\n) => IntlayerDictionaryTypesConnector[T];\n\n/**\n * Hook that picking one dictionary by its id and return the content.\n */\nexport const useIntlayerBase: UseIntlayer = <T extends DictionaryKeys>(\n id: T,\n locale?: Locales\n) => {\n const dictionary: Content = dictionaries[id];\n\n return processDictionary(\n dictionary,\n locale\n ) as IntlayerDictionaryTypesConnector[T];\n};\n"],"mappings":"AAMA,OAAO,kBAAkB;AAGzB,SAAS,yBAAyB;AAe3B,MAAM,kBAA+B,CAC1C,IACA,WACG;AACH,QAAM,aAAsB,aAAa,EAAE;AAE3C,SAAO;AAAA,IACL;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-intlayer",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "private": false,
5
5
  "description": "Internationalization layer for React applications - Layer of abstraction between the business logic and the data access layer. Manage internationalization in a simple way, through TypeScript, JavaScript or JSON declaration file.",
6
6
  "keywords": [
@@ -19,7 +19,7 @@
19
19
  "homepage": "https://github.com/aypineau/intlayer",
20
20
  "repository": {
21
21
  "type": "git",
22
- "url": "https://github.com/aypineau/intlayer"
22
+ "url": "git+https://github.com/aypineau/intlayer.git"
23
23
  },
24
24
  "license": "MIT",
25
25
  "author": {
@@ -55,22 +55,22 @@
55
55
  "dependencies": {
56
56
  "js-cookie": "^3.0.5",
57
57
  "react-cookie": "^7.1.4",
58
- "@intlayer/config": "^1.0.0",
59
- "@intlayer/core": "^1.0.0",
60
- "@intlayer/dictionaries-entry": "^1.0.0",
61
- "intlayer": "^1.0.0"
58
+ "@intlayer/config": "^1.2.0",
59
+ "@intlayer/core": "^1.2.0",
60
+ "@intlayer/dictionaries-entry": "^1.2.0",
61
+ "intlayer": "^1.2.0"
62
62
  },
63
63
  "devDependencies": {
64
64
  "@types/js-cookie": "^3.0.6",
65
65
  "@types/node": "^20.12.7",
66
- "@types/react": "^18.2.78",
66
+ "@types/react": "^18.2.79",
67
67
  "react": "^18.2.0",
68
68
  "react-dom": "^18.2.0",
69
69
  "rimraf": "5.0.5",
70
70
  "tsup": "^8.0.2",
71
- "typescript": "^5.4.3",
72
- "@utils/eslint-config": "^1.0.0",
73
- "@utils/ts-config": "^1.0.0"
71
+ "typescript": "^5.4.5",
72
+ "@utils/eslint-config": "^1.0.1",
73
+ "@utils/ts-config": "^1.0.1"
74
74
  },
75
75
  "peerDependencies": {
76
76
  "react": "^18.2.0",
@@ -0,0 +1,24 @@
1
+ import { intlayerIntlConfiguration } from '@intlayer/config/client';
2
+ import type { FC } from 'react';
3
+ import {
4
+ IntlayerClientProvider,
5
+ type IntlayerClientProviderProps,
6
+ } from './client/IntlayerClientProvider';
7
+ import {
8
+ IntlayerServerProvider,
9
+ type IntlayerServerProviderProps,
10
+ } from './server/IntlayerServerProvider';
11
+
12
+ const defaultLocale = intlayerIntlConfiguration.defaultLocale;
13
+
14
+ type IntlayerProviderProps = IntlayerClientProviderProps &
15
+ IntlayerServerProviderProps;
16
+
17
+ export const IntlayerProvider: FC<IntlayerProviderProps> = ({
18
+ children,
19
+ locale = defaultLocale,
20
+ }) => (
21
+ <IntlayerServerProvider locale={locale}>
22
+ <IntlayerClientProvider locale={locale}>{children}</IntlayerClientProvider>
23
+ </IntlayerServerProvider>
24
+ );
@@ -0,0 +1,45 @@
1
+ 'use client';
2
+
3
+ import {
4
+ intlayerIntlConfiguration,
5
+ type Locales,
6
+ } from '@intlayer/config/client';
7
+ import {
8
+ type PropsWithChildren,
9
+ createContext,
10
+ useContext,
11
+ type FC,
12
+ } from 'react';
13
+ import { localeCookie } from './useLocaleCookie';
14
+
15
+ type IntlayerValue = {
16
+ locale: Locales;
17
+ };
18
+
19
+ /**
20
+ * Context that store the current locale on the client side
21
+ */
22
+ export const IntlayerClientContext = createContext<IntlayerValue>({
23
+ locale: localeCookie ?? intlayerIntlConfiguration.defaultLocale,
24
+ });
25
+
26
+ /**
27
+ * Hook that provides the current locale
28
+ */
29
+ export const useIntlayerContext = () => useContext(IntlayerClientContext);
30
+
31
+ export type IntlayerClientProviderProps = PropsWithChildren & {
32
+ locale: Locales;
33
+ };
34
+
35
+ /**
36
+ * Provider that store the current locale on the client side
37
+ */
38
+ export const IntlayerClientProvider: FC<IntlayerClientProviderProps> = ({
39
+ locale,
40
+ children,
41
+ }) => (
42
+ <IntlayerClientContext.Provider value={{ locale }}>
43
+ {children}
44
+ </IntlayerClientContext.Provider>
45
+ );
@@ -1,9 +1,9 @@
1
- export type { LocaleClientContextProviderProps } from './LocaleClientContextProvider';
1
+ export type { IntlayerClientProviderProps } from './IntlayerClientProvider';
2
2
  export {
3
- LocaleClientContext,
4
- useLocaleContext,
5
- LocaleClientContextProvider,
6
- } from './LocaleClientContextProvider';
3
+ IntlayerClientContext as IntlayerClient,
4
+ useIntlayerContext,
5
+ IntlayerClientProvider,
6
+ } from './IntlayerClientProvider';
7
7
  export { useIntlayer } from './useIntlayer';
8
8
  export { useLocale } from './useLocale';
9
9
  export { useTraduction } from './useTraduction';
@@ -2,6 +2,9 @@ import type { LanguageContent } from '@intlayer/core';
2
2
  import { useLocale } from './useLocale';
3
3
  import { useTraduction } from './useTraduction';
4
4
 
5
+ /**
6
+ * On the client side, hook to get the translation content based on the locale
7
+ */
5
8
  export const useContent = <Content>(
6
9
  languageContent: LanguageContent<Content>
7
10
  ) => {
@@ -7,13 +7,18 @@ import {
7
7
  useIntlayerBase,
8
8
  type UseIntlayer,
9
9
  } from '../useIntlayerBase';
10
- import { LocaleClientContext } from './LocaleClientContextProvider';
10
+ import { IntlayerClientContext } from './IntlayerClientProvider';
11
11
 
12
+ /**
13
+ * On the client side, Hook that picking one dictionary by its id and return the content
14
+ *
15
+ * If the locale is not provided, it will use the locale from the client context
16
+ */
12
17
  export const useIntlayer: UseIntlayer = <T extends DictionaryKeys>(
13
18
  id: T,
14
19
  locale?: Locales
15
20
  ) => {
16
- const { locale: currentLocale } = useContext(LocaleClientContext);
21
+ const { locale: currentLocale } = useContext(IntlayerClientContext);
17
22
  const localeTarget = locale ?? currentLocale;
18
23
 
19
24
  return useIntlayerBase(id, localeTarget);
@@ -3,12 +3,15 @@
3
3
  import { intlayerIntlConfiguration } from '@intlayer/config/client';
4
4
  import { localeList } from '@intlayer/core';
5
5
  import { useContext } from 'react';
6
- import { LocaleClientContext } from './LocaleClientContextProvider';
6
+ import { IntlayerClientContext } from './IntlayerClientProvider';
7
7
 
8
8
  const { defaultLocale, locales: availableLocales } = intlayerIntlConfiguration;
9
9
 
10
+ /**
11
+ * On the client side, hook to get the current locale and all related fields
12
+ */
10
13
  export const useLocale = () => {
11
- const { locale } = useContext(LocaleClientContext);
14
+ const { locale } = useContext(IntlayerClientContext);
12
15
 
13
16
  return {
14
17
  locale, // Current locale
@@ -1,4 +1,3 @@
1
- /* eslint-disable @typescript-eslint/no-redundant-type-constituents */
2
1
  import {
3
2
  type Locales,
4
3
  intlayerMiddlewareConfiguration,
@@ -15,12 +14,21 @@ const cookieAttributes: Cookies.CookieAttributes = {
15
14
  sameSite: 'strict',
16
15
  };
17
16
 
17
+ /**
18
+ * Get the locale cookie
19
+ */
18
20
  export const localeCookie = Cookies.get(cookieName) as Locales | undefined;
19
21
 
22
+ /**
23
+ * Set the locale cookie
24
+ */
20
25
  export const setLocaleCookie = (locale: Locales) => {
21
26
  Cookies.set(cookieName, locale, cookieAttributes);
22
27
  };
23
28
 
29
+ /**
30
+ * Hook that provides the locale cookie and a function to set it
31
+ */
24
32
  export const useLocaleCookie = () => ({
25
33
  localeCookie,
26
34
  setLocaleCookie,
@@ -1,12 +1,32 @@
1
1
  import type { LanguageContent } from '@intlayer/core';
2
2
  import { useContext } from 'react';
3
3
  import { getTranslation } from '../getTranslation';
4
- import { LocaleClientContext } from './LocaleClientContextProvider';
4
+ import { IntlayerClientContext } from './IntlayerClientProvider';
5
5
 
6
- export const useTraduction = <Content>(
6
+ /**
7
+ * On the client side, Hook that picking one dictionary by its id and return the content.
8
+ *
9
+ * If not locale found, it will return the content related to the default locale.
10
+ *
11
+ * Return either the content editor, or the content itself depending on the configuration.
12
+ *
13
+ * Usage:
14
+ *
15
+ * const content = getTranslation<string>({
16
+ * en: 'Hello',
17
+ * fr: 'Bonjour',
18
+ * },
19
+ * 'fr');
20
+ * // 'Bonjour'
21
+ *
22
+ * Using TypeScript:
23
+ * - this function will require each locale to be defined if defined in the project configuration.
24
+ * - If a locale is missing, it will make each existing locale optional and raise an error if the locale is not found.
25
+ */
26
+ export const useTraduction = <Content = string>(
7
27
  languageContent: LanguageContent<Content>
8
28
  ): Content => {
9
- const { locale } = useContext(LocaleClientContext);
29
+ const { locale } = useContext(IntlayerClientContext);
10
30
 
11
31
  return getTranslation(languageContent, locale);
12
32
  };
@@ -1,6 +1,31 @@
1
1
  import { type QuantityContent, getEnumerationContent } from '@intlayer/core';
2
2
  import { contentRender } from './ContentEditor/contentRender';
3
3
 
4
+ /**
5
+ * Allow to pick a content based on a quantity.
6
+ *
7
+ * Return either the content editor, or the content itself depending on the configuration.
8
+ *
9
+ * Usage:
10
+ *
11
+ * const content = getEnumeration({
12
+ * '<=-2.3': 'You have less than -2.3',
13
+ * '<1': 'You have less than one',
14
+ * '2': 'You have two',
15
+ * '>=3': 'You have three or more',
16
+ * },
17
+ * 2);
18
+ * // 'You have two'
19
+ *
20
+ * The order of the keys will define the priority of the content.
21
+ *
22
+ * const content = getEnumeration({
23
+ * '<4': 'You have less than four',
24
+ * '2': 'You have two',
25
+ * });
26
+ * // 'You have less than four'
27
+ *
28
+ */
4
29
  export const getEnumeration = <Content>(
5
30
  enumerationContent: QuantityContent<Content>,
6
31
  quantity: number