react-intlayer 1.0.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 (163) hide show
  1. package/LICENSE +21 -0
  2. package/dist/cjs/ContentEditor/ContentEditor.cjs +113 -0
  3. package/dist/cjs/ContentEditor/ContentEditor.cjs.map +1 -0
  4. package/dist/cjs/ContentEditor/ContentEditor.d.ts +8 -0
  5. package/dist/cjs/ContentEditor/contentRender.cjs +45 -0
  6. package/dist/cjs/ContentEditor/contentRender.cjs.map +1 -0
  7. package/dist/cjs/ContentEditor/contentRender.d.ts +3 -0
  8. package/dist/cjs/ContentEditor/index.cjs +41 -0
  9. package/dist/cjs/ContentEditor/index.cjs.map +1 -0
  10. package/dist/cjs/ContentEditor/index.d.ts +3 -0
  11. package/dist/cjs/LocaleContextProvider.cjs +49 -0
  12. package/dist/cjs/LocaleContextProvider.cjs.map +1 -0
  13. package/dist/cjs/LocaleContextProvider.d.ts +10 -0
  14. package/dist/cjs/client/LocaleClientContextProvider.cjs +54 -0
  15. package/dist/cjs/client/LocaleClientContextProvider.cjs.map +1 -0
  16. package/dist/cjs/client/LocaleClientContextProvider.d.ts +20 -0
  17. package/dist/cjs/client/index.cjs +56 -0
  18. package/dist/cjs/client/index.cjs.map +1 -0
  19. package/dist/cjs/client/index.d.ts +19 -0
  20. package/dist/cjs/client/useContent.cjs +44 -0
  21. package/dist/cjs/client/useContent.cjs.map +1 -0
  22. package/dist/cjs/client/useContent.d.ts +12 -0
  23. package/dist/cjs/client/useIntlayer.cjs +44 -0
  24. package/dist/cjs/client/useIntlayer.cjs.map +1 -0
  25. package/dist/cjs/client/useIntlayer.d.ts +7 -0
  26. package/dist/cjs/client/useLocale.cjs +55 -0
  27. package/dist/cjs/client/useLocale.cjs.map +1 -0
  28. package/dist/cjs/client/useLocale.d.ts +10 -0
  29. package/dist/cjs/client/useLocaleCookie.cjs +70 -0
  30. package/dist/cjs/client/useLocaleCookie.cjs.map +1 -0
  31. package/dist/cjs/client/useLocaleCookie.d.ts +10 -0
  32. package/dist/cjs/client/useTraduction.cjs +42 -0
  33. package/dist/cjs/client/useTraduction.cjs.map +1 -0
  34. package/dist/cjs/client/useTraduction.d.ts +7 -0
  35. package/dist/cjs/getEnumeration.cjs +45 -0
  36. package/dist/cjs/getEnumeration.cjs.map +1 -0
  37. package/dist/cjs/getEnumeration.d.ts +8 -0
  38. package/dist/cjs/getTranslation.cjs +47 -0
  39. package/dist/cjs/getTranslation.cjs.map +1 -0
  40. package/dist/cjs/getTranslation.d.ts +9 -0
  41. package/dist/cjs/index.cjs +53 -0
  42. package/dist/cjs/index.cjs.map +1 -0
  43. package/dist/cjs/index.d.ts +20 -0
  44. package/dist/cjs/processDictionary/contentDictionary.d.cjs +21 -0
  45. package/dist/cjs/processDictionary/contentDictionary.d.cjs.map +1 -0
  46. package/dist/cjs/processDictionary/contentDictionary.d.d.ts +32 -0
  47. package/dist/cjs/processDictionary/index.cjs +82 -0
  48. package/dist/cjs/processDictionary/index.cjs.map +1 -0
  49. package/dist/cjs/processDictionary/index.d.ts +22 -0
  50. package/dist/cjs/server/LocaleServerContextProvider.cjs +49 -0
  51. package/dist/cjs/server/LocaleServerContextProvider.cjs.map +1 -0
  52. package/dist/cjs/server/LocaleServerContextProvider.d.ts +34 -0
  53. package/dist/cjs/server/getLocaleTranslation.cjs +50 -0
  54. package/dist/cjs/server/getLocaleTranslation.cjs.map +1 -0
  55. package/dist/cjs/server/getLocaleTranslation.d.ts +11 -0
  56. package/dist/cjs/server/index.cjs +49 -0
  57. package/dist/cjs/server/index.cjs.map +1 -0
  58. package/dist/cjs/server/index.d.ts +13 -0
  59. package/dist/cjs/server/serverContext.cjs +81 -0
  60. package/dist/cjs/server/serverContext.cjs.map +1 -0
  61. package/dist/cjs/server/serverContext.d.ts +43 -0
  62. package/dist/cjs/server/useIntlayer.cjs +44 -0
  63. package/dist/cjs/server/useIntlayer.cjs.map +1 -0
  64. package/dist/cjs/server/useIntlayer.d.ts +7 -0
  65. package/dist/cjs/server/useTraduction.cjs +42 -0
  66. package/dist/cjs/server/useTraduction.cjs.map +1 -0
  67. package/dist/cjs/server/useTraduction.d.ts +7 -0
  68. package/dist/cjs/useIntlayerBase.cjs +56 -0
  69. package/dist/cjs/useIntlayerBase.cjs.map +1 -0
  70. package/dist/cjs/useIntlayerBase.d.ts +23 -0
  71. package/dist/esm/ContentEditor/ContentEditor.d.mts +8 -0
  72. package/dist/esm/ContentEditor/ContentEditor.mjs +82 -0
  73. package/dist/esm/ContentEditor/ContentEditor.mjs.map +1 -0
  74. package/dist/esm/ContentEditor/contentRender.d.mts +3 -0
  75. package/dist/esm/ContentEditor/contentRender.mjs +11 -0
  76. package/dist/esm/ContentEditor/contentRender.mjs.map +1 -0
  77. package/dist/esm/ContentEditor/index.d.mts +3 -0
  78. package/dist/esm/ContentEditor/index.mjs +3 -0
  79. package/dist/esm/ContentEditor/index.mjs.map +1 -0
  80. package/dist/esm/LocaleContextProvider.d.mts +10 -0
  81. package/dist/esm/LocaleContextProvider.mjs +15 -0
  82. package/dist/esm/LocaleContextProvider.mjs.map +1 -0
  83. package/dist/esm/client/LocaleClientContextProvider.d.mts +20 -0
  84. package/dist/esm/client/LocaleClientContextProvider.mjs +16 -0
  85. package/dist/esm/client/LocaleClientContextProvider.mjs.map +1 -0
  86. package/dist/esm/client/index.d.mts +19 -0
  87. package/dist/esm/client/index.mjs +25 -0
  88. package/dist/esm/client/index.mjs.map +1 -0
  89. package/dist/esm/client/useContent.d.mts +12 -0
  90. package/dist/esm/client/useContent.mjs +13 -0
  91. package/dist/esm/client/useContent.mjs.map +1 -0
  92. package/dist/esm/client/useIntlayer.d.mts +7 -0
  93. package/dist/esm/client/useIntlayer.mjs +11 -0
  94. package/dist/esm/client/useIntlayer.mjs.map +1 -0
  95. package/dist/esm/client/useLocale.d.mts +10 -0
  96. package/dist/esm/client/useLocale.mjs +21 -0
  97. package/dist/esm/client/useLocale.mjs.map +1 -0
  98. package/dist/esm/client/useLocaleCookie.d.mts +10 -0
  99. package/dist/esm/client/useLocaleCookie.mjs +20 -0
  100. package/dist/esm/client/useLocaleCookie.mjs.map +1 -0
  101. package/dist/esm/client/useTraduction.d.mts +7 -0
  102. package/dist/esm/client/useTraduction.mjs +9 -0
  103. package/dist/esm/client/useTraduction.mjs.map +1 -0
  104. package/dist/esm/getEnumeration.d.mts +8 -0
  105. package/dist/esm/getEnumeration.mjs +11 -0
  106. package/dist/esm/getEnumeration.mjs.map +1 -0
  107. package/dist/esm/getTranslation.d.mts +9 -0
  108. package/dist/esm/getTranslation.mjs +16 -0
  109. package/dist/esm/getTranslation.mjs.map +1 -0
  110. package/dist/esm/index.d.mts +20 -0
  111. package/dist/esm/index.mjs +25 -0
  112. package/dist/esm/index.mjs.map +1 -0
  113. package/dist/esm/processDictionary/contentDictionary.d.d.mts +32 -0
  114. package/dist/esm/processDictionary/contentDictionary.d.mjs +1 -0
  115. package/dist/esm/processDictionary/contentDictionary.d.mjs.map +1 -0
  116. package/dist/esm/processDictionary/index.d.mts +22 -0
  117. package/dist/esm/processDictionary/index.mjs +43 -0
  118. package/dist/esm/processDictionary/index.mjs.map +1 -0
  119. package/dist/esm/server/LocaleServerContextProvider.d.mts +34 -0
  120. package/dist/esm/server/LocaleServerContextProvider.mjs +14 -0
  121. package/dist/esm/server/LocaleServerContextProvider.mjs.map +1 -0
  122. package/dist/esm/server/getLocaleTranslation.d.mts +11 -0
  123. package/dist/esm/server/getLocaleTranslation.mjs +17 -0
  124. package/dist/esm/server/getLocaleTranslation.mjs.map +1 -0
  125. package/dist/esm/server/index.d.mts +13 -0
  126. package/dist/esm/server/index.mjs +17 -0
  127. package/dist/esm/server/index.mjs.map +1 -0
  128. package/dist/esm/server/serverContext.d.mts +43 -0
  129. package/dist/esm/server/serverContext.mjs +33 -0
  130. package/dist/esm/server/serverContext.mjs.map +1 -0
  131. package/dist/esm/server/useIntlayer.d.mts +7 -0
  132. package/dist/esm/server/useIntlayer.mjs +9 -0
  133. package/dist/esm/server/useIntlayer.mjs.map +1 -0
  134. package/dist/esm/server/useTraduction.d.mts +7 -0
  135. package/dist/esm/server/useTraduction.mjs +9 -0
  136. package/dist/esm/server/useTraduction.mjs.map +1 -0
  137. package/dist/esm/useIntlayerBase.d.mts +23 -0
  138. package/dist/esm/useIntlayerBase.mjs +8 -0
  139. package/dist/esm/useIntlayerBase.mjs.map +1 -0
  140. package/package.json +95 -0
  141. package/src/ContentEditor/ContentEditor.tsx +116 -0
  142. package/src/ContentEditor/contentRender.tsx +11 -0
  143. package/src/ContentEditor/index.tsx +2 -0
  144. package/src/LocaleContextProvider.tsx +26 -0
  145. package/src/client/LocaleClientContextProvider.tsx +35 -0
  146. package/src/client/index.ts +14 -0
  147. package/src/client/useContent.ts +17 -0
  148. package/src/client/useIntlayer.ts +20 -0
  149. package/src/client/useLocale.ts +19 -0
  150. package/src/client/useLocaleCookie.ts +27 -0
  151. package/src/client/useTraduction.ts +12 -0
  152. package/src/getEnumeration.ts +18 -0
  153. package/src/getTranslation.ts +24 -0
  154. package/src/index.ts +14 -0
  155. package/src/processDictionary/contentDictionary.d.ts +27 -0
  156. package/src/processDictionary/index.ts +97 -0
  157. package/src/server/LocaleServerContextProvider.tsx +24 -0
  158. package/src/server/getLocaleTranslation.ts +19 -0
  159. package/src/server/index.ts +8 -0
  160. package/src/server/serverContext.ts +85 -0
  161. package/src/server/useIntlayer.ts +17 -0
  162. package/src/server/useTraduction.ts +13 -0
  163. package/src/useIntlayerBase.ts +32 -0
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Creates a new datastore for a given server context.
3
+ * Attempts to closely mimic the `createContext` API.
4
+ *
5
+ * @example
6
+ * const LocaleServerContext = createServerContext<string | null>(null);
7
+ *
8
+ * <LocaleServerContext.Provider value={locale}>
9
+ * {children}
10
+ * </LocaleServerContext.Provider>
11
+ */
12
+ declare const createServerContext: <T>(defaultValue?: T) => ServerContext<T>;
13
+ /**
14
+ * Fetches a value present in a given server context.
15
+ * Attempts to closely mimic the `useContext` API.
16
+ *
17
+ * @example
18
+ * getServerContext(LocaleServerContext);
19
+ */
20
+ declare const getServerContext: <T>({
21
+ _storage,
22
+ _defaultValue,
23
+ }: ServerContext<T>) => T | undefined;
24
+ 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;
41
+ };
42
+
43
+ export { createServerContext, getServerContext };
@@ -0,0 +1,33 @@
1
+ import react from "react";
2
+ const cacheFallback = () => () => ({ value: void 0 });
3
+ const createServerContext = (defaultValue) => {
4
+ throwInClient();
5
+ const cache = react.cache ?? cacheFallback;
6
+ const getCache = cache(() => ({
7
+ value: void 0,
8
+ }));
9
+ return {
10
+ Provider: ({ children, value }) => {
11
+ getCache().value = value;
12
+ return children;
13
+ },
14
+ Consumer: ({ children }) => {
15
+ const store = getCache();
16
+ return children(store ? store.value : defaultValue);
17
+ },
18
+ _storage: getCache,
19
+ _defaultValue: defaultValue,
20
+ };
21
+ };
22
+ const getServerContext = ({ _storage, _defaultValue }) => {
23
+ const store = _storage();
24
+ if (!store) return _defaultValue;
25
+ return store.value;
26
+ };
27
+ const throwInClient = () => {
28
+ if (typeof window !== "undefined") {
29
+ throw new Error(`createServerContext only works in Server Components`);
30
+ }
31
+ };
32
+ export { createServerContext, getServerContext };
33
+ //# sourceMappingURL=serverContext.mjs.map
@@ -0,0 +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":[]}
@@ -0,0 +1,7 @@
1
+ import { UseIntlayer } from "../useIntlayerBase.mjs";
2
+ import "@intlayer/config/client";
3
+ import "intlayer";
4
+
5
+ declare const useIntlayer: UseIntlayer;
6
+
7
+ export { useIntlayer };
@@ -0,0 +1,9 @@
1
+ import { useIntlayerBase } from "../useIntlayerBase.mjs";
2
+ import { LocaleServerContext } from "./LocaleServerContextProvider.mjs";
3
+ import { getServerContext } from "./serverContext.mjs";
4
+ const useIntlayer = (id, locale) => {
5
+ const localeTarget = locale ?? getServerContext(LocaleServerContext);
6
+ return useIntlayerBase(id, localeTarget);
7
+ };
8
+ export { useIntlayer };
9
+ //# sourceMappingURL=useIntlayer.mjs.map
@@ -0,0 +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":[]}
@@ -0,0 +1,7 @@
1
+ import { LanguageContent } from "@intlayer/core";
2
+
3
+ declare const useTraduction: <Content>(
4
+ languageContent: LanguageContent<Content>
5
+ ) => Content;
6
+
7
+ export { useTraduction };
@@ -0,0 +1,9 @@
1
+ import { getTranslation } from "../getTranslation.mjs";
2
+ import { LocaleServerContext } from "./LocaleServerContextProvider.mjs";
3
+ import { getServerContext } from "./serverContext.mjs";
4
+ const useTraduction = (languageContent) => {
5
+ const locale = getServerContext(LocaleServerContext);
6
+ return getTranslation(languageContent, locale);
7
+ };
8
+ export { useTraduction };
9
+ //# sourceMappingURL=useTraduction.mjs.map
@@ -0,0 +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":[]}
@@ -0,0 +1,23 @@
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
+ */
9
+
10
+ type StringFallback<T> = T extends never ? string : T;
11
+ type DictionaryKeys = StringFallback<keyof IntlayerDictionaryTypesConnector>;
12
+ type UseIntlayer = <T extends DictionaryKeys>(
13
+ id: T,
14
+ locale?: Locales
15
+ ) => IntlayerDictionaryTypesConnector[T];
16
+ declare const useIntlayerBase: UseIntlayer;
17
+
18
+ export {
19
+ type DictionaryKeys,
20
+ type StringFallback,
21
+ type UseIntlayer,
22
+ useIntlayerBase,
23
+ };
@@ -0,0 +1,8 @@
1
+ import dictionaries from "@intlayer/dictionaries-entry";
2
+ import { processDictionary } from "./processDictionary/index.mjs";
3
+ const useIntlayerBase = (id, locale) => {
4
+ const dictionary = dictionaries[id];
5
+ return processDictionary(dictionary, locale);
6
+ };
7
+ export { useIntlayerBase };
8
+ //# sourceMappingURL=useIntlayerBase.mjs.map
@@ -0,0 +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":[]}
package/package.json ADDED
@@ -0,0 +1,95 @@
1
+ {
2
+ "name": "react-intlayer",
3
+ "version": "1.0.0",
4
+ "private": false,
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
+ "keywords": [
7
+ "intlayer",
8
+ "layer",
9
+ "abstraction",
10
+ "data",
11
+ "internationalization",
12
+ "i18n",
13
+ "typescript",
14
+ "javascript",
15
+ "react",
16
+ "json",
17
+ "file"
18
+ ],
19
+ "homepage": "https://github.com/aypineau/intlayer",
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "https://github.com/aypineau/intlayer"
23
+ },
24
+ "license": "MIT",
25
+ "author": {
26
+ "name": "Aymeric PINEAU",
27
+ "url": "https://github.com/aypineau"
28
+ },
29
+ "exports": {
30
+ ".": {
31
+ "types": "./dist/esm/index.d.mts",
32
+ "require": "./dist/cjs/index.cjs",
33
+ "import": "./dist/esm/index.mjs"
34
+ },
35
+ "./server": {
36
+ "types": "./dist/esm/server/index.d.mts",
37
+ "require": "./dist/cjs/server/index.cjs",
38
+ "import": "./dist/esm/server/index.mjs"
39
+ },
40
+ "./package.json": "./package.json"
41
+ },
42
+ "main": "./src/index.ts",
43
+ "typesVersions": {
44
+ "*": {
45
+ "package.json": [
46
+ "./package.json"
47
+ ]
48
+ }
49
+ },
50
+ "files": [
51
+ "./dist",
52
+ "./src",
53
+ "./package.json"
54
+ ],
55
+ "dependencies": {
56
+ "js-cookie": "^3.0.5",
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"
62
+ },
63
+ "devDependencies": {
64
+ "@types/js-cookie": "^3.0.6",
65
+ "@types/node": "^20.12.7",
66
+ "@types/react": "^18.2.78",
67
+ "react": "^18.2.0",
68
+ "react-dom": "^18.2.0",
69
+ "rimraf": "5.0.5",
70
+ "tsup": "^8.0.2",
71
+ "typescript": "^5.4.3",
72
+ "@utils/eslint-config": "^1.0.0",
73
+ "@utils/ts-config": "^1.0.0"
74
+ },
75
+ "peerDependencies": {
76
+ "react": "^18.2.0",
77
+ "react-dom": "^18.2.0"
78
+ },
79
+ "engines": {
80
+ "node": ">=14.18"
81
+ },
82
+ "bug": {
83
+ "url": "https://github.com/aypineau/intlayer/issues"
84
+ },
85
+ "scripts": {
86
+ "build": "tsup",
87
+ "clean": "rimraf ./dist",
88
+ "dev": "tsup --watch",
89
+ "lint": "eslint . --ext .ts,.tsx,.js,.jsx,.cjs,.mjs",
90
+ "lint:fix": "eslint . --ext .ts,.tsx,.js,.jsx,.cjs,.mjs --fix",
91
+ "prettier:fix": "prettier --write src/**/*",
92
+ "test": "",
93
+ "typecheck": "tsup--project ./tsconfig.json --noEmit"
94
+ }
95
+ }
@@ -0,0 +1,116 @@
1
+ 'use client';
2
+
3
+ import {
4
+ useEffect,
5
+ useState,
6
+ useRef,
7
+ useCallback,
8
+ type FC,
9
+ type MouseEventHandler,
10
+ } from 'react';
11
+
12
+ type ContentEditorProps = {
13
+ children?: string;
14
+ };
15
+
16
+ const PRESS_DETECT_DURATION = 500;
17
+
18
+ export const ContentEditor: FC<ContentEditorProps> = ({ children }) => {
19
+ const divRef = useRef<HTMLDivElement>(null);
20
+ const [isEditing, setIsEditing] = useState(false);
21
+ const pressTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
22
+
23
+ const handleOnLongPress = () => {
24
+ setIsEditing(true);
25
+ };
26
+
27
+ const startPressTimer = () => {
28
+ pressTimerRef.current = setTimeout(() => {
29
+ handleOnLongPress();
30
+ }, PRESS_DETECT_DURATION);
31
+ };
32
+
33
+ const clearPressTimer = () => {
34
+ if (pressTimerRef.current) {
35
+ clearTimeout(pressTimerRef.current);
36
+ pressTimerRef.current = null;
37
+ }
38
+ };
39
+
40
+ const handleMouseDown = () => {
41
+ clearPressTimer(); // Ensure any previous timer is cleared
42
+ startPressTimer();
43
+ };
44
+
45
+ const handleMouseUp = () => {
46
+ clearPressTimer();
47
+ };
48
+
49
+ // Use useCallback to ensure the function identity remains stable
50
+ const handleClickOutside = useCallback(
51
+ (event: MouseEvent) => {
52
+ if (divRef.current && !divRef.current.contains(event.target as Node)) {
53
+ setIsEditing(false);
54
+ }
55
+ },
56
+ [divRef]
57
+ );
58
+
59
+ useEffect(() => {
60
+ // Attach click outside listener
61
+ document.addEventListener('mousedown', handleClickOutside);
62
+
63
+ return () => {
64
+ // Cleanup
65
+ document.removeEventListener('mousedown', handleClickOutside);
66
+ clearPressTimer(); // Ensure to clear the timer when component unmounts
67
+ };
68
+ }, [handleClickOutside]);
69
+
70
+ const handleOnClick: MouseEventHandler<HTMLDivElement> = (e) => {
71
+ if (isEditing) {
72
+ e.preventDefault();
73
+ e.stopPropagation();
74
+ }
75
+ };
76
+
77
+ const handleOnBlur = () => {
78
+ // Stop editing when the element loses focus
79
+ setIsEditing(false);
80
+ };
81
+
82
+ const onContentChange = (_e: React.FormEvent<HTMLDivElement>) => {
83
+ // console.log(e.currentTarget.textContent);
84
+ };
85
+
86
+ return (
87
+ <div
88
+ role="textbox"
89
+ tabIndex={0}
90
+ onKeyUp={() => null}
91
+ contentEditable={isEditing}
92
+ onInput={onContentChange}
93
+ onClick={handleOnClick}
94
+ onMouseDown={handleMouseDown}
95
+ onMouseUp={handleMouseUp}
96
+ onMouseLeave={handleMouseUp}
97
+ onTouchStart={handleMouseDown}
98
+ onTouchEnd={handleMouseUp}
99
+ onTouchCancel={handleMouseUp}
100
+ onBlur={handleOnBlur}
101
+ suppressContentEditableWarning={true} // To suppress the warning for controlled components
102
+ style={
103
+ isEditing
104
+ ? {
105
+ backgroundColor: 'transparent',
106
+ cursor: 'text',
107
+ display: 'inline',
108
+ }
109
+ : { cursor: 'pointer', display: 'inline' }
110
+ }
111
+ ref={divRef}
112
+ >
113
+ {children}
114
+ </div>
115
+ );
116
+ };
@@ -0,0 +1,11 @@
1
+ import { ContentEditor } from './ContentEditor';
2
+
3
+ export const contentRender = (content: string) => {
4
+ const isEditable = process.env.NODE_ENV === 'development';
5
+
6
+ if (isEditable && ContentEditor) {
7
+ return (<ContentEditor>{content}</ContentEditor>) as unknown as string;
8
+ }
9
+
10
+ return content;
11
+ };
@@ -0,0 +1,2 @@
1
+ export * from './ContentEditor';
2
+ export * from './contentRender';
@@ -0,0 +1,26 @@
1
+ import { intlayerIntlConfiguration } from '@intlayer/config/client';
2
+ import React, { type FC } from 'react';
3
+ import {
4
+ LocaleClientContextProvider,
5
+ type LocaleClientContextProviderProps,
6
+ } from './client/LocaleClientContextProvider';
7
+ import {
8
+ LocaleServerContextProvider,
9
+ type LocaleServerContextProviderProps,
10
+ } from './server/LocaleServerContextProvider';
11
+
12
+ const defaultLocale = intlayerIntlConfiguration.defaultLocale;
13
+
14
+ type LocaleContextProviderProps = LocaleClientContextProviderProps &
15
+ LocaleServerContextProviderProps;
16
+
17
+ export const LocaleContextProvider: FC<LocaleContextProviderProps> = ({
18
+ children,
19
+ locale = defaultLocale,
20
+ }) => (
21
+ <LocaleServerContextProvider locale={locale}>
22
+ <LocaleClientContextProvider locale={locale}>
23
+ {children}
24
+ </LocaleClientContextProvider>
25
+ </LocaleServerContextProvider>
26
+ );
@@ -0,0 +1,35 @@
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 LocaleContextValue = {
16
+ locale: Locales;
17
+ };
18
+
19
+ export const LocaleClientContext = createContext<LocaleContextValue>({
20
+ locale: localeCookie ?? intlayerIntlConfiguration.defaultLocale,
21
+ });
22
+
23
+ export const useLocaleContext = () => useContext(LocaleClientContext);
24
+
25
+ export type LocaleClientContextProviderProps = PropsWithChildren & {
26
+ locale: Locales;
27
+ };
28
+
29
+ export const LocaleClientContextProvider: FC<
30
+ LocaleClientContextProviderProps
31
+ > = ({ locale, children }) => (
32
+ <LocaleClientContext.Provider value={{ locale }}>
33
+ {children}
34
+ </LocaleClientContext.Provider>
35
+ );
@@ -0,0 +1,14 @@
1
+ export type { LocaleClientContextProviderProps } from './LocaleClientContextProvider';
2
+ export {
3
+ LocaleClientContext,
4
+ useLocaleContext,
5
+ LocaleClientContextProvider,
6
+ } from './LocaleClientContextProvider';
7
+ export { useIntlayer } from './useIntlayer';
8
+ export { useLocale } from './useLocale';
9
+ export { useTraduction } from './useTraduction';
10
+ export {
11
+ useLocaleCookie,
12
+ localeCookie,
13
+ setLocaleCookie,
14
+ } from './useLocaleCookie';
@@ -0,0 +1,17 @@
1
+ import type { LanguageContent } from '@intlayer/core';
2
+ import { useLocale } from './useLocale';
3
+ import { useTraduction } from './useTraduction';
4
+
5
+ export const useContent = <Content>(
6
+ languageContent: LanguageContent<Content>
7
+ ) => {
8
+ const { locale } = useLocale();
9
+
10
+ const content = useTraduction(languageContent);
11
+
12
+ return {
13
+ locale,
14
+ content,
15
+ t: useTraduction,
16
+ };
17
+ };
@@ -0,0 +1,20 @@
1
+ 'use client';
2
+
3
+ import type { Locales } from '@intlayer/config/client';
4
+ import { useContext } from 'react';
5
+ import {
6
+ type DictionaryKeys,
7
+ useIntlayerBase,
8
+ type UseIntlayer,
9
+ } from '../useIntlayerBase';
10
+ import { LocaleClientContext } from './LocaleClientContextProvider';
11
+
12
+ export const useIntlayer: UseIntlayer = <T extends DictionaryKeys>(
13
+ id: T,
14
+ locale?: Locales
15
+ ) => {
16
+ const { locale: currentLocale } = useContext(LocaleClientContext);
17
+ const localeTarget = locale ?? currentLocale;
18
+
19
+ return useIntlayerBase(id, localeTarget);
20
+ };
@@ -0,0 +1,19 @@
1
+ 'use client';
2
+
3
+ import { intlayerIntlConfiguration } from '@intlayer/config/client';
4
+ import { localeList } from '@intlayer/core';
5
+ import { useContext } from 'react';
6
+ import { LocaleClientContext } from './LocaleClientContextProvider';
7
+
8
+ const { defaultLocale, locales: availableLocales } = intlayerIntlConfiguration;
9
+
10
+ export const useLocale = () => {
11
+ const { locale } = useContext(LocaleClientContext);
12
+
13
+ return {
14
+ locale, // Current locale
15
+ defaultLocale, // Principal locale defined in config
16
+ availableLocales, // List of the available locales defined in config
17
+ localeList, // List of all available locales
18
+ };
19
+ };
@@ -0,0 +1,27 @@
1
+ /* eslint-disable @typescript-eslint/no-redundant-type-constituents */
2
+ import {
3
+ type Locales,
4
+ intlayerMiddlewareConfiguration,
5
+ } from '@intlayer/config/client';
6
+ import Cookies from 'js-cookie';
7
+
8
+ const { cookieName } = intlayerMiddlewareConfiguration;
9
+
10
+ const cookieAttributes: Cookies.CookieAttributes = {
11
+ path: '/',
12
+ expires: undefined,
13
+ domain: undefined,
14
+ secure: false,
15
+ sameSite: 'strict',
16
+ };
17
+
18
+ export const localeCookie = Cookies.get(cookieName) as Locales | undefined;
19
+
20
+ export const setLocaleCookie = (locale: Locales) => {
21
+ Cookies.set(cookieName, locale, cookieAttributes);
22
+ };
23
+
24
+ export const useLocaleCookie = () => ({
25
+ localeCookie,
26
+ setLocaleCookie,
27
+ });
@@ -0,0 +1,12 @@
1
+ import type { LanguageContent } from '@intlayer/core';
2
+ import { useContext } from 'react';
3
+ import { getTranslation } from '../getTranslation';
4
+ import { LocaleClientContext } from './LocaleClientContextProvider';
5
+
6
+ export const useTraduction = <Content>(
7
+ languageContent: LanguageContent<Content>
8
+ ): Content => {
9
+ const { locale } = useContext(LocaleClientContext);
10
+
11
+ return getTranslation(languageContent, locale);
12
+ };
@@ -0,0 +1,18 @@
1
+ import { type QuantityContent, getEnumerationContent } from '@intlayer/core';
2
+ import { contentRender } from './ContentEditor/contentRender';
3
+
4
+ export const getEnumeration = <Content>(
5
+ enumerationContent: QuantityContent<Content>,
6
+ quantity: number
7
+ ): Content => {
8
+ const result: Content = getEnumerationContent<Content>(
9
+ enumerationContent,
10
+ quantity
11
+ );
12
+
13
+ if (typeof result === 'string') {
14
+ return contentRender(result) as Content;
15
+ }
16
+
17
+ return result;
18
+ };
@@ -0,0 +1,24 @@
1
+ import {
2
+ type Locales,
3
+ intlayerIntlConfiguration,
4
+ } from '@intlayer/config/client';
5
+ import { type LanguageContent, getTranslationContent } from '@intlayer/core';
6
+ import { contentRender } from './ContentEditor/contentRender';
7
+
8
+ export const getTranslation = <Content>(
9
+ languageContent: LanguageContent<Content>,
10
+ locale?: Locales
11
+ ): Content => {
12
+ const { defaultLocale } = intlayerIntlConfiguration;
13
+
14
+ const result: Content = getTranslationContent<Content>(
15
+ languageContent,
16
+ locale ?? defaultLocale
17
+ );
18
+
19
+ if (typeof result === 'string') {
20
+ return contentRender(result) as Content;
21
+ }
22
+
23
+ return result;
24
+ };
package/src/index.ts ADDED
@@ -0,0 +1,14 @@
1
+ export { getTranslation } from './getTranslation';
2
+ export {
3
+ LocaleClientContextProvider,
4
+ type LocaleClientContextProviderProps,
5
+ LocaleClientContext,
6
+ useLocaleContext,
7
+ useIntlayer,
8
+ useLocale,
9
+ useTraduction,
10
+ useLocaleCookie,
11
+ localeCookie,
12
+ setLocaleCookie,
13
+ } from './client/index';
14
+ export {} from './client/useLocaleCookie';
@@ -0,0 +1,27 @@
1
+ import { type TranslationContent } from '@intlayer/core';
2
+
3
+ export type ContentValue =
4
+ | string
5
+ | {
6
+ [key: string]: ContentValue;
7
+ }
8
+ | TranslationContent<unknown>;
9
+
10
+ export type Content = Record<string, ContentValue | undefined>;
11
+
12
+ export type TransformedContentValue =
13
+ | string
14
+ | {
15
+ [key: string]: TransformedContentValue;
16
+ }
17
+ | undefined
18
+ | ((quantity: number) => TransformedContentValue);
19
+
20
+ export type TransformedContent = Record<
21
+ string,
22
+ TransformedContentValue | undefined
23
+ >;
24
+
25
+ export type ContentDictionary = Content & {
26
+ id: string;
27
+ };