react-intlayer 1.2.1 → 2.0.1

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 (143) hide show
  1. package/dist/cjs/{ContentEditor/contentRender.cjs → client/ContentEditionLayout.cjs} +13 -14
  2. package/dist/cjs/client/ContentEditionLayout.cjs.map +1 -0
  3. package/dist/cjs/client/ContentEditionLayout.d.ts +6 -0
  4. package/dist/cjs/client/{IntlayerClientProvider.cjs → IntlayerProvider.cjs} +21 -10
  5. package/dist/cjs/client/IntlayerProvider.cjs.map +1 -0
  6. package/dist/{esm/client/IntlayerClientProvider.d.mts → cjs/client/IntlayerProvider.d.ts} +4 -3
  7. package/dist/cjs/client/index.cjs +6 -6
  8. package/dist/cjs/client/index.cjs.map +1 -1
  9. package/dist/cjs/client/index.d.ts +2 -1
  10. package/dist/cjs/client/useContent.d.ts +1 -2
  11. package/dist/cjs/client/useIntlayer.cjs +2 -2
  12. package/dist/cjs/client/useIntlayer.cjs.map +1 -1
  13. package/dist/cjs/client/useIntlayer.d.ts +2 -1
  14. package/dist/cjs/client/useLocale.cjs +2 -2
  15. package/dist/cjs/client/useLocale.cjs.map +1 -1
  16. package/dist/cjs/client/useLocaleBase.cjs +2 -2
  17. package/dist/cjs/client/useLocaleBase.cjs.map +1 -1
  18. package/dist/cjs/client/useTraduction.cjs +2 -2
  19. package/dist/cjs/client/useTraduction.cjs.map +1 -1
  20. package/dist/cjs/getEnumeration.cjs +0 -4
  21. package/dist/cjs/getEnumeration.cjs.map +1 -1
  22. package/dist/cjs/getTranslation.cjs +0 -4
  23. package/dist/cjs/getTranslation.cjs.map +1 -1
  24. package/dist/cjs/index.cjs +7 -4
  25. package/dist/cjs/index.cjs.map +1 -1
  26. package/dist/cjs/index.d.ts +3 -2
  27. package/dist/cjs/processDictionary/{contentDictionary.d.cjs → contentDictionary.cjs} +3 -3
  28. package/dist/cjs/processDictionary/contentDictionary.cjs.map +1 -0
  29. package/dist/cjs/processDictionary/contentDictionary.d.ts +16 -0
  30. package/dist/cjs/processDictionary/index.cjs +114 -33
  31. package/dist/cjs/processDictionary/index.cjs.map +1 -1
  32. package/dist/cjs/processDictionary/index.d.ts +5 -4
  33. package/dist/cjs/server/IntlayerServerProvider.cjs +3 -4
  34. package/dist/cjs/server/IntlayerServerProvider.cjs.map +1 -1
  35. package/dist/cjs/server/IntlayerServerProvider.d.ts +1 -1
  36. package/dist/cjs/server/index.d.ts +1 -0
  37. package/dist/cjs/server/useIntlayer.d.ts +2 -1
  38. package/dist/cjs/useIntlayerBase.cjs +48 -1
  39. package/dist/cjs/useIntlayerBase.cjs.map +1 -1
  40. package/dist/cjs/useIntlayerBase.d.ts +51 -4
  41. package/dist/cjs/vite/intlayerPlugin.cjs +1 -1
  42. package/dist/cjs/vite/intlayerPlugin.cjs.map +1 -1
  43. package/dist/esm/client/ContentEditionLayout.d.mts +6 -0
  44. package/dist/esm/client/ContentEditionLayout.mjs +15 -0
  45. package/dist/esm/client/ContentEditionLayout.mjs.map +1 -0
  46. package/dist/{cjs/client/IntlayerClientProvider.d.ts → esm/client/IntlayerProvider.d.mts} +4 -3
  47. package/dist/esm/client/{IntlayerClientProvider.mjs → IntlayerProvider.mjs} +17 -6
  48. package/dist/esm/client/IntlayerProvider.mjs.map +1 -0
  49. package/dist/esm/client/index.d.mts +2 -1
  50. package/dist/esm/client/index.mjs +4 -4
  51. package/dist/esm/client/index.mjs.map +1 -1
  52. package/dist/esm/client/useContent.d.mts +1 -2
  53. package/dist/esm/client/useIntlayer.d.mts +2 -1
  54. package/dist/esm/client/useIntlayer.mjs +1 -1
  55. package/dist/esm/client/useIntlayer.mjs.map +1 -1
  56. package/dist/esm/client/useLocale.mjs +1 -1
  57. package/dist/esm/client/useLocale.mjs.map +1 -1
  58. package/dist/esm/client/useLocaleBase.mjs +1 -1
  59. package/dist/esm/client/useLocaleBase.mjs.map +1 -1
  60. package/dist/esm/client/useTraduction.mjs +1 -1
  61. package/dist/esm/client/useTraduction.mjs.map +1 -1
  62. package/dist/esm/getEnumeration.mjs +0 -4
  63. package/dist/esm/getEnumeration.mjs.map +1 -1
  64. package/dist/esm/getTranslation.mjs +0 -4
  65. package/dist/esm/getTranslation.mjs.map +1 -1
  66. package/dist/esm/index.d.mts +3 -2
  67. package/dist/esm/index.mjs +6 -4
  68. package/dist/esm/index.mjs.map +1 -1
  69. package/dist/esm/processDictionary/contentDictionary.d.mts +16 -0
  70. package/dist/esm/processDictionary/contentDictionary.mjs +1 -0
  71. package/dist/esm/processDictionary/index.d.mts +5 -4
  72. package/dist/esm/processDictionary/index.mjs +116 -34
  73. package/dist/esm/processDictionary/index.mjs.map +1 -1
  74. package/dist/esm/server/IntlayerServerProvider.d.mts +1 -1
  75. package/dist/esm/server/IntlayerServerProvider.mjs +3 -4
  76. package/dist/esm/server/IntlayerServerProvider.mjs.map +1 -1
  77. package/dist/esm/server/index.d.mts +1 -0
  78. package/dist/esm/server/useIntlayer.d.mts +2 -1
  79. package/dist/esm/useIntlayerBase.d.mts +51 -4
  80. package/dist/esm/useIntlayerBase.mjs +46 -1
  81. package/dist/esm/useIntlayerBase.mjs.map +1 -1
  82. package/dist/esm/vite/intlayerPlugin.mjs +1 -1
  83. package/dist/esm/vite/intlayerPlugin.mjs.map +1 -1
  84. package/package.json +14 -9
  85. package/src/index.ts +4 -3
  86. package/dist/cjs/ContentEditor/ContentEditor.cjs +0 -110
  87. package/dist/cjs/ContentEditor/ContentEditor.cjs.map +0 -1
  88. package/dist/cjs/ContentEditor/ContentEditor.d.ts +0 -8
  89. package/dist/cjs/ContentEditor/contentRender.cjs.map +0 -1
  90. package/dist/cjs/ContentEditor/contentRender.d.ts +0 -3
  91. package/dist/cjs/ContentEditor/index.cjs +0 -25
  92. package/dist/cjs/ContentEditor/index.cjs.map +0 -1
  93. package/dist/cjs/ContentEditor/index.d.ts +0 -3
  94. package/dist/cjs/IntlayerProvider.cjs +0 -37
  95. package/dist/cjs/IntlayerProvider.cjs.map +0 -1
  96. package/dist/cjs/IntlayerProvider.d.ts +0 -9
  97. package/dist/cjs/client/IntlayerClientProvider.cjs.map +0 -1
  98. package/dist/cjs/processDictionary/contentDictionary.d.cjs.map +0 -1
  99. package/dist/cjs/processDictionary/contentDictionary.d.d.ts +0 -29
  100. package/dist/esm/ContentEditor/ContentEditor.d.mts +0 -8
  101. package/dist/esm/ContentEditor/ContentEditor.mjs +0 -92
  102. package/dist/esm/ContentEditor/ContentEditor.mjs.map +0 -1
  103. package/dist/esm/ContentEditor/contentRender.d.mts +0 -3
  104. package/dist/esm/ContentEditor/contentRender.mjs +0 -14
  105. package/dist/esm/ContentEditor/contentRender.mjs.map +0 -1
  106. package/dist/esm/ContentEditor/index.d.mts +0 -3
  107. package/dist/esm/ContentEditor/index.mjs +0 -3
  108. package/dist/esm/ContentEditor/index.mjs.map +0 -1
  109. package/dist/esm/IntlayerProvider.d.mts +0 -9
  110. package/dist/esm/IntlayerProvider.mjs +0 -18
  111. package/dist/esm/IntlayerProvider.mjs.map +0 -1
  112. package/dist/esm/client/IntlayerClientProvider.mjs.map +0 -1
  113. package/dist/esm/processDictionary/contentDictionary.d.d.mts +0 -29
  114. package/dist/esm/processDictionary/contentDictionary.d.mjs +0 -1
  115. package/src/ContentEditor/ContentEditor.tsx +0 -116
  116. package/src/ContentEditor/contentRender.tsx +0 -11
  117. package/src/ContentEditor/index.tsx +0 -2
  118. package/src/IntlayerProvider.tsx +0 -24
  119. package/src/cli/react-intlayer.ts +0 -60
  120. package/src/client/IntlayerClientProvider.tsx +0 -76
  121. package/src/client/getBrowserLocale.tsx +0 -192
  122. package/src/client/index.ts +0 -16
  123. package/src/client/useContent.ts +0 -20
  124. package/src/client/useIntlayer.ts +0 -25
  125. package/src/client/useLocale.ts +0 -23
  126. package/src/client/useLocaleBase.ts +0 -24
  127. package/src/client/useLocaleCookie.ts +0 -32
  128. package/src/client/useTraduction.ts +0 -33
  129. package/src/craco/craco.config.ts +0 -11
  130. package/src/craco/intlayerCracoPlugin.ts +0 -89
  131. package/src/getEnumeration.ts +0 -46
  132. package/src/getTranslation.ts +0 -42
  133. package/src/processDictionary/contentDictionary.d.ts +0 -27
  134. package/src/processDictionary/index.ts +0 -144
  135. package/src/server/IntlayerServerProvider.tsx +0 -36
  136. package/src/server/getLocaleTranslation.ts +0 -22
  137. package/src/server/index.ts +0 -8
  138. package/src/server/serverContext.ts +0 -85
  139. package/src/server/useIntlayer.ts +0 -23
  140. package/src/server/useTraduction.ts +0 -34
  141. package/src/useIntlayerBase.ts +0 -35
  142. package/src/vite/intlayerPlugin.ts +0 -59
  143. /package/dist/esm/processDictionary/{contentDictionary.d.mjs.map → contentDictionary.mjs.map} +0 -0
