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,97 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import {
3
+ type Locales,
4
+ intlayerIntlConfiguration,
5
+ } from '@intlayer/config/client';
6
+ import {
7
+ NodeType,
8
+ type QuantityContent,
9
+ type LanguageContent,
10
+ } from '@intlayer/core';
11
+ import { getEnumeration } from '../getEnumeration';
12
+ import { getTranslation } from '../getTranslation';
13
+ import type {
14
+ Content,
15
+ ContentValue,
16
+ TransformedContent,
17
+ TransformedContentValue,
18
+ } from './contentDictionary';
19
+
20
+ const defaultLocale = intlayerIntlConfiguration.defaultLocale;
21
+
22
+ const processTranslation = (
23
+ languageContent: LanguageContent<ContentValue>,
24
+ locale: Locales
25
+ ): TransformedContent => {
26
+ const translationResult: ContentValue = getTranslation<ContentValue>(
27
+ languageContent,
28
+ locale
29
+ );
30
+
31
+ return processDictionary(translationResult as Content, locale);
32
+ };
33
+
34
+ const processEnumeration = (
35
+ enumerationContent: QuantityContent<ContentValue>,
36
+ locale: Locales
37
+ ): TransformedContentValue => {
38
+ return (quantity: number): TransformedContentValue => {
39
+ const enumerationResult: ContentValue = getEnumeration<ContentValue>(
40
+ enumerationContent,
41
+ quantity
42
+ );
43
+
44
+ //
45
+ return processDictionary(enumerationResult as Content, locale);
46
+ };
47
+ };
48
+
49
+ export const processNode = (
50
+ field: ContentValue | undefined,
51
+ locale: Locales
52
+ ): TransformedContentValue => {
53
+ if (typeof field !== 'object' || field === null) {
54
+ return field;
55
+ }
56
+
57
+ if (field.nodeType === NodeType.Translation) {
58
+ return processTranslation(field as LanguageContent<ContentValue>, locale);
59
+ }
60
+
61
+ if (field.nodeType === NodeType.Enumeration) {
62
+ return processEnumeration(
63
+ field satisfies QuantityContent<ContentValue>,
64
+ locale
65
+ );
66
+ }
67
+
68
+ if (typeof (field as any).type === 'undefined') {
69
+ return processDictionary(field as Content, locale);
70
+ }
71
+
72
+ return field as TransformedContentValue;
73
+ };
74
+
75
+ /**
76
+ * Function to replace the multi lingual content with content in the current locale
77
+ */
78
+ export const processDictionary = (
79
+ content: Content,
80
+ locale: Locales = defaultLocale
81
+ ): TransformedContent => {
82
+ if (content && typeof content === 'object') {
83
+ const result: TransformedContent = {};
84
+
85
+ // List each key in the content and process it
86
+ for (const key of Object.keys(content)) {
87
+ const field = content[key];
88
+
89
+ result[key] = processNode(field, locale);
90
+ }
91
+
92
+ return result;
93
+ }
94
+
95
+ // If it's a string, number, or function, return it
96
+ return content;
97
+ };
@@ -0,0 +1,24 @@
1
+ import {
2
+ type Locales,
3
+ intlayerIntlConfiguration,
4
+ } from '@intlayer/config/client';
5
+ import type { FC, PropsWithChildren } from 'react';
6
+ import { createServerContext, getServerContext } from './serverContext';
7
+
8
+ export const LocaleServerContext = createServerContext<Locales>(
9
+ intlayerIntlConfiguration.defaultLocale
10
+ );
11
+
12
+ export const locale = getServerContext(LocaleServerContext);
13
+
14
+ export type LocaleServerContextProviderProps = PropsWithChildren & {
15
+ locale: Locales;
16
+ };
17
+
18
+ export const LocaleServerContextProvider: FC<
19
+ LocaleServerContextProviderProps
20
+ > = ({ children, locale }) => (
21
+ <LocaleServerContext.Provider value={locale}>
22
+ {children}
23
+ </LocaleServerContext.Provider>
24
+ );
@@ -0,0 +1,19 @@
1
+ import { intlayerIntlConfiguration } from '@intlayer/config/client';
2
+ import { type LanguageContent, getTranslationContent } from '@intlayer/core';
3
+ import { LocaleServerContext } from './LocaleServerContextProvider';
4
+ import { getServerContext } from './serverContext';
5
+
6
+ export const getLocaleTranslation = <Content>(
7
+ languageContent: LanguageContent<Content>
8
+ ) => {
9
+ const locale = getServerContext(LocaleServerContext);
10
+ const content = getTranslationContent<Content>(
11
+ languageContent,
12
+ locale ?? intlayerIntlConfiguration.defaultLocale
13
+ );
14
+
15
+ return {
16
+ locale,
17
+ content,
18
+ };
19
+ };
@@ -0,0 +1,8 @@
1
+ export { getLocaleTranslation as getLocaleContent } from './getLocaleTranslation';
2
+ export { useTraduction } from './useTraduction';
3
+ export {
4
+ LocaleServerContext,
5
+ locale,
6
+ LocaleServerContextProvider,
7
+ } from './LocaleServerContextProvider';
8
+ export { useIntlayer } from './useIntlayer';
@@ -0,0 +1,85 @@
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
+
13
+ import react from 'react';
14
+
15
+ type CacheType<T> = () => { value: T | undefined };
16
+
17
+ const cacheFallback = () => () => ({ value: undefined });
18
+
19
+ export const createServerContext = <T>(defaultValue?: T): ServerContext<T> => {
20
+ throwInClient();
21
+
22
+ const cache = react.cache<CacheType<T>> ?? cacheFallback;
23
+
24
+ const getCache = cache(() => ({
25
+ value: undefined,
26
+ }));
27
+
28
+ return {
29
+ Provider: ({ children, value }) => {
30
+ getCache().value = value;
31
+ return children;
32
+ },
33
+ Consumer: ({ children }) => {
34
+ const store = getCache();
35
+ return children(store ? store.value : defaultValue);
36
+ },
37
+ _storage: getCache,
38
+ _defaultValue: defaultValue,
39
+ };
40
+ };
41
+
42
+ /**
43
+ * Fetches a value present in a given server context.
44
+ * Attempts to closely mimic the `useContext` API.
45
+ *
46
+ * @example
47
+ * getServerContext(LocaleServerContext);
48
+ */
49
+ export const getServerContext = <T>({
50
+ _storage,
51
+ _defaultValue,
52
+ }: ServerContext<T>) => {
53
+ // throwInClient();
54
+ const store = _storage();
55
+ if (!store) return _defaultValue;
56
+ return store.value;
57
+ };
58
+
59
+ type ServerContext<T> = {
60
+ Provider: ({
61
+ children,
62
+ value,
63
+ }: {
64
+ children: React.ReactNode;
65
+ value: T;
66
+ }) => React.ReactNode;
67
+ Consumer: ({
68
+ children,
69
+ }: {
70
+ children: (context: T | undefined) => React.ReactNode;
71
+ }) => React.ReactNode;
72
+ _storage: () => { value: T | undefined };
73
+ _defaultValue: T | undefined;
74
+ };
75
+
76
+ /**
77
+ * Throws if called within a client component environment.
78
+ * Useful to help prevent mistakes.
79
+ */
80
+ const throwInClient = (): void | never => {
81
+ // If window.document is defined we're in a client component
82
+ if (typeof window !== 'undefined') {
83
+ throw new Error(`createServerContext only works in Server Components`);
84
+ }
85
+ };
@@ -0,0 +1,17 @@
1
+ import type { Locales } from '@intlayer/config/client';
2
+ import {
3
+ type DictionaryKeys,
4
+ useIntlayerBase,
5
+ type UseIntlayer,
6
+ } from '../useIntlayerBase';
7
+ import { LocaleServerContext } from './LocaleServerContextProvider';
8
+ import { getServerContext } from './serverContext';
9
+
10
+ export const useIntlayer: UseIntlayer = <T extends DictionaryKeys>(
11
+ id: T,
12
+ locale?: Locales
13
+ ) => {
14
+ const localeTarget = locale ?? getServerContext<Locales>(LocaleServerContext);
15
+
16
+ return useIntlayerBase(id, localeTarget);
17
+ };
@@ -0,0 +1,13 @@
1
+ import type { Locales } from '@intlayer/config/client';
2
+ import type { LanguageContent } from '@intlayer/core';
3
+ import { getTranslation } from '../getTranslation';
4
+ import { LocaleServerContext } from './LocaleServerContextProvider';
5
+ import { getServerContext } from './serverContext';
6
+
7
+ export const useTraduction = <Content>(
8
+ languageContent: LanguageContent<Content>
9
+ ) => {
10
+ const locale = getServerContext<Locales>(LocaleServerContext);
11
+
12
+ return getTranslation(languageContent, locale);
13
+ };
@@ -0,0 +1,32 @@
1
+ /**
2
+ * @intlayer/dictionaries-entry is a package that only returns the dictionary entry path.
3
+ * Using an external package allow to alias it in the bundle configuration (such as webpack).
4
+ * The alias allow hot reload the app (such as nextjs) on any dictionary change.
5
+ */
6
+ import type { Locales } from '@intlayer/config/client';
7
+ import dictionaries from '@intlayer/dictionaries-entry';
8
+ import type { IntlayerDictionaryTypesConnector } from 'intlayer';
9
+ import type { Content } from './processDictionary/contentDictionary';
10
+ import { processDictionary } from './processDictionary/index';
11
+
12
+ export 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
13
+ export type DictionaryKeys = StringFallback<
14
+ keyof IntlayerDictionaryTypesConnector
15
+ >;
16
+
17
+ export type UseIntlayer = <T extends DictionaryKeys>(
18
+ id: T,
19
+ locale?: Locales
20
+ ) => IntlayerDictionaryTypesConnector[T];
21
+
22
+ export const useIntlayerBase: UseIntlayer = <T extends DictionaryKeys>(
23
+ id: T,
24
+ locale?: Locales
25
+ ) => {
26
+ const dictionary: Content = dictionaries[id];
27
+
28
+ return processDictionary(
29
+ dictionary,
30
+ locale
31
+ ) as IntlayerDictionaryTypesConnector[T];
32
+ };