react-intlayer 1.0.1 → 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 (136) hide show
  1. package/dist/cjs/{LocaleContextProvider.cjs → IntlayerProvider.cjs} +10 -10
  2. package/dist/cjs/IntlayerProvider.cjs.map +1 -0
  3. package/dist/cjs/IntlayerProvider.d.ts +9 -0
  4. package/dist/cjs/client/{LocaleClientContextProvider.cjs → IntlayerClientProvider.cjs} +16 -13
  5. package/dist/cjs/client/IntlayerClientProvider.cjs.map +1 -0
  6. package/dist/cjs/client/IntlayerClientProvider.d.ts +24 -0
  7. package/dist/cjs/client/index.cjs +7 -7
  8. package/dist/cjs/client/index.cjs.map +1 -1
  9. package/dist/cjs/client/index.d.ts +1 -1
  10. package/dist/cjs/client/useContent.cjs.map +1 -1
  11. package/dist/cjs/client/useContent.d.ts +4 -1
  12. package/dist/cjs/client/useIntlayer.cjs +2 -2
  13. package/dist/cjs/client/useIntlayer.cjs.map +1 -1
  14. package/dist/cjs/client/useIntlayer.d.ts +5 -0
  15. package/dist/cjs/client/useLocale.cjs +2 -2
  16. package/dist/cjs/client/useLocale.cjs.map +1 -1
  17. package/dist/cjs/client/useLocale.d.ts +3 -0
  18. package/dist/cjs/client/useLocaleCookie.cjs.map +1 -1
  19. package/dist/cjs/client/useLocaleCookie.d.ts +9 -0
  20. package/dist/cjs/client/useTraduction.cjs +2 -2
  21. package/dist/cjs/client/useTraduction.cjs.map +1 -1
  22. package/dist/cjs/client/useTraduction.d.ts +21 -1
  23. package/dist/cjs/getEnumeration.cjs.map +1 -1
  24. package/dist/cjs/getEnumeration.d.ts +25 -0
  25. package/dist/cjs/getTranslation.cjs.map +1 -1
  26. package/dist/cjs/getTranslation.d.ts +21 -1
  27. package/dist/cjs/index.cjs +4 -6
  28. package/dist/cjs/index.cjs.map +1 -1
  29. package/dist/cjs/index.d.ts +1 -1
  30. package/dist/cjs/processDictionary/index.cjs.map +1 -1
  31. package/dist/cjs/processDictionary/index.d.ts +1 -1
  32. package/dist/cjs/server/{LocaleServerContextProvider.cjs → IntlayerServerProvider.cjs} +19 -13
  33. package/dist/cjs/server/IntlayerServerProvider.cjs.map +1 -0
  34. package/dist/{esm/server/LocaleServerContextProvider.d.mts → cjs/server/IntlayerServerProvider.d.ts} +17 -4
  35. package/dist/cjs/server/getLocaleTranslation.cjs +2 -2
  36. package/dist/cjs/server/getLocaleTranslation.cjs.map +1 -1
  37. package/dist/cjs/server/getLocaleTranslation.d.ts +3 -0
  38. package/dist/cjs/server/index.cjs +6 -6
  39. package/dist/cjs/server/index.cjs.map +1 -1
  40. package/dist/cjs/server/index.d.ts +1 -1
  41. package/dist/cjs/server/serverContext.cjs.map +1 -1
  42. package/dist/cjs/server/serverContext.d.ts +4 -4
  43. package/dist/cjs/server/useIntlayer.cjs +2 -2
  44. package/dist/cjs/server/useIntlayer.cjs.map +1 -1
  45. package/dist/cjs/server/useIntlayer.d.ts +5 -0
  46. package/dist/cjs/server/useTraduction.cjs +2 -2
  47. package/dist/cjs/server/useTraduction.cjs.map +1 -1
  48. package/dist/cjs/server/useTraduction.d.ts +21 -1
  49. package/dist/cjs/useIntlayerBase.cjs.map +1 -1
  50. package/dist/cjs/useIntlayerBase.d.ts +3 -6
  51. package/dist/esm/IntlayerProvider.d.mts +9 -0
  52. package/dist/esm/IntlayerProvider.mjs +17 -0
  53. package/dist/esm/IntlayerProvider.mjs.map +1 -0
  54. package/dist/esm/client/IntlayerClientProvider.d.mts +24 -0
  55. package/dist/esm/client/IntlayerClientProvider.mjs +24 -0
  56. package/dist/esm/client/IntlayerClientProvider.mjs.map +1 -0
  57. package/dist/esm/client/index.d.mts +1 -1
  58. package/dist/esm/client/index.mjs +7 -7
  59. package/dist/esm/client/index.mjs.map +1 -1
  60. package/dist/esm/client/useContent.d.mts +4 -1
  61. package/dist/esm/client/useContent.mjs.map +1 -1
  62. package/dist/esm/client/useIntlayer.d.mts +5 -0
  63. package/dist/esm/client/useIntlayer.mjs +2 -2
  64. package/dist/esm/client/useIntlayer.mjs.map +1 -1
  65. package/dist/esm/client/useLocale.d.mts +3 -0
  66. package/dist/esm/client/useLocale.mjs +2 -2
  67. package/dist/esm/client/useLocale.mjs.map +1 -1
  68. package/dist/esm/client/useLocaleCookie.d.mts +9 -0
  69. package/dist/esm/client/useLocaleCookie.mjs.map +1 -1
  70. package/dist/esm/client/useTraduction.d.mts +21 -1
  71. package/dist/esm/client/useTraduction.mjs +2 -2
  72. package/dist/esm/client/useTraduction.mjs.map +1 -1
  73. package/dist/esm/getEnumeration.d.mts +25 -0
  74. package/dist/esm/getEnumeration.mjs.map +1 -1
  75. package/dist/esm/getTranslation.d.mts +21 -1
  76. package/dist/esm/getTranslation.mjs.map +1 -1
  77. package/dist/esm/index.d.mts +1 -1
  78. package/dist/esm/index.mjs +4 -6
  79. package/dist/esm/index.mjs.map +1 -1
  80. package/dist/esm/processDictionary/index.d.mts +1 -1
  81. package/dist/esm/processDictionary/index.mjs.map +1 -1
  82. package/dist/{cjs/server/LocaleServerContextProvider.d.ts → esm/server/IntlayerServerProvider.d.mts} +17 -4
  83. package/dist/esm/server/IntlayerServerProvider.mjs +21 -0
  84. package/dist/esm/server/IntlayerServerProvider.mjs.map +1 -0
  85. package/dist/esm/server/getLocaleTranslation.d.mts +3 -0
  86. package/dist/esm/server/getLocaleTranslation.mjs +2 -2
  87. package/dist/esm/server/getLocaleTranslation.mjs.map +1 -1
  88. package/dist/esm/server/index.d.mts +1 -1
  89. package/dist/esm/server/index.mjs +5 -5
  90. package/dist/esm/server/index.mjs.map +1 -1
  91. package/dist/esm/server/serverContext.d.mts +4 -4
  92. package/dist/esm/server/serverContext.mjs.map +1 -1
  93. package/dist/esm/server/useIntlayer.d.mts +5 -0
  94. package/dist/esm/server/useIntlayer.mjs +2 -2
  95. package/dist/esm/server/useIntlayer.mjs.map +1 -1
  96. package/dist/esm/server/useTraduction.d.mts +21 -1
  97. package/dist/esm/server/useTraduction.mjs +2 -2
  98. package/dist/esm/server/useTraduction.mjs.map +1 -1
  99. package/dist/esm/useIntlayerBase.d.mts +3 -6
  100. package/dist/esm/useIntlayerBase.mjs.map +1 -1
  101. package/package.json +7 -7
  102. package/src/IntlayerProvider.tsx +24 -0
  103. package/src/client/IntlayerClientProvider.tsx +45 -0
  104. package/src/client/index.ts +5 -5
  105. package/src/client/useContent.ts +3 -0
  106. package/src/client/useIntlayer.ts +7 -2
  107. package/src/client/useLocale.ts +5 -2
  108. package/src/client/useLocaleCookie.ts +9 -1
  109. package/src/client/useTraduction.ts +23 -3
  110. package/src/getEnumeration.ts +25 -0
  111. package/src/getTranslation.ts +21 -1
  112. package/src/index.ts +3 -4
  113. package/src/processDictionary/index.ts +1 -1
  114. package/src/server/IntlayerServerProvider.tsx +39 -0
  115. package/src/server/getLocaleTranslation.ts +5 -2
  116. package/src/server/index.ts +3 -3
  117. package/src/server/serverContext.ts +4 -4
  118. package/src/server/useIntlayer.ts +8 -2
  119. package/src/server/useTraduction.ts +23 -3
  120. package/src/useIntlayerBase.ts +4 -1
  121. package/dist/cjs/LocaleContextProvider.cjs.map +0 -1
  122. package/dist/cjs/LocaleContextProvider.d.ts +0 -9
  123. package/dist/cjs/client/LocaleClientContextProvider.cjs.map +0 -1
  124. package/dist/cjs/client/LocaleClientContextProvider.d.ts +0 -15
  125. package/dist/cjs/server/LocaleServerContextProvider.cjs.map +0 -1
  126. package/dist/esm/LocaleContextProvider.d.mts +0 -9
  127. package/dist/esm/LocaleContextProvider.mjs +0 -17
  128. package/dist/esm/LocaleContextProvider.mjs.map +0 -1
  129. package/dist/esm/client/LocaleClientContextProvider.d.mts +0 -15
  130. package/dist/esm/client/LocaleClientContextProvider.mjs +0 -21
  131. package/dist/esm/client/LocaleClientContextProvider.mjs.map +0 -1
  132. package/dist/esm/server/LocaleServerContextProvider.mjs +0 -16
  133. package/dist/esm/server/LocaleServerContextProvider.mjs.map +0 -1
  134. package/src/LocaleContextProvider.tsx +0 -26
  135. package/src/client/LocaleClientContextProvider.tsx +0 -35
  136. package/src/server/LocaleServerContextProvider.tsx +0 -24