@@ -1,33 +0,0 @@
1
- import type { LanguageContent } from '@intlayer/core';
2
- import { useContext } from 'react';
3
- import { getTranslation } from '../getTranslation';
4
- import { IntlayerClientContext } from './IntlayerClientProvider';
5
-
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
- * ```tsx
16
- * const content = useTraduction<string>({
17
- * en: 'Hello',
18
- * fr: 'Bonjour',
19
- * }, 'fr');
20
- * // 'Bonjour'
21
- * ```
22
- *
23
- * Using TypeScript:
24
- * - this function will require each locale to be defined if defined in the project configuration.
25
- * - If a locale is missing, it will make each existing locale optional and raise an error if the locale is not found.
26
- */
27
- export const useTraduction = <Content = string>(
28
- languageContent: LanguageContent<Content>
29
- ): Content => {
30
- const { locale } = useContext(IntlayerClientContext);
31
-
32
- return getTranslation(languageContent, locale);
33
- };
@@ -1,11 +0,0 @@
1
- import type { CracoConfig } from '@craco/types';
2
- import * as intlayerPlugin from './intlayerCracoPlugin';
3
-
4
- // Usage Example
5
- module.exports = {
6
- plugins: [
7
- {
8
- plugin: intlayerPlugin,
9
- },
10
- ],
11
- } satisfies CracoConfig;
@@ -1,89 +0,0 @@
1
- import { resolve, relative, join } from 'path';
2
- import type {
3
- CracoConfig,
4
- CracoConfigOverride,
5
- CracoPlugin,
6
- WebpackConfigOverride,
7
- } from '@craco/types';
8
- import { getConfiguration, formatEnvVariable } from '@intlayer/config';
9
- import { IntLayerPlugin as IntLayerWebpackPlugin } from '@intlayer/webpack';
10
- import webpack, { type Configuration as WebpackConfig } from 'webpack';
11
-
12
- // Get IntLayer configuration
13
- const intlayerConfig = getConfiguration();
14
-
15
- // Format environment variables
16
- const env: Record<string, string> = formatEnvVariable('react_app');
17
-
18
- // Custom CRACO plugin function to override webpack configuration
19
- export const overrideWebpackConfig = ({
20
- webpackConfig,
21
- }: WebpackConfigOverride): WebpackConfig => {
22
- webpackConfig.externals = {
23
- ...(typeof webpackConfig.externals === 'object'
24
- ? webpackConfig.externals
25
- : {}),
26
- esbuild: 'esbuild',
27
- module: 'module',
28
- fs: 'fs',
29
- vm: 'vm',
30
- };
31
-
32
- (webpackConfig.module?.rules ?? []).push({
33
- test: /\.node$/,
34
- use: 'node-loader',
35
- });
36
-
37
- // You can add any custom CRACO plugins here if needed
38
- // config.plugins.push(new CustomCracoPlugin());
39
-
40
- return webpackConfig;
41
- };
42
-
43
- // Return a CRACO configuration object
44
- export const overrideCracoConfig = ({
45
- cracoConfig,
46
- }: CracoConfigOverride): CracoConfig => {
47
- const { mainDir, baseDir } = intlayerConfig.content;
48
-
49
- const dictionariesPath = join(mainDir, 'dictionaries.mjs');
50
- const relativeDictionariesPath = relative(baseDir, dictionariesPath);
51
-
52
- return {
53
- ...cracoConfig,
54
- webpack: {
55
- ...cracoConfig.webpack,
56
- plugins: {
57
- ...cracoConfig.webpack?.plugins,
58
- add: [new webpack.EnvironmentPlugin(env), new IntLayerWebpackPlugin()],
59
- },
60
- alias: {
61
- ...cracoConfig.webpack?.alias,
62
- '@intlayer/dictionaries-entry': resolve('./', relativeDictionariesPath),
63
- },
64
- },
65
- };
66
- };
67
-
68
- /**
69
- * A CRACO plugin that adds the IntLayer configuration to the webpack configuration and sets the environment variables.
70
- *
71
- * Usage:
72
- *
73
- * ```ts
74
- * const cracoConfig: CracoConfig = {
75
- * plugins: [
76
- * {
77
- * plugin: intlayerCracoPlugin(),
78
- * },
79
- * ],
80
- * };
81
- *
82
- * export default cracoConfig;
83
- * ```
84
- *
85
- */
86
- export const plugin: CracoPlugin = {
87
- overrideCracoConfig,
88
- overrideWebpackConfig,
89
- };
@@ -1,46 +0,0 @@
1
- import { type QuantityContent, getEnumerationContent } from '@intlayer/core';
2
- import { contentRender } from './ContentEditor/contentRender';
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
- * ```ts
12
- * const content = getEnumeration({
13
- * '<=-2.3': 'You have less than -2.3',
14
- * '<1': 'You have less than one',
15
- * '2': 'You have two',
16
- * '>=3': 'You have three or more',
17
- * }, 2);
18
- * // 'You have two'
19
- * ```
20
- *
21
- * The order of the keys will define the priority of the content.
22
- *
23
- * ```ts
24
- * const content = getEnumeration({
25
- * '<4': 'You have less than four',
26
- * '2': 'You have two',
27
- * }, 2);
28
- * // 'You have less than four'
29
- * ```
30
- *
31
- */
32
- export const getEnumeration = <Content>(
33
- enumerationContent: QuantityContent<Content>,
34
- quantity: number
35
- ): Content => {
36
- const result: Content = getEnumerationContent<Content>(
37
- enumerationContent,
38
- quantity
39
- );
40
-
41
- if (typeof result === 'string') {
42
- return contentRender(result) as Content;
43
- }
44
-
45
- return result;
46
- };
@@ -1,42 +0,0 @@
1
- import { type Locales, getConfiguration } from '@intlayer/config/client';
2
- import { type LanguageContent, getTranslationContent } from '@intlayer/core';
3
- import { contentRender } from './ContentEditor/contentRender';
4
-
5
- /**
6
- *
7
- * Allow to pick a content based on a locale.
8
- * If not locale found, it will return the content related to the default locale.
9
- *
10
- * Return either the content editor, or the content itself depending on the configuration.
11
- *
12
- * Usage:
13
- *
14
- * ```ts
15
- * const content = getTranslation<string>({
16
- * en: 'Hello',
17
- * fr: 'Bonjour',
18
- * }, 'fr');
19
- * // 'Bonjour'
20
- * ```
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 getTranslation = <Content = string>(
27
- languageContent: LanguageContent<Content>,
28
- locale?: Locales
29
- ): Content => {
30
- const { defaultLocale } = getConfiguration().internationalization;
31
-
32
- const result: Content = getTranslationContent<Content>(
33
- languageContent,
34
- locale ?? defaultLocale
35
- );
36
-
37
- if (typeof result === 'string') {
38
- return contentRender(result) as Content;
39
- }
40
-
41
- return result;
42
- };
@@ -1,27 +0,0 @@
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
- };
@@ -1,144 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { type Locales, getConfiguration } from '@intlayer/config/client';
3
- import {
4
- NodeType,
5
- type QuantityContent,
6
- type LanguageContent,
7
- } from '@intlayer/core';
8
- import { type ReactElement, createElement, type ReactNode } from 'react';
9
- import { getEnumeration } from '../getEnumeration';
10
- import { getTranslation } from '../getTranslation';
11
- import type {
12
- Content,
13
- ContentValue,
14
- TransformedContent,
15
- TransformedContentValue,
16
- } from './contentDictionary';
17
-
18
- const { defaultLocale } = getConfiguration().internationalization;
19
-
20
- const processTranslation = (
21
- languageContent: LanguageContent<ContentValue>,
22
- locale: Locales
23
- ): TransformedContent => {
24
- const translationResult: ContentValue = getTranslation<ContentValue>(
25
- languageContent,
26
- locale
27
- );
28
-
29
- return processDictionary(translationResult as Content, locale);
30
- };
31
-
32
- const processEnumeration = (
33
- enumerationContent: QuantityContent<ContentValue>,
34
- locale: Locales
35
- ): TransformedContentValue => {
36
- return (quantity: number): TransformedContentValue => {
37
- const enumerationResult: ContentValue = getEnumeration<ContentValue>(
38
- enumerationContent,
39
- quantity
40
- );
41
-
42
- //
43
- return processDictionary(enumerationResult as Content, locale);
44
- };
45
- };
46
-
47
- const isReactNode = (node: Record<string, unknown>): boolean =>
48
- typeof node?.key !== 'undefined' && typeof node?.props !== 'undefined';
49
-
50
- export const processNode = (
51
- field: ContentValue | undefined,
52
- locale: Locales
53
- ): TransformedContentValue => {
54
- if (typeof field !== 'object' || field === null) {
55
- return field;
56
- }
57
-
58
- if (field.nodeType === NodeType.Translation) {
59
- return processTranslation(field as LanguageContent<ContentValue>, locale);
60
- }
61
-
62
- if (field.nodeType === NodeType.Enumeration) {
63
- return processEnumeration(
64
- field satisfies QuantityContent<ContentValue>,
65
- locale
66
- );
67
- }
68
-
69
- if (typeof (field as any).type === 'undefined') {
70
- return processDictionary(field as Content, locale);
71
- }
72
-
73
- return field as TransformedContentValue;
74
- };
75
-
76
- // This function recursively creates React elements from a given JSON-like structure
77
- const createReactElement = (element: ReactElement) => {
78
- if (typeof element === 'string') {
79
- // If it's a string, simply return it (used for text content)
80
- return element;
81
- }
82
-
83
- // Destructure the component properties
84
-
85
- const convertChildrenAsArray = (element: ReactElement): ReactElement => {
86
- if (element?.props && typeof element.props.children === 'object') {
87
- const childrenResult: ReactNode[] = [];
88
- const { children } = element.props;
89
-
90
- // Create the children elements recursively, if any
91
- Object.keys(children).forEach((key) => {
92
- childrenResult.push(createReactElement(children[key]));
93
- });
94
-
95
- return {
96
- ...element,
97
- props: { ...element.props, children: childrenResult },
98
- };
99
- }
100
-
101
- return {
102
- ...element,
103
- props: { ...element.props, children: element.props.children },
104
- };
105
- };
106
-
107
- const fixedElement = convertChildrenAsArray(element);
108
-
109
- const { type, props } = fixedElement;
110
-
111
- // Create and return the React element
112
- return createElement(type ?? 'div', props, ...props.children);
113
- };
114
-
115
- /**
116
- * Function that process a dictionary and return the result to be used in the application.
117
- */
118
- export const processDictionary = (
119
- content: Content,
120
- locale: Locales = defaultLocale
121
- ): TransformedContent => {
122
- // If it's a React element, render it
123
- if (isReactNode(content)) {
124
- return createReactElement(
125
- content as unknown as ReactElement
126
- ) as unknown as TransformedContent;
127
- }
128
-
129
- if (content && typeof content === 'object') {
130
- const result: TransformedContent = {};
131
-
132
- // List each key in the content and process it
133
- for (const key of Object.keys(content)) {
134
- const field = content[key];
135
-
136
- result[key] = processNode(field, locale);
137
- }
138
-
139
- return result;
140
- }
141
-
142
- // If it's a string, number, or function, return it
143
- return content;
144
- };
@@ -1,36 +0,0 @@
1
- import { type Locales, getConfiguration } from '@intlayer/config/client';
2
- import type { FC, PropsWithChildren } from 'react';
3
- import { createServerContext, getServerContext } from './serverContext';
4
-
5
- /**
6
- * Context that store the current locale on the server side
7
- */
8
- export const IntlayerServerContext = createServerContext<Locales>(
9
- getConfiguration().internationalization.defaultLocale
10
- );
11
-
12
- /**
13
- * Hook that provides the current locale
14
- */
15
- export const useIntlayer = () => getServerContext(IntlayerServerContext);
16
-
17
- /**
18
- * Get the current locale
19
- */
20
- export const locale = getServerContext(IntlayerServerContext);
21
-
22
- export type IntlayerServerProviderProps = PropsWithChildren & {
23
- locale: Locales;
24
- };
25
-
26
- /**
27
- * Provider that store the current locale on the server side
28
- */
29
- export const IntlayerServerProvider: FC<IntlayerServerProviderProps> = ({
30
- children,
31
- locale,
32
- }) => (
33
- <IntlayerServerContext.Provider value={locale}>
34
- {children}
35
- </IntlayerServerContext.Provider>
36
- );
@@ -1,22 +0,0 @@
1
- import { getConfiguration } from '@intlayer/config/client';
2
- import { type LanguageContent, getTranslationContent } from '@intlayer/core';
3
- import { IntlayerServerContext } from './IntlayerServerProvider';
4
- import { getServerContext } from './serverContext';
5
-
6
- /**
7
- * On the server side, get the translation content based on the locale
8
- */
9
- export const getLocaleTranslation = <Content>(
10
- languageContent: LanguageContent<Content>
11
- ) => {
12
- const locale = getServerContext(IntlayerServerContext);
13
- const content = getTranslationContent<Content>(
14
- languageContent,
15
- locale ?? getConfiguration().internationalization.defaultLocale
16
- );
17
-
18
- return {
19
- locale,
20
- content,
21
- };
22
- };
@@ -1,8 +0,0 @@
1
- export { getLocaleTranslation as getLocaleContent } from './getLocaleTranslation';
2
- export { useTraduction } from './useTraduction';
3
- export {
4
- IntlayerServerContext as IntlayerServer,
5
- locale,
6
- IntlayerServerProvider,
7
- } from './IntlayerServerProvider';
8
- export { useIntlayer } from './useIntlayer';
@@ -1,85 +0,0 @@
1
- /**
2
- * Creates a new datastore for a given server context.
3
- * Attempts to closely mimic the `createContext` API.
4
- *
5
- * @example
6
- * const IntlayerServer = createServerContext<string | null>(null);
7
- *
8
- * <IntlayerServer.Provider value={locale}>
9
- * {children}
10
- * </IntlayerServer.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(IntlayerServer);
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
- };
@@ -1,23 +0,0 @@
1
- import type { Locales } from '@intlayer/config/client';
2
- import {
3
- type DictionaryKeys,
4
- useIntlayerBase,
5
- type UseIntlayer,
6
- } from '../useIntlayerBase';
7
- import { IntlayerServerContext } from './IntlayerServerProvider';
8
- import { getServerContext } from './serverContext';
9
-
10
- /**
11
- * On the server side, Hook that picking one dictionary by its id and return the content
12
- *
13
- * If the locale is not provided, it will use the locale from the server context
14
- */
15
- export const useIntlayer: UseIntlayer = <T extends DictionaryKeys>(
16
- id: T,
17
- locale?: Locales
18
- ) => {
19
- const localeTarget =
20
- locale ?? getServerContext<Locales>(IntlayerServerContext);
21
-
22
- return useIntlayerBase(id, localeTarget);
23
- };
@@ -1,34 +0,0 @@
1
- import type { Locales } from '@intlayer/config/client';
2
- import type { LanguageContent } from '@intlayer/core';
3
- import { getTranslation } from '../getTranslation';
4
- import { IntlayerServerContext } from './IntlayerServerProvider';
5
- import { getServerContext } from './serverContext';
6
-
7
- /**
8
- * On the server side, hook to get the translation content based on the locale
9
- *
10
- * If not locale found, it will return the content related to the default locale.
11
- *
12
- * Return either the content editor, or the content itself depending on the configuration.
13
- *
14
- * Usage:
15
- *
16
- * ```ts
17
- * const content = useTraduction<string>({
18
- * en: 'Hello',
19
- * fr: 'Bonjour',
20
- * }, 'fr');
21
- * // 'Bonjour'
22
- * ```
23
- *
24
- * Using TypeScript:
25
- * - this function will require each locale to be defined if defined in the project configuration.
26
- * - If a locale is missing, it will make each existing locale optional and raise an error if the locale is not found.
27
- */
28
- export const useTraduction = <Content = string>(
29
- languageContent: LanguageContent<Content>
30
- ) => {
31
- const locale = getServerContext<Locales>(IntlayerServerContext);
32
-
33
- return getTranslation(languageContent, locale);
34
- };
@@ -1,35 +0,0 @@
1
- import type { Locales } from '@intlayer/config/client';
2
- /**
3
- * @intlayer/dictionaries-entry is a package that only returns the dictionary entry path.
4
- * Using an external package allow to alias it in the bundle configuration (such as webpack).
5
- * The alias allow hot reload the app (such as nextjs) on any dictionary change.
6
- */
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
- /**
23
- * Hook that picking one dictionary by its id and return the content.
24
- */
25
- export const useIntlayerBase: UseIntlayer = <T extends DictionaryKeys>(
26
- id: T,
27
- locale?: Locales
28
- ) => {
29
- const dictionary: Content = dictionaries[id];
30
-
31
- return processDictionary(
32
- dictionary,
33
- locale
34
- ) as IntlayerDictionaryTypesConnector[T];
35
- };