@@ -1,5 +1,5 @@
1
1
  export { getTranslation } from './getTranslation.mjs';
2
- export { LocaleClientContext, LocaleClientContextProvider, LocaleClientContextProviderProps, useLocaleContext } from './client/LocaleClientContextProvider.mjs';
2
+ export { IntlayerClientContext as IntlayerClient, IntlayerClientProvider, IntlayerClientProviderProps } from './client/IntlayerClientProvider.mjs';
3
3
  export { useIntlayer } from './client/useIntlayer.mjs';
4
4
  export { useLocale } from './client/useLocale.mjs';
5
5
  export { useTraduction } from './client/useTraduction.mjs';
@@ -1,8 +1,7 @@
1
1
  import { getTranslation } from './getTranslation.mjs';
2
2
  import {
3
- LocaleClientContextProvider,
4
- LocaleClientContext,
5
- useLocaleContext,
3
+ IntlayerClientProvider,
4
+ IntlayerClient,
6
5
  useIntlayer,
7
6
  useLocale,
8
7
  useTraduction,
@@ -11,14 +10,13 @@ import {
11
10
  setLocaleCookie
12
11
  } from './client/index.mjs';
13
12
  export {
14
- LocaleClientContext,
15
- LocaleClientContextProvider,
13
+ IntlayerClient,
14
+ IntlayerClientProvider,
16
15
  getTranslation,
17
16
  localeCookie,
18
17
  setLocaleCookie,
19
18
  useIntlayer,
20
19
  useLocale,
21
- useLocaleContext,
22
20
  useLocaleCookie,
23
21
  useTraduction
24
22
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export { getTranslation } from './getTranslation';\nexport {\n LocaleClientContextProvider,\n type LocaleClientContextProviderProps,\n LocaleClientContext,\n useLocaleContext,\n useIntlayer,\n useLocale,\n useTraduction,\n useLocaleCookie,\n localeCookie,\n setLocaleCookie,\n} from './client/index';\nexport {} from './client/useLocaleCookie';\n"],"mappings":"AAAA,SAAS,sBAAsB;AAC/B;AAAA,EACE;AAAA,EAEA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;","names":[]}
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export { getTranslation } from './getTranslation';\nexport {\n IntlayerClientProvider,\n type IntlayerClientProviderProps,\n IntlayerClient,\n useIntlayer,\n useLocale,\n useTraduction,\n useLocaleCookie,\n localeCookie,\n setLocaleCookie,\n} from './client/index';\nexport {} from './client/useLocaleCookie';\n"],"mappings":"AAAA,SAAS,sBAAsB;AAC/B;AAAA,EACE;AAAA,EAEA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;","names":[]}
@@ -4,7 +4,7 @@ import '@intlayer/core';
4
4
 
5
5
  declare const processNode: (field: ContentValue | undefined, locale: Locales) => TransformedContentValue;
6
6
  /**
7
- * Function to replace the multi lingual content with content in the current locale
7
+ * Function that process a dictionary and return the result to be used in the application.
8
8
  */
9
9
  declare const processDictionary: (content: Content, locale?: Locales) => TransformedContent;
10
10
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/processDictionary/index.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport {\n type Locales,\n intlayerIntlConfiguration,\n} from '@intlayer/config/client';\nimport {\n NodeType,\n type QuantityContent,\n type LanguageContent,\n} from '@intlayer/core';\nimport { getEnumeration } from '../getEnumeration';\nimport { getTranslation } from '../getTranslation';\nimport type {\n Content,\n ContentValue,\n TransformedContent,\n TransformedContentValue,\n} from './contentDictionary';\n\nconst defaultLocale = intlayerIntlConfiguration.defaultLocale;\n\nconst processTranslation = (\n languageContent: LanguageContent<ContentValue>,\n locale: Locales\n): TransformedContent => {\n const translationResult: ContentValue = getTranslation<ContentValue>(\n languageContent,\n locale\n );\n\n return processDictionary(translationResult as Content, locale);\n};\n\nconst processEnumeration = (\n enumerationContent: QuantityContent<ContentValue>,\n locale: Locales\n): TransformedContentValue => {\n return (quantity: number): TransformedContentValue => {\n const enumerationResult: ContentValue = getEnumeration<ContentValue>(\n enumerationContent,\n quantity\n );\n\n //\n return processDictionary(enumerationResult as Content, locale);\n };\n};\n\nexport const processNode = (\n field: ContentValue | undefined,\n locale: Locales\n): TransformedContentValue => {\n if (typeof field !== 'object' || field === null) {\n return field;\n }\n\n if (field.nodeType === NodeType.Translation) {\n return processTranslation(field as LanguageContent<ContentValue>, locale);\n }\n\n if (field.nodeType === NodeType.Enumeration) {\n return processEnumeration(\n field satisfies QuantityContent<ContentValue>,\n locale\n );\n }\n\n if (typeof (field as any).type === 'undefined') {\n return processDictionary(field as Content, locale);\n }\n\n return field as TransformedContentValue;\n};\n\n/**\n * Function to replace the multi lingual content with content in the current locale\n */\nexport const processDictionary = (\n content: Content,\n locale: Locales = defaultLocale\n): TransformedContent => {\n if (content && typeof content === 'object') {\n const result: TransformedContent = {};\n\n // List each key in the content and process it\n for (const key of Object.keys(content)) {\n const field = content[key];\n\n result[key] = processNode(field, locale);\n }\n\n return result;\n }\n\n // If it's a string, number, or function, return it\n return content;\n};\n"],"mappings":"AACA;AAAA,EAEE;AAAA,OACK;AACP;AAAA,EACE;AAAA,OAGK;AACP,SAAS,sBAAsB;AAC/B,SAAS,sBAAsB;AAQ/B,MAAM,gBAAgB,0BAA0B;AAEhD,MAAM,qBAAqB,CACzB,iBACA,WACuB;AACvB,QAAM,oBAAkC;AAAA,IACtC;AAAA,IACA;AAAA,EACF;AAEA,SAAO,kBAAkB,mBAA8B,MAAM;AAC/D;AAEA,MAAM,qBAAqB,CACzB,oBACA,WAC4B;AAC5B,SAAO,CAAC,aAA8C;AACpD,UAAM,oBAAkC;AAAA,MACtC;AAAA,MACA;AAAA,IACF;AAGA,WAAO,kBAAkB,mBAA8B,MAAM;AAAA,EAC/D;AACF;AAEO,MAAM,cAAc,CACzB,OACA,WAC4B;AAC5B,MAAI,OAAO,UAAU,YAAY,UAAU,MAAM;AAC/C,WAAO;AAAA,EACT;AAEA,MAAI,MAAM,aAAa,SAAS,aAAa;AAC3C,WAAO,mBAAmB,OAAwC,MAAM;AAAA,EAC1E;AAEA,MAAI,MAAM,aAAa,SAAS,aAAa;AAC3C,WAAO;AAAA,MACL;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAEA,MAAI,OAAQ,MAAc,SAAS,aAAa;AAC9C,WAAO,kBAAkB,OAAkB,MAAM;AAAA,EACnD;AAEA,SAAO;AACT;AAKO,MAAM,oBAAoB,CAC/B,SACA,SAAkB,kBACK;AACvB,MAAI,WAAW,OAAO,YAAY,UAAU;AAC1C,UAAM,SAA6B,CAAC;AAGpC,eAAW,OAAO,OAAO,KAAK,OAAO,GAAG;AACtC,YAAM,QAAQ,QAAQ,GAAG;AAEzB,aAAO,GAAG,IAAI,YAAY,OAAO,MAAM;AAAA,IACzC;AAEA,WAAO;AAAA,EACT;AAGA,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../../../src/processDictionary/index.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport {\n type Locales,\n intlayerIntlConfiguration,\n} from '@intlayer/config/client';\nimport {\n NodeType,\n type QuantityContent,\n type LanguageContent,\n} from '@intlayer/core';\nimport { getEnumeration } from '../getEnumeration';\nimport { getTranslation } from '../getTranslation';\nimport type {\n Content,\n ContentValue,\n TransformedContent,\n TransformedContentValue,\n} from './contentDictionary';\n\nconst defaultLocale = intlayerIntlConfiguration.defaultLocale;\n\nconst processTranslation = (\n languageContent: LanguageContent<ContentValue>,\n locale: Locales\n): TransformedContent => {\n const translationResult: ContentValue = getTranslation<ContentValue>(\n languageContent,\n locale\n );\n\n return processDictionary(translationResult as Content, locale);\n};\n\nconst processEnumeration = (\n enumerationContent: QuantityContent<ContentValue>,\n locale: Locales\n): TransformedContentValue => {\n return (quantity: number): TransformedContentValue => {\n const enumerationResult: ContentValue = getEnumeration<ContentValue>(\n enumerationContent,\n quantity\n );\n\n //\n return processDictionary(enumerationResult as Content, locale);\n };\n};\n\nexport const processNode = (\n field: ContentValue | undefined,\n locale: Locales\n): TransformedContentValue => {\n if (typeof field !== 'object' || field === null) {\n return field;\n }\n\n if (field.nodeType === NodeType.Translation) {\n return processTranslation(field as LanguageContent<ContentValue>, locale);\n }\n\n if (field.nodeType === NodeType.Enumeration) {\n return processEnumeration(\n field satisfies QuantityContent<ContentValue>,\n locale\n );\n }\n\n if (typeof (field as any).type === 'undefined') {\n return processDictionary(field as Content, locale);\n }\n\n return field as TransformedContentValue;\n};\n\n/**\n * Function that process a dictionary and return the result to be used in the application.\n */\nexport const processDictionary = (\n content: Content,\n locale: Locales = defaultLocale\n): TransformedContent => {\n if (content && typeof content === 'object') {\n const result: TransformedContent = {};\n\n // List each key in the content and process it\n for (const key of Object.keys(content)) {\n const field = content[key];\n\n result[key] = processNode(field, locale);\n }\n\n return result;\n }\n\n // If it's a string, number, or function, return it\n return content;\n};\n"],"mappings":"AACA;AAAA,EAEE;AAAA,OACK;AACP;AAAA,EACE;AAAA,OAGK;AACP,SAAS,sBAAsB;AAC/B,SAAS,sBAAsB;AAQ/B,MAAM,gBAAgB,0BAA0B;AAEhD,MAAM,qBAAqB,CACzB,iBACA,WACuB;AACvB,QAAM,oBAAkC;AAAA,IACtC;AAAA,IACA;AAAA,EACF;AAEA,SAAO,kBAAkB,mBAA8B,MAAM;AAC/D;AAEA,MAAM,qBAAqB,CACzB,oBACA,WAC4B;AAC5B,SAAO,CAAC,aAA8C;AACpD,UAAM,oBAAkC;AAAA,MACtC;AAAA,MACA;AAAA,IACF;AAGA,WAAO,kBAAkB,mBAA8B,MAAM;AAAA,EAC/D;AACF;AAEO,MAAM,cAAc,CACzB,OACA,WAC4B;AAC5B,MAAI,OAAO,UAAU,YAAY,UAAU,MAAM;AAC/C,WAAO;AAAA,EACT;AAEA,MAAI,MAAM,aAAa,SAAS,aAAa;AAC3C,WAAO,mBAAmB,OAAwC,MAAM;AAAA,EAC1E;AAEA,MAAI,MAAM,aAAa,SAAS,aAAa;AAC3C,WAAO;AAAA,MACL;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAEA,MAAI,OAAQ,MAAc,SAAS,aAAa;AAC9C,WAAO,kBAAkB,OAAkB,MAAM;AAAA,EACnD;AAEA,SAAO;AACT;AAKO,MAAM,oBAAoB,CAC/B,SACA,SAAkB,kBACK;AACvB,MAAI,WAAW,OAAO,YAAY,UAAU;AAC1C,UAAM,SAA6B,CAAC;AAGpC,eAAW,OAAO,OAAO,KAAK,OAAO,GAAG;AACtC,YAAM,QAAQ,QAAQ,GAAG;AAEzB,aAAO,GAAG,IAAI,YAAY,OAAO,MAAM;AAAA,IACzC;AAEA,WAAO;AAAA,EACT;AAGA,SAAO;AACT;","names":[]}
@@ -2,7 +2,10 @@ import * as react from 'react';
2
2
  import { PropsWithChildren, FC } from 'react';
3
3
  import { Locales } from '@intlayer/config/client';
4
4
 
5
- declare const LocaleServerContext: {
5
+ /**
6
+ * Context that store the current locale on the server side
7
+ */
8
+ declare const IntlayerServerContext: {
6
9
  Provider: ({ children, value, }: {
7
10
  children: react.ReactNode;
8
11
  value: Locales;
@@ -15,10 +18,20 @@ declare const LocaleServerContext: {
15
18
  };
16
19
  _defaultValue: Locales | undefined;
17
20
  };
21
+ /**
22
+ * Hook that provides the current locale
23
+ */
24
+ declare const useIntlayer: () => Locales | undefined;
25
+ /**
26
+ * Get the current locale
27
+ */
18
28
  declare const locale: Locales | undefined;
19
- type LocaleServerContextProviderProps = PropsWithChildren & {
29
+ type IntlayerServerProviderProps = PropsWithChildren & {
20
30
  locale: Locales;
21
31
  };
22
- declare const LocaleServerContextProvider: FC<LocaleServerContextProviderProps>;
32
+ /**
33
+ * Provider that store the current locale on the server side
34
+ */
35
+ declare const IntlayerServerProvider: FC<IntlayerServerProviderProps>;
23
36
 
24
- export { LocaleServerContext, LocaleServerContextProvider, type LocaleServerContextProviderProps, locale };
37
+ export { IntlayerServerContext, IntlayerServerProvider, type IntlayerServerProviderProps, locale, useIntlayer };
@@ -0,0 +1,21 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import {
3
+ intlayerIntlConfiguration
4
+ } from "@intlayer/config/client";
5
+ import { createServerContext, getServerContext } from './serverContext.mjs';
6
+ const IntlayerServerContext = createServerContext(
7
+ intlayerIntlConfiguration.defaultLocale
8
+ );
9
+ const useIntlayer = () => getServerContext(IntlayerServerContext);
10
+ const locale = getServerContext(IntlayerServerContext);
11
+ const IntlayerServerProvider = ({
12
+ children,
13
+ locale: locale2
14
+ }) => /* @__PURE__ */ jsx(IntlayerServerContext.Provider, { value: locale2, children });
15
+ export {
16
+ IntlayerServerContext,
17
+ IntlayerServerProvider,
18
+ locale,
19
+ useIntlayer
20
+ };
21
+ //# sourceMappingURL=IntlayerServerProvider.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/server/IntlayerServerProvider.tsx"],"sourcesContent":["import {\n type Locales,\n intlayerIntlConfiguration,\n} from '@intlayer/config/client';\nimport type { FC, PropsWithChildren } from 'react';\nimport { createServerContext, getServerContext } from './serverContext';\n\n/**\n * Context that store the current locale on the server side\n */\nexport const IntlayerServerContext = createServerContext<Locales>(\n intlayerIntlConfiguration.defaultLocale\n);\n\n/**\n * Hook that provides the current locale\n */\nexport const useIntlayer = () => getServerContext(IntlayerServerContext);\n\n/**\n * Get the current locale\n */\nexport const locale = getServerContext(IntlayerServerContext);\n\nexport type IntlayerServerProviderProps = PropsWithChildren & {\n locale: Locales;\n};\n\n/**\n * Provider that store the current locale on the server side\n */\nexport const IntlayerServerProvider: FC<IntlayerServerProviderProps> = ({\n children,\n locale,\n}) => (\n <IntlayerServerContext.Provider value={locale}>\n {children}\n </IntlayerServerContext.Provider>\n);\n"],"mappings":"AAmCE;AAnCF;AAAA,EAEE;AAAA,OACK;AAEP,SAAS,qBAAqB,wBAAwB;AAK/C,MAAM,wBAAwB;AAAA,EACnC,0BAA0B;AAC5B;AAKO,MAAM,cAAc,MAAM,iBAAiB,qBAAqB;AAKhE,MAAM,SAAS,iBAAiB,qBAAqB;AASrD,MAAM,yBAA0D,CAAC;AAAA,EACtE;AAAA,EACA,QAAAA;AACF,MACE,oBAAC,sBAAsB,UAAtB,EAA+B,OAAOA,SACpC,UACH;","names":["locale"]}
@@ -1,6 +1,9 @@
1
1
  import * as _intlayer_config_client from '@intlayer/config/client';
2
2
  import { LanguageContent } from '@intlayer/core';
3
3
 
4
+ /**
5
+ * On the server side, get the translation content based on the locale
6
+ */
4
7
  declare const getLocaleTranslation: <Content>(languageContent: LanguageContent<Content>) => {
5
8
  locale: _intlayer_config_client.Locales | undefined;
6
9
  content: Content;
@@ -1,9 +1,9 @@
1
1
  import { intlayerIntlConfiguration } from "@intlayer/config/client";
2
2
  import { getTranslationContent } from "@intlayer/core";
3
- import { LocaleServerContext } from './LocaleServerContextProvider.mjs';
3
+ import { IntlayerServerContext } from './IntlayerServerProvider.mjs';
4
4
  import { getServerContext } from './serverContext.mjs';
5
5
  const getLocaleTranslation = (languageContent) => {
6
- const locale = getServerContext(LocaleServerContext);
6
+ const locale = getServerContext(IntlayerServerContext);
7
7
  const content = getTranslationContent(
8
8
  languageContent,
9
9
  locale ?? intlayerIntlConfiguration.defaultLocale
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/server/getLocaleTranslation.ts"],"sourcesContent":["import { intlayerIntlConfiguration } from '@intlayer/config/client';\nimport { type LanguageContent, getTranslationContent } from '@intlayer/core';\nimport { LocaleServerContext } from './LocaleServerContextProvider';\nimport { getServerContext } from './serverContext';\n\nexport const getLocaleTranslation = <Content>(\n languageContent: LanguageContent<Content>\n) => {\n const locale = getServerContext(LocaleServerContext);\n const content = getTranslationContent<Content>(\n languageContent,\n locale ?? intlayerIntlConfiguration.defaultLocale\n );\n\n return {\n locale,\n content,\n };\n};\n"],"mappings":"AAAA,SAAS,iCAAiC;AAC1C,SAA+B,6BAA6B;AAC5D,SAAS,2BAA2B;AACpC,SAAS,wBAAwB;AAE1B,MAAM,uBAAuB,CAClC,oBACG;AACH,QAAM,SAAS,iBAAiB,mBAAmB;AACnD,QAAM,UAAU;AAAA,IACd;AAAA,IACA,UAAU,0BAA0B;AAAA,EACtC;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../../../src/server/getLocaleTranslation.ts"],"sourcesContent":["import { intlayerIntlConfiguration } from '@intlayer/config/client';\nimport { type LanguageContent, getTranslationContent } from '@intlayer/core';\nimport { IntlayerServerContext } from './IntlayerServerProvider';\nimport { getServerContext } from './serverContext';\n\n/**\n * On the server side, get the translation content based on the locale\n */\nexport const getLocaleTranslation = <Content>(\n languageContent: LanguageContent<Content>\n) => {\n const locale = getServerContext(IntlayerServerContext);\n const content = getTranslationContent<Content>(\n languageContent,\n locale ?? intlayerIntlConfiguration.defaultLocale\n );\n\n return {\n locale,\n content,\n };\n};\n"],"mappings":"AAAA,SAAS,iCAAiC;AAC1C,SAA+B,6BAA6B;AAC5D,SAAS,6BAA6B;AACtC,SAAS,wBAAwB;AAK1B,MAAM,uBAAuB,CAClC,oBACG;AACH,QAAM,SAAS,iBAAiB,qBAAqB;AACrD,QAAM,UAAU;AAAA,IACd;AAAA,IACA,UAAU,0BAA0B;AAAA,EACtC;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
@@ -1,6 +1,6 @@
1
1
  export { getLocaleTranslation as getLocaleContent } from './getLocaleTranslation.mjs';
2
2
  export { useTraduction } from './useTraduction.mjs';
3
- export { LocaleServerContext, LocaleServerContextProvider, locale } from './LocaleServerContextProvider.mjs';
3
+ export { IntlayerServerContext as IntlayerServer, IntlayerServerProvider, locale } from './IntlayerServerProvider.mjs';
4
4
  export { useIntlayer } from './useIntlayer.mjs';
5
5
  import '@intlayer/config/client';
6
6
  import '@intlayer/core';
@@ -1,14 +1,14 @@
1
1
  import { getLocaleTranslation } from './getLocaleTranslation.mjs';
2
2
  import { useTraduction } from './useTraduction.mjs';
3
3
  import {
4
- LocaleServerContext,
4
+ IntlayerServerContext,
5
5
  locale,
6
- LocaleServerContextProvider
7
- } from './LocaleServerContextProvider.mjs';
6
+ IntlayerServerProvider
7
+ } from './IntlayerServerProvider.mjs';
8
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,
@@ -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,7 +15,7 @@ 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
20
  declare const getServerContext: <T>({ _storage, _defaultValue, }: ServerContext<T>) => T | undefined;
21
21
  type ServerContext<T> = {
@@ -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":[]}
@@ -2,6 +2,11 @@ import { UseIntlayer } from '../useIntlayerBase.mjs';
2
2
  import '@intlayer/config/client';
3
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,10 +1,10 @@
1
1
  import {
2
2
  useIntlayerBase
3
3
  } from '../useIntlayerBase.mjs';
4
- import { LocaleServerContext } from './LocaleServerContextProvider.mjs';
4
+ import { IntlayerServerContext } from './IntlayerServerProvider.mjs';
5
5
  import { getServerContext } from './serverContext.mjs';
6
6
  const useIntlayer = (id, locale) => {
7
- const localeTarget = locale ?? getServerContext(LocaleServerContext);
7
+ const localeTarget = locale ?? getServerContext(IntlayerServerContext);
8
8
  return useIntlayerBase(id, localeTarget);
9
9
  };
10
10
  export {
@@ -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,5 +1,25 @@
1
1
  import { LanguageContent } from '@intlayer/core';
2
2
 
3
- declare const useTraduction: <Content>(languageContent: LanguageContent<Content>) => 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;
4
24
 
5
25
  export { useTraduction };
@@ -1,8 +1,8 @@
1
1
  import { getTranslation } from '../getTranslation.mjs';
2
- import { LocaleServerContext } from './LocaleServerContextProvider.mjs';
2
+ import { IntlayerServerContext } from './IntlayerServerProvider.mjs';
3
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
8
  export {
@@ -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,15 +1,12 @@
1
1
  import { Locales } from '@intlayer/config/client';
2
2
  import { IntlayerDictionaryTypesConnector } from 'intlayer';
3
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
4
  type StringFallback<T> = T extends never ? string : T;
11
5
  type DictionaryKeys = StringFallback<keyof IntlayerDictionaryTypesConnector>;
12
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
+ */
13
10
  declare const useIntlayerBase: UseIntlayer;
14
11
 
15
12
  export { type DictionaryKeys, type StringFallback, type UseIntlayer, useIntlayerBase };
@@ -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.1",
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": [
@@ -55,20 +55,20 @@
55
55
  "dependencies": {
56
56
  "js-cookie": "^3.0.5",
57
57
  "react-cookie": "^7.1.4",
58
- "@intlayer/config": "^1.0.1",
59
- "@intlayer/core": "^1.0.1",
60
- "@intlayer/dictionaries-entry": "^1.0.1",
61
- "intlayer": "^1.0.1"
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",
71
+ "typescript": "^5.4.5",
72
72
  "@utils/eslint-config": "^1.0.1",
73
73
  "@utils/ts-config": "^1.0.1"
74
74
  },
@@ -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
  };