@intlayer/react-i18next 8.12.5-canary.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.
- package/README.md +330 -0
- package/dist/cjs/I18nextProvider.cjs +25 -0
- package/dist/cjs/I18nextProvider.cjs.map +1 -0
- package/dist/cjs/Trans.cjs +64 -0
- package/dist/cjs/Trans.cjs.map +1 -0
- package/dist/cjs/_virtual/_rolldown/runtime.cjs +29 -0
- package/dist/cjs/helpers.cjs +67 -0
- package/dist/cjs/helpers.cjs.map +1 -0
- package/dist/cjs/index.cjs +49 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/initReactI18next.cjs +24 -0
- package/dist/cjs/initReactI18next.cjs.map +1 -0
- package/dist/cjs/plugin/index.cjs +62 -0
- package/dist/cjs/plugin/index.cjs.map +1 -0
- package/dist/cjs/useDictionary.cjs +64 -0
- package/dist/cjs/useDictionary.cjs.map +1 -0
- package/dist/cjs/useDictionaryDynamic.cjs +49 -0
- package/dist/cjs/useDictionaryDynamic.cjs.map +1 -0
- package/dist/cjs/useTranslation.cjs +109 -0
- package/dist/cjs/useTranslation.cjs.map +1 -0
- package/dist/cjs/useTranslationDictionary.cjs +57 -0
- package/dist/cjs/useTranslationDictionary.cjs.map +1 -0
- package/dist/cjs/useTranslationDictionaryDynamic.cjs +49 -0
- package/dist/cjs/useTranslationDictionaryDynamic.cjs.map +1 -0
- package/dist/cjs/withTranslation.cjs +28 -0
- package/dist/cjs/withTranslation.cjs.map +1 -0
- package/dist/esm/I18nextProvider.mjs +23 -0
- package/dist/esm/I18nextProvider.mjs.map +1 -0
- package/dist/esm/Trans.mjs +62 -0
- package/dist/esm/Trans.mjs.map +1 -0
- package/dist/esm/helpers.mjs +54 -0
- package/dist/esm/helpers.mjs.map +1 -0
- package/dist/esm/index.mjs +28 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/initReactI18next.mjs +23 -0
- package/dist/esm/initReactI18next.mjs.map +1 -0
- package/dist/esm/plugin/index.mjs +58 -0
- package/dist/esm/plugin/index.mjs.map +1 -0
- package/dist/esm/useDictionary.mjs +62 -0
- package/dist/esm/useDictionary.mjs.map +1 -0
- package/dist/esm/useDictionaryDynamic.mjs +47 -0
- package/dist/esm/useDictionaryDynamic.mjs.map +1 -0
- package/dist/esm/useTranslation.mjs +107 -0
- package/dist/esm/useTranslation.mjs.map +1 -0
- package/dist/esm/useTranslationDictionary.mjs +55 -0
- package/dist/esm/useTranslationDictionary.mjs.map +1 -0
- package/dist/esm/useTranslationDictionaryDynamic.mjs +47 -0
- package/dist/esm/useTranslationDictionaryDynamic.mjs.map +1 -0
- package/dist/esm/withTranslation.mjs +26 -0
- package/dist/esm/withTranslation.mjs.map +1 -0
- package/dist/types/I18nextProvider.d.ts +13 -0
- package/dist/types/I18nextProvider.d.ts.map +1 -0
- package/dist/types/Trans.d.ts +22 -0
- package/dist/types/Trans.d.ts.map +1 -0
- package/dist/types/helpers.d.ts +35 -0
- package/dist/types/helpers.d.ts.map +1 -0
- package/dist/types/index.d.ts +27 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/initReactI18next.d.ts +16 -0
- package/dist/types/initReactI18next.d.ts.map +1 -0
- package/dist/types/plugin/index.d.ts +12 -0
- package/dist/types/plugin/index.d.ts.map +1 -0
- package/dist/types/useDictionary.d.ts +32 -0
- package/dist/types/useDictionary.d.ts.map +1 -0
- package/dist/types/useDictionaryDynamic.d.ts +22 -0
- package/dist/types/useDictionaryDynamic.d.ts.map +1 -0
- package/dist/types/useTranslation.d.ts +43 -0
- package/dist/types/useTranslation.d.ts.map +1 -0
- package/dist/types/useTranslationDictionary.d.ts +29 -0
- package/dist/types/useTranslationDictionary.d.ts.map +1 -0
- package/dist/types/useTranslationDictionaryDynamic.d.ts +22 -0
- package/dist/types/useTranslationDictionaryDynamic.d.ts.map +1 -0
- package/dist/types/withTranslation.d.ts +10 -0
- package/dist/types/withTranslation.d.ts.map +1 -0
- package/package.json +118 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["nodesToStringImpl","setDefaultsImpl","getDefaultsImpl","setI18nImpl","getI18nImpl","composeInitialPropsImpl","getInitialPropsImpl","useSSRImpl","withSSRImpl","TranslationImpl","I18nContextImpl","Trans"],"sources":["../../src/index.ts"],"sourcesContent":["import type {\n composeInitialProps as _composeInitialProps,\n getDefaults as _getDefaults,\n getI18n as _getI18n,\n getInitialProps as _getInitialProps,\n I18nContext as _I18nContext,\n setDefaults as _setDefaults,\n setI18n as _setI18n,\n Trans as _Trans,\n Translation as _Translation,\n TransWithoutContext as _TransWithoutContext,\n useSSR as _useSSR,\n withSSR as _withSSR,\n} from 'react-i18next';\n\nimport {\n composeInitialProps as composeInitialPropsImpl,\n getDefaults as getDefaultsImpl,\n getI18n as getI18nImpl,\n getInitialProps as getInitialPropsImpl,\n I18nContext as I18nContextImpl,\n nodesToString as nodesToStringImpl,\n setDefaults as setDefaultsImpl,\n setI18n as setI18nImpl,\n Translation as TranslationImpl,\n useSSR as useSSRImpl,\n withSSR as withSSRImpl,\n} from './helpers';\n\nimport { Trans } from './Trans';\n\n// Re-export common types from react-i18next so consumers don't need both packages\nexport type {\n I18nextProviderProps,\n TransProps,\n UseTranslationOptions,\n UseTranslationResponse,\n WithTranslation,\n WithTranslationProps,\n} from 'react-i18next';\nexport { I18nextProvider } from './I18nextProvider';\nexport { initReactI18next } from './initReactI18next';\nexport { useDictionary } from './useDictionary';\nexport { useDictionaryDynamic } from './useDictionaryDynamic';\nexport { useTranslation } from './useTranslation';\nexport { withTranslation } from './withTranslation';\nexport { Trans };\n\n// Wrap and export all implementation functions.\n// `nodesToString` is not part of react-i18next's public type surface in all\n// supported versions, so the local implementation's inferred type is exposed.\nexport const nodesToString = nodesToStringImpl;\nexport const setDefaults = setDefaultsImpl as typeof _setDefaults;\nexport const getDefaults = getDefaultsImpl as typeof _getDefaults;\nexport const setI18n = setI18nImpl as typeof _setI18n;\nexport const getI18n = getI18nImpl as typeof _getI18n;\nexport const composeInitialProps =\n composeInitialPropsImpl as typeof _composeInitialProps;\nexport const getInitialProps = getInitialPropsImpl as typeof _getInitialProps;\nexport const useSSR = useSSRImpl as typeof _useSSR;\nexport const withSSR = withSSRImpl as typeof _withSSR;\nexport const Translation = TranslationImpl as typeof _Translation;\nexport const I18nContext = I18nContextImpl as typeof _I18nContext;\n\nexport const TransWithoutContext = Trans as typeof _TransWithoutContext;\n// `IcuTrans` / `IcuTransWithoutContext` are aliases for the standard `Trans`\n// component (they only differ by interpolation engine, which the compat shim\n// does not distinguish). They are typed against `Trans` for broad version\n// compatibility, since the dedicated ICU exports are not present in every\n// supported react-i18next release.\nexport const IcuTrans = Trans as typeof _Trans;\nexport const IcuTransWithoutContext = Trans as typeof _Trans;\n"],"mappings":";;;;;;;;;;;AAmDA,MAAa,gBAAgBA;AAC7B,MAAa,cAAcC;AAC3B,MAAa,cAAcC;AAC3B,MAAa,UAAUC;AACvB,MAAa,UAAUC;AACvB,MAAa,sBACXC;AACF,MAAa,kBAAkBC;AAC/B,MAAa,SAASC;AACtB,MAAa,UAAUC;AACvB,MAAa,cAAcC;AAC3B,MAAa,cAAcC;AAE3B,MAAa,sBAAsBC;AAMnC,MAAa,WAAWA;AACxB,MAAa,yBAAyBA"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_helpers = require('./helpers.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/initReactI18next.ts
|
|
5
|
+
/**
|
|
6
|
+
* Drop-in for react-i18next's `initReactI18next` plugin.
|
|
7
|
+
*
|
|
8
|
+
* Implemented locally rather than re-exported from `react-i18next`: when the
|
|
9
|
+
* compat alias plugin is enabled, `react-i18next` resolves back to this package,
|
|
10
|
+
* so a re-export would be self-referential (resolving to `undefined`). The
|
|
11
|
+
* plugin records the i18n instance via the compat `setI18n`/`setDefaults`
|
|
12
|
+
* helpers; actual translation is served by Intlayer.
|
|
13
|
+
*/
|
|
14
|
+
const initReactI18next = {
|
|
15
|
+
type: "3rdParty",
|
|
16
|
+
init: (instance) => {
|
|
17
|
+
require_helpers.setI18n(instance);
|
|
18
|
+
require_helpers.setDefaults({});
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
exports.initReactI18next = initReactI18next;
|
|
24
|
+
//# sourceMappingURL=initReactI18next.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"initReactI18next.cjs","names":[],"sources":["../../src/initReactI18next.ts"],"sourcesContent":["import type { initReactI18next as _initReactI18next } from 'react-i18next';\nimport { setDefaults, setI18n } from './helpers';\n\n/**\n * Drop-in for react-i18next's `initReactI18next` plugin.\n *\n * Implemented locally rather than re-exported from `react-i18next`: when the\n * compat alias plugin is enabled, `react-i18next` resolves back to this package,\n * so a re-export would be self-referential (resolving to `undefined`). The\n * plugin records the i18n instance via the compat `setI18n`/`setDefaults`\n * helpers; actual translation is served by Intlayer.\n */\nexport const initReactI18next: typeof _initReactI18next = {\n type: '3rdParty' as const,\n init: (instance) => {\n setI18n(instance as never);\n setDefaults({});\n },\n};\n"],"mappings":";;;;;;;;;;;;;AAYA,MAAa,mBAA6C;CACxD,MAAM;CACN,OAAO,aAAa;AAClB,0BAAQ,SAAkB;AAC1B,8BAAY,EAAE,CAAC;;CAElB"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
|
|
2
|
+
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
|
|
3
|
+
let _intlayer_config_colors = require("@intlayer/config/colors");
|
|
4
|
+
_intlayer_config_colors = require_runtime.__toESM(_intlayer_config_colors);
|
|
5
|
+
let _intlayer_config_logger = require("@intlayer/config/logger");
|
|
6
|
+
let node_path = require("node:path");
|
|
7
|
+
let _intlayer_chokidar_utils = require("@intlayer/chokidar/utils");
|
|
8
|
+
let _intlayer_config_node = require("@intlayer/config/node");
|
|
9
|
+
let vite_intlayer = require("vite-intlayer");
|
|
10
|
+
|
|
11
|
+
//#region src/plugin/index.ts
|
|
12
|
+
/**
|
|
13
|
+
* Caller configurations for `react-i18next`'s `useTranslation` hook.
|
|
14
|
+
*
|
|
15
|
+
* Tells the intlayer field-usage analyser how to extract the dictionary key
|
|
16
|
+
* (namespace) and consumed fields from `useTranslation` call sites, enabling
|
|
17
|
+
* accurate dictionary pruning for projects using `@intlayer/react-i18next`.
|
|
18
|
+
*/
|
|
19
|
+
const REACT_I18NEXT_COMPAT_CALLERS = [{
|
|
20
|
+
callerName: "useTranslation",
|
|
21
|
+
importSources: ["react-i18next", "@intlayer/react-i18next"],
|
|
22
|
+
namespace: {
|
|
23
|
+
from: "argument",
|
|
24
|
+
index: 0
|
|
25
|
+
},
|
|
26
|
+
keyPrefix: {
|
|
27
|
+
from: "option",
|
|
28
|
+
argumentIndex: 1,
|
|
29
|
+
property: "keyPrefix"
|
|
30
|
+
},
|
|
31
|
+
translationFunction: "destructured-t"
|
|
32
|
+
}];
|
|
33
|
+
/**
|
|
34
|
+
* A Vite plugin for react-i18next compat that wraps vite-intlayer
|
|
35
|
+
* and configures resolve aliases for react-i18next and i18next.
|
|
36
|
+
*/
|
|
37
|
+
const reactI18nextVitePlugin = (options) => {
|
|
38
|
+
const intlayerConfig = (0, _intlayer_config_node.getConfiguration)();
|
|
39
|
+
const appLogger = (0, _intlayer_config_logger.getAppLogger)(intlayerConfig);
|
|
40
|
+
(0, _intlayer_chokidar_utils.runOnce)((0, node_path.join)(intlayerConfig.system.baseDir, ".intlayer", "cache", "intlayer-issues-invitation.lock"), () => {
|
|
41
|
+
appLogger([(0, _intlayer_config_logger.colorize)("Please report any issues you met on GitHub:", _intlayer_config_colors.GREY), (0, _intlayer_config_logger.colorize)("https://github.com/aymericzip/intlayer/issues", _intlayer_config_colors.GREY_LIGHT)]);
|
|
42
|
+
}, { cacheTimeoutMs: 1e3 * 60 * 60 });
|
|
43
|
+
const basePlugins = (0, vite_intlayer.intlayer)({
|
|
44
|
+
...options,
|
|
45
|
+
compatCallers: [...options?.compatCallers ?? [], ...REACT_I18NEXT_COMPAT_CALLERS]
|
|
46
|
+
});
|
|
47
|
+
const compatPlugin = {
|
|
48
|
+
name: "vite-react-i18next-compat-plugin",
|
|
49
|
+
config: () => {
|
|
50
|
+
return { resolve: { alias: {
|
|
51
|
+
"react-i18next": "@intlayer/react-i18next",
|
|
52
|
+
i18next: "@intlayer/i18next"
|
|
53
|
+
} } };
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
return [...Array.isArray(basePlugins) ? basePlugins : [basePlugins], compatPlugin];
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
//#endregion
|
|
60
|
+
exports.default = reactI18nextVitePlugin;
|
|
61
|
+
exports.reactI18nextVitePlugin = reactI18nextVitePlugin;
|
|
62
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["ANSIColors"],"sources":["../../../src/plugin/index.ts"],"sourcesContent":["import { join } from 'node:path';\nimport { runOnce } from '@intlayer/chokidar/utils';\nimport * as ANSIColors from '@intlayer/config/colors';\nimport { colorize, getAppLogger } from '@intlayer/config/logger';\nimport { getConfiguration } from '@intlayer/config/node';\nimport type { PluginOption } from 'vite';\nimport { type CompatCallerConfig, intlayer } from 'vite-intlayer';\n\n/**\n * Caller configurations for `react-i18next`'s `useTranslation` hook.\n *\n * Tells the intlayer field-usage analyser how to extract the dictionary key\n * (namespace) and consumed fields from `useTranslation` call sites, enabling\n * accurate dictionary pruning for projects using `@intlayer/react-i18next`.\n */\nconst REACT_I18NEXT_COMPAT_CALLERS: CompatCallerConfig[] = [\n {\n callerName: 'useTranslation',\n importSources: ['react-i18next', '@intlayer/react-i18next'],\n namespace: { from: 'argument', index: 0 },\n keyPrefix: { from: 'option', argumentIndex: 1, property: 'keyPrefix' },\n translationFunction: 'destructured-t',\n },\n];\n\n/**\n * A Vite plugin for react-i18next compat that wraps vite-intlayer\n * and configures resolve aliases for react-i18next and i18next.\n */\nexport const reactI18nextVitePlugin = (\n options?: Parameters<typeof intlayer>[0]\n): PluginOption[] => {\n const intlayerConfig = getConfiguration();\n const appLogger = getAppLogger(intlayerConfig);\n\n runOnce(\n join(\n intlayerConfig.system.baseDir,\n '.intlayer',\n 'cache',\n 'intlayer-issues-invitation.lock'\n ),\n () => {\n appLogger([\n colorize(\n 'Please report any issues you met on GitHub:',\n ANSIColors.GREY\n ),\n colorize(\n 'https://github.com/aymericzip/intlayer/issues',\n ANSIColors.GREY_LIGHT\n ),\n ]);\n },\n {\n cacheTimeoutMs: 1000 * 60 * 60, // 1 hour\n }\n );\n\n const basePlugins = intlayer({\n ...options,\n compatCallers: [\n ...(options?.compatCallers ?? []),\n ...REACT_I18NEXT_COMPAT_CALLERS,\n ],\n });\n\n const compatPlugin: PluginOption = {\n name: 'vite-react-i18next-compat-plugin',\n config: () => {\n return {\n resolve: {\n alias: {\n 'react-i18next': '@intlayer/react-i18next',\n i18next: '@intlayer/i18next',\n },\n },\n };\n },\n };\n\n return [\n ...(Array.isArray(basePlugins) ? basePlugins : [basePlugins]),\n compatPlugin,\n ];\n};\n\nexport default reactI18nextVitePlugin;\n"],"mappings":";;;;;;;;;;;;;;;;;;AAeA,MAAM,+BAAqD,CACzD;CACE,YAAY;CACZ,eAAe,CAAC,iBAAiB,0BAA0B;CAC3D,WAAW;EAAE,MAAM;EAAY,OAAO;EAAG;CACzC,WAAW;EAAE,MAAM;EAAU,eAAe;EAAG,UAAU;EAAa;CACtE,qBAAqB;CACtB,CACF;;;;;AAMD,MAAa,0BACX,YACmB;CACnB,MAAM,8DAAmC;CACzC,MAAM,sDAAyB,eAAe;AAE9C,2DAEI,eAAe,OAAO,SACtB,aACA,SACA,kCACD,QACK;AACJ,YAAU,uCAEN,+CACAA,wBAAW,KACZ,wCAEC,iDACAA,wBAAW,WACZ,CACF,CAAC;IAEJ,EACE,gBAAgB,MAAO,KAAK,IAC7B,CACF;CAED,MAAM,0CAAuB;EAC3B,GAAG;EACH,eAAe,CACb,GAAI,SAAS,iBAAiB,EAAE,EAChC,GAAG,6BACJ;EACF,CAAC;CAEF,MAAM,eAA6B;EACjC,MAAM;EACN,cAAc;AACZ,UAAO,EACL,SAAS,EACP,OAAO;IACL,iBAAiB;IACjB,SAAS;IACV,EACF,EACF;;EAEJ;AAED,QAAO,CACL,GAAI,MAAM,QAAQ,YAAY,GAAG,cAAc,CAAC,YAAY,EAC5D,aACD"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
5
|
+
let react = require("react");
|
|
6
|
+
let react_intlayer = require("react-intlayer");
|
|
7
|
+
|
|
8
|
+
//#region src/useDictionary.ts
|
|
9
|
+
const navigatePath = (objectValue, path) => {
|
|
10
|
+
if (!path) return objectValue;
|
|
11
|
+
let current = objectValue;
|
|
12
|
+
for (const part of path.split(".")) {
|
|
13
|
+
if (current === null || current === void 0 || typeof current !== "object") return;
|
|
14
|
+
current = current[part];
|
|
15
|
+
}
|
|
16
|
+
return current;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Dictionary-accepting variant of `useTranslation`.
|
|
20
|
+
*
|
|
21
|
+
* Used internally by the SWC optimization: instead of looking up the dictionary
|
|
22
|
+
* at runtime by key, the SWC plugin pre-imports the dictionary JSON at build time
|
|
23
|
+
* and passes it directly here. This enables tree-shaking of unused locale content.
|
|
24
|
+
*
|
|
25
|
+
* For a nested namespace (`useTranslation('about.counter')`), the SWC passes the
|
|
26
|
+
* key prefix (`'counter'`) as the second argument.
|
|
27
|
+
*
|
|
28
|
+
* @example (generated by SWC, not written manually)
|
|
29
|
+
* import _abc from '.intlayer/dictionaries/about.json' with { type: 'json' };
|
|
30
|
+
* const { t } = useDictionary(_abc);
|
|
31
|
+
*/
|
|
32
|
+
const useDictionary = (dictionary, keyPrefix, options) => {
|
|
33
|
+
const content = (0, react_intlayer.useDictionary)(dictionary);
|
|
34
|
+
const { locale, setLocale, availableLocales } = (0, react_intlayer.useLocale)();
|
|
35
|
+
const i18n = (0, react.useMemo)(() => ({
|
|
36
|
+
language: locale,
|
|
37
|
+
languages: availableLocales ?? [],
|
|
38
|
+
changeLanguage: async (lng) => {
|
|
39
|
+
setLocale(lng);
|
|
40
|
+
},
|
|
41
|
+
isInitialized: true
|
|
42
|
+
}), [
|
|
43
|
+
locale,
|
|
44
|
+
availableLocales,
|
|
45
|
+
setLocale
|
|
46
|
+
]);
|
|
47
|
+
const prefix = (typeof keyPrefix === "object" ? keyPrefix : options)?.keyPrefix ?? (typeof keyPrefix === "string" ? keyPrefix : void 0);
|
|
48
|
+
const t = (key, params) => {
|
|
49
|
+
const lookupKey = prefix ? `${prefix}.${String(key)}` : String(key);
|
|
50
|
+
const rawValue = navigatePath(content, lookupKey);
|
|
51
|
+
const str = String(rawValue ?? lookupKey);
|
|
52
|
+
if (!params) return str;
|
|
53
|
+
return str.replace(/\{\{(\w+)\}\}/g, (_, name) => params[name] != null ? String(params[name]) : `{{${name}}}`);
|
|
54
|
+
};
|
|
55
|
+
return {
|
|
56
|
+
t,
|
|
57
|
+
i18n,
|
|
58
|
+
ready: true
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
//#endregion
|
|
63
|
+
exports.useDictionary = useDictionary;
|
|
64
|
+
//# sourceMappingURL=useDictionary.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDictionary.cjs","names":[],"sources":["../../src/useDictionary.ts"],"sourcesContent":["'use client';\n\nimport type { ValidDotPathsFor } from '@intlayer/core/transpiler';\nimport type { Dictionary } from '@intlayer/types/dictionary';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\nimport { useMemo } from 'react';\nimport type { UseTranslationOptions } from 'react-i18next';\nimport { useDictionary as useDictionaryBase, useLocale } from 'react-intlayer';\n\nconst navigatePath = (objectValue: unknown, path: string): unknown => {\n if (!path) return objectValue;\n let current: unknown = objectValue;\n for (const part of path.split('.')) {\n if (\n current === null ||\n current === undefined ||\n typeof current !== 'object'\n ) {\n return undefined;\n }\n current = (current as Record<string, unknown>)[part];\n }\n return current;\n};\n\n/**\n * Dictionary-accepting variant of `useTranslation`.\n *\n * Used internally by the SWC optimization: instead of looking up the dictionary\n * at runtime by key, the SWC plugin pre-imports the dictionary JSON at build time\n * and passes it directly here. This enables tree-shaking of unused locale content.\n *\n * For a nested namespace (`useTranslation('about.counter')`), the SWC passes the\n * key prefix (`'counter'`) as the second argument.\n *\n * @example (generated by SWC, not written manually)\n * import _abc from '.intlayer/dictionaries/about.json' with { type: 'json' };\n * const { t } = useDictionary(_abc);\n */\nexport const useDictionary = <T extends Dictionary>(\n dictionary: T,\n keyPrefix?: string | UseTranslationOptions<string>,\n options?: UseTranslationOptions<string>\n) => {\n const content = useDictionaryBase(dictionary);\n const { locale, setLocale, availableLocales } = useLocale();\n\n const i18n = useMemo(\n () => ({\n language: locale as string,\n languages: (availableLocales ?? []) as string[],\n changeLanguage: async (lng: string) => {\n setLocale(lng as LocalesValues);\n },\n isInitialized: true,\n }),\n [locale, availableLocales, setLocale]\n );\n\n const resolvedOptions = typeof keyPrefix === 'object' ? keyPrefix : options;\n const prefix =\n resolvedOptions?.keyPrefix ??\n (typeof keyPrefix === 'string' ? keyPrefix : undefined);\n\n const t = <P extends ValidDotPathsFor<any>>(\n key: P,\n params?: Record<string, unknown>\n ): string => {\n const lookupKey = prefix ? `${prefix}.${String(key)}` : String(key);\n const rawValue = navigatePath(content, lookupKey);\n const str = String(rawValue ?? lookupKey);\n\n if (!params) return str;\n\n return str.replace(/\\{\\{(\\w+)\\}\\}/g, (_, name) =>\n params[name] != null ? String(params[name]) : `{{${name}}}`\n );\n };\n\n return { t, i18n, ready: true };\n};\n"],"mappings":";;;;;;;;AASA,MAAM,gBAAgB,aAAsB,SAA0B;AACpE,KAAI,CAAC,KAAM,QAAO;CAClB,IAAI,UAAmB;AACvB,MAAK,MAAM,QAAQ,KAAK,MAAM,IAAI,EAAE;AAClC,MACE,YAAY,QACZ,YAAY,UACZ,OAAO,YAAY,SAEnB;AAEF,YAAW,QAAoC;;AAEjD,QAAO;;;;;;;;;;;;;;;;AAiBT,MAAa,iBACX,YACA,WACA,YACG;CACH,MAAM,4CAA4B,WAAW;CAC7C,MAAM,EAAE,QAAQ,WAAW,oDAAgC;CAE3D,MAAM,iCACG;EACL,UAAU;EACV,WAAY,oBAAoB,EAAE;EAClC,gBAAgB,OAAO,QAAgB;AACrC,aAAU,IAAqB;;EAEjC,eAAe;EAChB,GACD;EAAC;EAAQ;EAAkB;EAAU,CACtC;CAGD,MAAM,UADkB,OAAO,cAAc,WAAW,YAAY,UAEjD,cAChB,OAAO,cAAc,WAAW,YAAY;CAE/C,MAAM,KACJ,KACA,WACW;EACX,MAAM,YAAY,SAAS,GAAG,OAAO,GAAG,OAAO,IAAI,KAAK,OAAO,IAAI;EACnE,MAAM,WAAW,aAAa,SAAS,UAAU;EACjD,MAAM,MAAM,OAAO,YAAY,UAAU;AAEzC,MAAI,CAAC,OAAQ,QAAO;AAEpB,SAAO,IAAI,QAAQ,mBAAmB,GAAG,SACvC,OAAO,SAAS,OAAO,OAAO,OAAO,MAAM,GAAG,KAAK,KAAK,IACzD;;AAGH,QAAO;EAAE;EAAG;EAAM,OAAO;EAAM"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
5
|
+
let react = require("react");
|
|
6
|
+
let react_intlayer = require("react-intlayer");
|
|
7
|
+
|
|
8
|
+
//#region src/useDictionaryDynamic.ts
|
|
9
|
+
/**
|
|
10
|
+
* Dynamic dictionary-accepting variant of `useTranslation`.
|
|
11
|
+
*/
|
|
12
|
+
const useDictionaryDynamic = (dictionaryPromise, key, options) => {
|
|
13
|
+
const content = (0, react_intlayer.useDictionaryDynamic)(dictionaryPromise, key);
|
|
14
|
+
const { locale, setLocale, availableLocales } = (0, react_intlayer.useLocale)();
|
|
15
|
+
const i18n = (0, react.useMemo)(() => ({
|
|
16
|
+
language: locale,
|
|
17
|
+
languages: availableLocales ?? [],
|
|
18
|
+
changeLanguage: async (lng) => {
|
|
19
|
+
setLocale(lng);
|
|
20
|
+
},
|
|
21
|
+
isInitialized: true
|
|
22
|
+
}), [
|
|
23
|
+
locale,
|
|
24
|
+
availableLocales,
|
|
25
|
+
setLocale
|
|
26
|
+
]);
|
|
27
|
+
const prefix = options?.keyPrefix;
|
|
28
|
+
const t = (lookup, params) => {
|
|
29
|
+
const lookupKey = prefix ? `${prefix}.${String(lookup)}` : String(lookup);
|
|
30
|
+
const parts = lookupKey.split(".");
|
|
31
|
+
let current = content;
|
|
32
|
+
for (const part of parts) {
|
|
33
|
+
if (current == null) break;
|
|
34
|
+
current = current[part];
|
|
35
|
+
}
|
|
36
|
+
const str = String(current ?? lookupKey);
|
|
37
|
+
if (!params) return str;
|
|
38
|
+
return str.replace(/\{\{(\w+)\}\}/g, (_, k) => params[k] != null ? String(params[k]) : `{{${k}}}`);
|
|
39
|
+
};
|
|
40
|
+
return {
|
|
41
|
+
t,
|
|
42
|
+
i18n,
|
|
43
|
+
ready: true
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
//#endregion
|
|
48
|
+
exports.useDictionaryDynamic = useDictionaryDynamic;
|
|
49
|
+
//# sourceMappingURL=useDictionaryDynamic.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDictionaryDynamic.cjs","names":[],"sources":["../../src/useDictionaryDynamic.ts"],"sourcesContent":["'use client';\n\nimport type { ValidDotPathsFor } from '@intlayer/core/transpiler';\nimport type { Dictionary } from '@intlayer/types/dictionary';\nimport type { StrictModeLocaleMap } from '@intlayer/types/module_augmentation';\nimport { useMemo } from 'react';\nimport type { UseTranslationOptions } from 'react-i18next';\nimport {\n useDictionaryDynamic as useDictionaryDynamicBase,\n useLocale,\n} from 'react-intlayer';\n\n/**\n * Dynamic dictionary-accepting variant of `useTranslation`.\n */\nexport const useDictionaryDynamic = <\n const T extends Dictionary,\n const K extends string,\n>(\n dictionaryPromise: StrictModeLocaleMap<() => Promise<T>>,\n key: K,\n options?: UseTranslationOptions<string>\n) => {\n const content = useDictionaryDynamicBase<T, any>(\n dictionaryPromise,\n key as any\n );\n const { locale, setLocale, availableLocales } = useLocale();\n\n const i18n = useMemo(\n () => ({\n language: locale as string,\n languages: (availableLocales ?? []) as string[],\n changeLanguage: async (lng: string) => {\n setLocale(lng as any);\n },\n isInitialized: true,\n }),\n [locale, availableLocales, setLocale]\n );\n\n const prefix = options?.keyPrefix;\n\n const t = <P extends ValidDotPathsFor<any>>(\n lookup: P,\n params?: Record<string, unknown>\n ): string => {\n const lookupKey = prefix ? `${prefix}.${String(lookup)}` : String(lookup);\n const parts = lookupKey.split('.');\n let current: any = content;\n\n for (const part of parts) {\n if (current == null) break;\n current = current[part];\n }\n\n const str = String(current ?? lookupKey);\n\n if (!params) return str;\n\n return str.replace(/\\{\\{(\\w+)\\}\\}/g, (_, k) =>\n params[k] != null ? String(params[k]) : `{{${k}}}`\n );\n };\n\n return { t, i18n, ready: true };\n};\n"],"mappings":";;;;;;;;;;;AAeA,MAAa,wBAIX,mBACA,KACA,YACG;CACH,MAAM,mDACJ,mBACA,IACD;CACD,MAAM,EAAE,QAAQ,WAAW,oDAAgC;CAE3D,MAAM,iCACG;EACL,UAAU;EACV,WAAY,oBAAoB,EAAE;EAClC,gBAAgB,OAAO,QAAgB;AACrC,aAAU,IAAW;;EAEvB,eAAe;EAChB,GACD;EAAC;EAAQ;EAAkB;EAAU,CACtC;CAED,MAAM,SAAS,SAAS;CAExB,MAAM,KACJ,QACA,WACW;EACX,MAAM,YAAY,SAAS,GAAG,OAAO,GAAG,OAAO,OAAO,KAAK,OAAO,OAAO;EACzE,MAAM,QAAQ,UAAU,MAAM,IAAI;EAClC,IAAI,UAAe;AAEnB,OAAK,MAAM,QAAQ,OAAO;AACxB,OAAI,WAAW,KAAM;AACrB,aAAU,QAAQ;;EAGpB,MAAM,MAAM,OAAO,WAAW,UAAU;AAExC,MAAI,CAAC,OAAQ,QAAO;AAEpB,SAAO,IAAI,QAAQ,mBAAmB,GAAG,MACvC,OAAO,MAAM,OAAO,OAAO,OAAO,GAAG,GAAG,KAAK,EAAE,IAChD;;AAGH,QAAO;EAAE;EAAG;EAAM,OAAO;EAAM"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
5
|
+
let _intlayer_core_localization = require("@intlayer/core/localization");
|
|
6
|
+
let _intlayer_core_messageFormat = require("@intlayer/core/messageFormat");
|
|
7
|
+
let _intlayer_i18next = require("@intlayer/i18next");
|
|
8
|
+
let react = require("react");
|
|
9
|
+
let react_intlayer = require("react-intlayer");
|
|
10
|
+
|
|
11
|
+
//#region src/useTranslation.ts
|
|
12
|
+
/**
|
|
13
|
+
* Drop-in for react-i18next's `useTranslation`.
|
|
14
|
+
*
|
|
15
|
+
* Translation lookup goes through the shared i18next-dialect resolver:
|
|
16
|
+
* namespace prefixes (`ns:key`), the `ns` option, plural suffixes
|
|
17
|
+
* (`key_one`/`key_other` via `Intl.PluralRules`), context suffixes
|
|
18
|
+
* (`key_male`), `$t()` nesting, `defaultValue` and `{{var}}` interpolation
|
|
19
|
+
* are all supported.
|
|
20
|
+
*
|
|
21
|
+
* The returned `t()` is typed against the intlayer dictionary for namespace N:
|
|
22
|
+
* keys are autocompleted and dot-paths are validated at compile time.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```tsx
|
|
26
|
+
* const { t } = useTranslation('about');
|
|
27
|
+
* t('counter.label'); // ✓ typed; compile error if key doesn't exist
|
|
28
|
+
* t('items', { count: 3 }); // plural suffix resolution
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
const useTranslation = (ns, options) => {
|
|
32
|
+
const namespace = Array.isArray(ns) ? ns[0] ?? "translation" : ns ?? "translation";
|
|
33
|
+
const { locale, setLocale, availableLocales } = (0, react_intlayer.useLocale)();
|
|
34
|
+
const keyPrefix = options?.keyPrefix;
|
|
35
|
+
/**
|
|
36
|
+
* @param key - A valid dot-notation path within the namespace N dictionary.
|
|
37
|
+
* @param optionsOrDefaultValue - i18next `t()` options (interpolation
|
|
38
|
+
* values, `count`, `context`, `ns`, `defaultValue`, …) or a default
|
|
39
|
+
* value string.
|
|
40
|
+
*/
|
|
41
|
+
const translate = (0, react.useMemo)(() => {
|
|
42
|
+
const resolveSingleKey = (key, translateOptions) => (0, _intlayer_i18next.resolveTranslation)({
|
|
43
|
+
locale,
|
|
44
|
+
namespace,
|
|
45
|
+
key: keyPrefix ? `${keyPrefix}.${key}` : key,
|
|
46
|
+
options: translateOptions
|
|
47
|
+
});
|
|
48
|
+
return (key, optionsOrDefaultValue, extraOptions) => {
|
|
49
|
+
const translateOptions = typeof optionsOrDefaultValue === "string" ? {
|
|
50
|
+
defaultValue: optionsOrDefaultValue,
|
|
51
|
+
...extraOptions
|
|
52
|
+
} : optionsOrDefaultValue ?? {};
|
|
53
|
+
const keys = Array.isArray(key) ? key : [key];
|
|
54
|
+
for (const candidateKey of keys) {
|
|
55
|
+
const resolved = resolveSingleKey(candidateKey, translateOptions);
|
|
56
|
+
if (resolved !== void 0) return resolved;
|
|
57
|
+
}
|
|
58
|
+
const defaultValue = translateOptions.defaultValue;
|
|
59
|
+
if (typeof defaultValue === "string") return (0, _intlayer_core_messageFormat.resolveMessage)(defaultValue, (0, _intlayer_i18next.getInterpolationValues)(translateOptions), locale, "i18next");
|
|
60
|
+
return keys[keys.length - 1];
|
|
61
|
+
};
|
|
62
|
+
}, [
|
|
63
|
+
locale,
|
|
64
|
+
namespace,
|
|
65
|
+
keyPrefix
|
|
66
|
+
]);
|
|
67
|
+
return {
|
|
68
|
+
t: translate,
|
|
69
|
+
i18n: (0, react.useMemo)(() => ({
|
|
70
|
+
language: locale,
|
|
71
|
+
languages: availableLocales ?? [],
|
|
72
|
+
resolvedLanguage: locale,
|
|
73
|
+
isInitialized: true,
|
|
74
|
+
changeLanguage: async (newLanguage) => {
|
|
75
|
+
setLocale(newLanguage);
|
|
76
|
+
},
|
|
77
|
+
dir: (language) => {
|
|
78
|
+
return (0, _intlayer_core_localization.getHTMLTextDir)(language ?? locale) === "rtl" ? "rtl" : "ltr";
|
|
79
|
+
},
|
|
80
|
+
exists: (key, existsOptions) => (0, _intlayer_i18next.resolveTranslation)({
|
|
81
|
+
locale,
|
|
82
|
+
namespace,
|
|
83
|
+
key,
|
|
84
|
+
options: existsOptions
|
|
85
|
+
}) !== void 0,
|
|
86
|
+
t: translate,
|
|
87
|
+
getFixedT: (language, fixedNamespace) => (key, fixedOptions) => {
|
|
88
|
+
const resolved = (0, _intlayer_i18next.resolveTranslation)({
|
|
89
|
+
locale: language ?? locale,
|
|
90
|
+
namespace: fixedNamespace ?? namespace,
|
|
91
|
+
key,
|
|
92
|
+
options: fixedOptions
|
|
93
|
+
});
|
|
94
|
+
return resolved !== void 0 ? resolved : key;
|
|
95
|
+
}
|
|
96
|
+
}), [
|
|
97
|
+
locale,
|
|
98
|
+
availableLocales,
|
|
99
|
+
setLocale,
|
|
100
|
+
namespace,
|
|
101
|
+
translate
|
|
102
|
+
]),
|
|
103
|
+
ready: true
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
//#endregion
|
|
108
|
+
exports.useTranslation = useTranslation;
|
|
109
|
+
//# sourceMappingURL=useTranslation.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTranslation.cjs","names":[],"sources":["../../src/useTranslation.ts"],"sourcesContent":["'use client';\n\nimport { getHTMLTextDir } from '@intlayer/core/localization';\nimport { resolveMessage } from '@intlayer/core/messageFormat';\nimport type { ValidDotPathsFor } from '@intlayer/core/transpiler';\nimport { getInterpolationValues, resolveTranslation } from '@intlayer/i18next';\nimport type {\n DictionaryKeys,\n LocalesValues,\n} from '@intlayer/types/module_augmentation';\nimport type { TOptions } from 'i18next';\nimport { useMemo } from 'react';\nimport type { UseTranslationOptions } from 'react-i18next';\nimport { useLocale } from 'react-intlayer';\n\n/**\n * Drop-in for react-i18next's `useTranslation`.\n *\n * Translation lookup goes through the shared i18next-dialect resolver:\n * namespace prefixes (`ns:key`), the `ns` option, plural suffixes\n * (`key_one`/`key_other` via `Intl.PluralRules`), context suffixes\n * (`key_male`), `$t()` nesting, `defaultValue` and `{{var}}` interpolation\n * are all supported.\n *\n * The returned `t()` is typed against the intlayer dictionary for namespace N:\n * keys are autocompleted and dot-paths are validated at compile time.\n *\n * @example\n * ```tsx\n * const { t } = useTranslation('about');\n * t('counter.label'); // ✓ typed; compile error if key doesn't exist\n * t('items', { count: 3 }); // plural suffix resolution\n * ```\n */\nexport const useTranslation = <N extends DictionaryKeys>(\n ns?: N | N[],\n options?: UseTranslationOptions<string>\n) => {\n const namespace = (\n Array.isArray(ns) ? (ns[0] ?? 'translation') : (ns ?? 'translation')\n ) as N;\n\n const { locale, setLocale, availableLocales } = useLocale();\n\n const keyPrefix = options?.keyPrefix;\n\n /**\n * @param key - A valid dot-notation path within the namespace N dictionary.\n * @param optionsOrDefaultValue - i18next `t()` options (interpolation\n * values, `count`, `context`, `ns`, `defaultValue`, …) or a default\n * value string.\n */\n const translate = useMemo(() => {\n const resolveSingleKey = (\n key: string,\n translateOptions?: TOptions\n ): unknown =>\n resolveTranslation({\n locale: locale as LocalesValues,\n namespace,\n key: keyPrefix ? `${keyPrefix}.${key}` : key,\n options: translateOptions,\n });\n\n return (\n key: string | string[],\n optionsOrDefaultValue?: TOptions | string,\n extraOptions?: TOptions\n ): string => {\n const translateOptions: TOptions =\n typeof optionsOrDefaultValue === 'string'\n ? { defaultValue: optionsOrDefaultValue, ...extraOptions }\n : (optionsOrDefaultValue ?? {});\n\n const keys = Array.isArray(key) ? key : [key];\n\n for (const candidateKey of keys) {\n const resolved = resolveSingleKey(candidateKey, translateOptions);\n if (resolved !== undefined) return resolved as string;\n }\n\n const defaultValue = translateOptions.defaultValue;\n if (typeof defaultValue === 'string') {\n return resolveMessage(\n defaultValue,\n getInterpolationValues(translateOptions),\n locale as LocalesValues,\n 'i18next'\n );\n }\n\n return keys[keys.length - 1];\n };\n }, [locale, namespace, keyPrefix]);\n\n const i18n = useMemo(\n () => ({\n language: locale as string,\n languages: (availableLocales ?? []) as string[],\n resolvedLanguage: locale as string,\n isInitialized: true,\n changeLanguage: async (newLanguage: string) => {\n setLocale(newLanguage as LocalesValues);\n },\n dir: (language?: string): 'ltr' | 'rtl' => {\n const direction = getHTMLTextDir((language ?? locale) as LocalesValues);\n return direction === 'rtl' ? 'rtl' : 'ltr';\n },\n exists: (key: string, existsOptions?: TOptions): boolean =>\n resolveTranslation({\n locale: locale as LocalesValues,\n namespace,\n key,\n options: existsOptions,\n }) !== undefined,\n t: translate,\n getFixedT:\n (language?: string | null, fixedNamespace?: string | null) =>\n (key: string, fixedOptions?: TOptions): string => {\n const resolved = resolveTranslation({\n locale: (language ?? locale) as LocalesValues,\n namespace: fixedNamespace ?? namespace,\n key,\n options: fixedOptions,\n });\n return resolved !== undefined ? (resolved as string) : key;\n },\n }),\n [locale, availableLocales, setLocale, namespace, translate]\n );\n\n /** Typed facade over the untyped runtime translate function. */\n const t = translate as <P extends ValidDotPathsFor<N>>(\n key: P | P[],\n optionsOrDefaultValue?: TOptions | string,\n extraOptions?: TOptions\n ) => string;\n\n return { t, i18n, ready: true };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,MAAa,kBACX,IACA,YACG;CACH,MAAM,YACJ,MAAM,QAAQ,GAAG,GAAI,GAAG,MAAM,gBAAkB,MAAM;CAGxD,MAAM,EAAE,QAAQ,WAAW,oDAAgC;CAE3D,MAAM,YAAY,SAAS;;;;;;;CAQ3B,MAAM,qCAA0B;EAC9B,MAAM,oBACJ,KACA,+DAEmB;GACT;GACR;GACA,KAAK,YAAY,GAAG,UAAU,GAAG,QAAQ;GACzC,SAAS;GACV,CAAC;AAEJ,UACE,KACA,uBACA,iBACW;GACX,MAAM,mBACJ,OAAO,0BAA0B,WAC7B;IAAE,cAAc;IAAuB,GAAG;IAAc,GACvD,yBAAyB,EAAE;GAElC,MAAM,OAAO,MAAM,QAAQ,IAAI,GAAG,MAAM,CAAC,IAAI;AAE7C,QAAK,MAAM,gBAAgB,MAAM;IAC/B,MAAM,WAAW,iBAAiB,cAAc,iBAAiB;AACjE,QAAI,aAAa,OAAW,QAAO;;GAGrC,MAAM,eAAe,iBAAiB;AACtC,OAAI,OAAO,iBAAiB,SAC1B,yDACE,4DACuB,iBAAiB,EACxC,QACA,UACD;AAGH,UAAO,KAAK,KAAK,SAAS;;IAE3B;EAAC;EAAQ;EAAW;EAAU,CAAC;AA6ClC,QAAO;EAAE;EAAG,gCA1CH;GACL,UAAU;GACV,WAAY,oBAAoB,EAAE;GAClC,kBAAkB;GAClB,eAAe;GACf,gBAAgB,OAAO,gBAAwB;AAC7C,cAAU,YAA6B;;GAEzC,MAAM,aAAqC;AAEzC,2DADkC,YAAY,OAC9B,KAAK,QAAQ,QAAQ;;GAEvC,SAAS,KAAa,4DACD;IACT;IACR;IACA;IACA,SAAS;IACV,CAAC,KAAK;GACT,GAAG;GACH,YACG,UAA0B,oBAC1B,KAAa,iBAAoC;IAChD,MAAM,qDAA8B;KAClC,QAAS,YAAY;KACrB,WAAW,kBAAkB;KAC7B;KACA,SAAS;KACV,CAAC;AACF,WAAO,aAAa,SAAa,WAAsB;;GAE5D,GACD;GAAC;GAAQ;GAAkB;GAAW;GAAW;GAAU,CAU7C;EAAE,OAAO;EAAM"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
5
|
+
let react = require("react");
|
|
6
|
+
let react_intlayer = require("react-intlayer");
|
|
7
|
+
|
|
8
|
+
//#region src/useTranslationDictionary.ts
|
|
9
|
+
/**
|
|
10
|
+
* Dictionary-accepting variant of `useTranslation`.
|
|
11
|
+
*
|
|
12
|
+
* Used internally by the SWC optimization: instead of looking up the dictionary
|
|
13
|
+
* at runtime by key, the SWC plugin pre-imports the dictionary JSON at build time
|
|
14
|
+
* and passes it directly here. This enables tree-shaking of unused locale content.
|
|
15
|
+
*
|
|
16
|
+
* @example (generated by SWC, not written manually)
|
|
17
|
+
* import _abc from '.intlayer/dictionaries/about.json' with { type: 'json' };
|
|
18
|
+
* const { t } = useTranslationDictionary(_abc);
|
|
19
|
+
*/
|
|
20
|
+
const useTranslationDictionary = (dictionary, keyPrefix, options) => {
|
|
21
|
+
const content = (0, react_intlayer.useDictionary)(dictionary);
|
|
22
|
+
const { locale, setLocale, availableLocales } = (0, react_intlayer.useLocale)();
|
|
23
|
+
const i18n = (0, react.useMemo)(() => ({
|
|
24
|
+
language: locale,
|
|
25
|
+
languages: availableLocales ?? [],
|
|
26
|
+
changeLanguage: async (lng) => {
|
|
27
|
+
setLocale(lng);
|
|
28
|
+
},
|
|
29
|
+
isInitialized: true
|
|
30
|
+
}), [
|
|
31
|
+
locale,
|
|
32
|
+
availableLocales,
|
|
33
|
+
setLocale
|
|
34
|
+
]);
|
|
35
|
+
const prefix = options?.keyPrefix ?? keyPrefix;
|
|
36
|
+
const t = (key, params) => {
|
|
37
|
+
const lookupKey = prefix ? `${prefix}.${String(key)}` : String(key);
|
|
38
|
+
const parts = lookupKey.split(".");
|
|
39
|
+
let current = content;
|
|
40
|
+
for (const part of parts) {
|
|
41
|
+
if (current == null) break;
|
|
42
|
+
current = current[part];
|
|
43
|
+
}
|
|
44
|
+
const str = String(current ?? lookupKey);
|
|
45
|
+
if (!params) return str;
|
|
46
|
+
return str.replace(/\{\{(\w+)\}\}/g, (_, k) => params[k] != null ? String(params[k]) : `{{${k}}}`);
|
|
47
|
+
};
|
|
48
|
+
return {
|
|
49
|
+
t,
|
|
50
|
+
i18n,
|
|
51
|
+
ready: true
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
//#endregion
|
|
56
|
+
exports.useTranslationDictionary = useTranslationDictionary;
|
|
57
|
+
//# sourceMappingURL=useTranslationDictionary.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTranslationDictionary.cjs","names":[],"sources":["../../src/useTranslationDictionary.ts"],"sourcesContent":["'use client';\n\nimport type { ValidDotPathsFor } from '@intlayer/core/transpiler';\nimport type { Dictionary } from '@intlayer/types/dictionary';\nimport type { UseTranslationOptions } from 'i18next';\nimport { useMemo } from 'react';\nimport { useDictionary, useLocale } from 'react-intlayer';\n\n/**\n * Dictionary-accepting variant of `useTranslation`.\n *\n * Used internally by the SWC optimization: instead of looking up the dictionary\n * at runtime by key, the SWC plugin pre-imports the dictionary JSON at build time\n * and passes it directly here. This enables tree-shaking of unused locale content.\n *\n * @example (generated by SWC, not written manually)\n * import _abc from '.intlayer/dictionaries/about.json' with { type: 'json' };\n * const { t } = useTranslationDictionary(_abc);\n */\nexport const useTranslationDictionary = <T extends Dictionary>(\n dictionary: T,\n keyPrefix?: string,\n options?: UseTranslationOptions<string>\n) => {\n // reuse useDictionary which handles locale + getDictionary\n const content = useDictionary(dictionary);\n const { locale, setLocale, availableLocales } = useLocale();\n\n const i18n = useMemo(\n () => ({\n language: locale as string,\n languages: (availableLocales ?? []) as string[],\n changeLanguage: async (lng: string) => {\n setLocale(lng as any);\n },\n isInitialized: true,\n }),\n [locale, availableLocales, setLocale]\n );\n\n const prefix = options?.keyPrefix ?? keyPrefix;\n\n const t = <P extends ValidDotPathsFor<any>>(\n key: P,\n params?: Record<string, unknown>\n ): string => {\n const lookupKey = prefix ? `${prefix}.${String(key)}` : String(key);\n const parts = lookupKey.split('.');\n let current: any = content;\n for (const part of parts) {\n if (current == null) break;\n current = current[part];\n }\n const str = String(current ?? lookupKey);\n if (!params) return str;\n return str.replace(/\\{\\{(\\w+)\\}\\}/g, (_, k) =>\n params[k] != null ? String(params[k]) : `{{${k}}}`\n );\n };\n\n return { t, i18n, ready: true };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAmBA,MAAa,4BACX,YACA,WACA,YACG;CAEH,MAAM,4CAAwB,WAAW;CACzC,MAAM,EAAE,QAAQ,WAAW,oDAAgC;CAE3D,MAAM,iCACG;EACL,UAAU;EACV,WAAY,oBAAoB,EAAE;EAClC,gBAAgB,OAAO,QAAgB;AACrC,aAAU,IAAW;;EAEvB,eAAe;EAChB,GACD;EAAC;EAAQ;EAAkB;EAAU,CACtC;CAED,MAAM,SAAS,SAAS,aAAa;CAErC,MAAM,KACJ,KACA,WACW;EACX,MAAM,YAAY,SAAS,GAAG,OAAO,GAAG,OAAO,IAAI,KAAK,OAAO,IAAI;EACnE,MAAM,QAAQ,UAAU,MAAM,IAAI;EAClC,IAAI,UAAe;AACnB,OAAK,MAAM,QAAQ,OAAO;AACxB,OAAI,WAAW,KAAM;AACrB,aAAU,QAAQ;;EAEpB,MAAM,MAAM,OAAO,WAAW,UAAU;AACxC,MAAI,CAAC,OAAQ,QAAO;AACpB,SAAO,IAAI,QAAQ,mBAAmB,GAAG,MACvC,OAAO,MAAM,OAAO,OAAO,OAAO,GAAG,GAAG,KAAK,EAAE,IAChD;;AAGH,QAAO;EAAE;EAAG;EAAM,OAAO;EAAM"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
5
|
+
let react = require("react");
|
|
6
|
+
let react_intlayer = require("react-intlayer");
|
|
7
|
+
|
|
8
|
+
//#region src/useTranslationDictionaryDynamic.ts
|
|
9
|
+
/**
|
|
10
|
+
* Dynamic dictionary-accepting variant of `useTranslation`.
|
|
11
|
+
*/
|
|
12
|
+
const useTranslationDictionaryDynamic = (dictionaryPromise, key, keyPrefix, options) => {
|
|
13
|
+
const content = (0, react_intlayer.useDictionaryDynamic)(dictionaryPromise, key);
|
|
14
|
+
const { locale, setLocale, availableLocales } = (0, react_intlayer.useLocale)();
|
|
15
|
+
const i18n = (0, react.useMemo)(() => ({
|
|
16
|
+
language: locale,
|
|
17
|
+
languages: availableLocales ?? [],
|
|
18
|
+
changeLanguage: async (lng) => {
|
|
19
|
+
setLocale(lng);
|
|
20
|
+
},
|
|
21
|
+
isInitialized: true
|
|
22
|
+
}), [
|
|
23
|
+
locale,
|
|
24
|
+
availableLocales,
|
|
25
|
+
setLocale
|
|
26
|
+
]);
|
|
27
|
+
const prefix = options?.keyPrefix ?? keyPrefix;
|
|
28
|
+
const t = (lookup, params) => {
|
|
29
|
+
const lookupKey = prefix ? `${prefix}.${String(lookup)}` : String(lookup);
|
|
30
|
+
const parts = lookupKey.split(".");
|
|
31
|
+
let current = content;
|
|
32
|
+
for (const part of parts) {
|
|
33
|
+
if (current == null) break;
|
|
34
|
+
current = current[part];
|
|
35
|
+
}
|
|
36
|
+
const str = String(current ?? lookupKey);
|
|
37
|
+
if (!params) return str;
|
|
38
|
+
return str.replace(/\{\{(\w+)\}\}/g, (_, k) => params[k] != null ? String(params[k]) : `{{${k}}}`);
|
|
39
|
+
};
|
|
40
|
+
return {
|
|
41
|
+
t,
|
|
42
|
+
i18n,
|
|
43
|
+
ready: true
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
//#endregion
|
|
48
|
+
exports.useTranslationDictionaryDynamic = useTranslationDictionaryDynamic;
|
|
49
|
+
//# sourceMappingURL=useTranslationDictionaryDynamic.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTranslationDictionaryDynamic.cjs","names":[],"sources":["../../src/useTranslationDictionaryDynamic.ts"],"sourcesContent":["'use client';\n\nimport type { ValidDotPathsFor } from '@intlayer/core/transpiler';\nimport type { Dictionary } from '@intlayer/types/dictionary';\nimport type { StrictModeLocaleMap } from '@intlayer/types/module_augmentation';\nimport type { UseTranslationOptions } from 'i18next';\nimport { useMemo } from 'react';\nimport { useDictionaryDynamic, useLocale } from 'react-intlayer';\n\n/**\n * Dynamic dictionary-accepting variant of `useTranslation`.\n */\nexport const useTranslationDictionaryDynamic = <\n const T extends Dictionary,\n const K extends string,\n>(\n dictionaryPromise: StrictModeLocaleMap<() => Promise<T>>,\n key: K,\n keyPrefix?: string,\n options?: UseTranslationOptions<string>\n) => {\n const content = useDictionaryDynamic<T, any>(dictionaryPromise, key as any);\n const { locale, setLocale, availableLocales } = useLocale();\n\n const i18n = useMemo(\n () => ({\n language: locale as string,\n languages: (availableLocales ?? []) as string[],\n changeLanguage: async (lng: string) => {\n setLocale(lng as any);\n },\n isInitialized: true,\n }),\n [locale, availableLocales, setLocale]\n );\n\n const prefix = options?.keyPrefix ?? keyPrefix;\n\n const t = <P extends ValidDotPathsFor<any>>(\n lookup: P,\n params?: Record<string, unknown>\n ): string => {\n const lookupKey = prefix ? `${prefix}.${String(lookup)}` : String(lookup);\n const parts = lookupKey.split('.');\n let current: any = content;\n for (const part of parts) {\n if (current == null) break;\n current = current[part];\n }\n const str = String(current ?? lookupKey);\n if (!params) return str;\n return str.replace(/\\{\\{(\\w+)\\}\\}/g, (_, k) =>\n params[k] != null ? String(params[k]) : `{{${k}}}`\n );\n };\n\n return { t, i18n, ready: true };\n};\n"],"mappings":";;;;;;;;;;;AAYA,MAAa,mCAIX,mBACA,KACA,WACA,YACG;CACH,MAAM,mDAAuC,mBAAmB,IAAW;CAC3E,MAAM,EAAE,QAAQ,WAAW,oDAAgC;CAE3D,MAAM,iCACG;EACL,UAAU;EACV,WAAY,oBAAoB,EAAE;EAClC,gBAAgB,OAAO,QAAgB;AACrC,aAAU,IAAW;;EAEvB,eAAe;EAChB,GACD;EAAC;EAAQ;EAAkB;EAAU,CACtC;CAED,MAAM,SAAS,SAAS,aAAa;CAErC,MAAM,KACJ,QACA,WACW;EACX,MAAM,YAAY,SAAS,GAAG,OAAO,GAAG,OAAO,OAAO,KAAK,OAAO,OAAO;EACzE,MAAM,QAAQ,UAAU,MAAM,IAAI;EAClC,IAAI,UAAe;AACnB,OAAK,MAAM,QAAQ,OAAO;AACxB,OAAI,WAAW,KAAM;AACrB,aAAU,QAAQ;;EAEpB,MAAM,MAAM,OAAO,WAAW,UAAU;AACxC,MAAI,CAAC,OAAQ,QAAO;AACpB,SAAO,IAAI,QAAQ,mBAAmB,GAAG,MACvC,OAAO,MAAM,OAAO,OAAO,OAAO,GAAG,GAAG,KAAK,EAAE,IAChD;;AAGH,QAAO;EAAE;EAAG;EAAM,OAAO;EAAM"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
5
|
+
const require_useTranslation = require('./useTranslation.cjs');
|
|
6
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
7
|
+
|
|
8
|
+
//#region src/withTranslation.tsx
|
|
9
|
+
/**
|
|
10
|
+
* Drop-in for react-i18next's `withTranslation` HOC.
|
|
11
|
+
*/
|
|
12
|
+
const withTranslation = ((ns, options) => (WrappedComponent) => {
|
|
13
|
+
const WithTranslation = (props) => {
|
|
14
|
+
const { t, i18n } = require_useTranslation.useTranslation(ns, options);
|
|
15
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(WrappedComponent, {
|
|
16
|
+
...props,
|
|
17
|
+
t,
|
|
18
|
+
i18n,
|
|
19
|
+
tReady: true
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
WithTranslation.displayName = `withTranslation(${WrappedComponent.displayName ?? WrappedComponent.name ?? "Component"})`;
|
|
23
|
+
return WithTranslation;
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
exports.withTranslation = withTranslation;
|
|
28
|
+
//# sourceMappingURL=withTranslation.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"withTranslation.cjs","names":["useTranslation"],"sources":["../../src/withTranslation.tsx"],"sourcesContent":["'use client';\n\nimport type * as React from 'react';\nimport type {\n withTranslation as _withTranslation,\n UseTranslationOptions,\n} from 'react-i18next';\nimport { useTranslation } from './useTranslation';\n\n/**\n * Drop-in for react-i18next's `withTranslation` HOC.\n */\nexport const withTranslation: typeof _withTranslation = ((\n ns?: string | string[],\n options?: UseTranslationOptions<string>\n) =>\n <P extends { t: unknown; i18n: unknown; tReady: boolean }>(\n WrappedComponent: React.ComponentType<P>\n ) => {\n const WithTranslation = (props: Omit<P, 't' | 'i18n' | 'tReady'>) => {\n const { t, i18n } = useTranslation(ns, options);\n return (\n <WrappedComponent\n {...(props as unknown as P)}\n t={t}\n i18n={i18n}\n tReady\n />\n );\n };\n WithTranslation.displayName = `withTranslation(${\n WrappedComponent.displayName ?? WrappedComponent.name ?? 'Component'\n })`;\n return WithTranslation as unknown as React.ComponentType<\n Omit<P, 't' | 'i18n' | 'tReady'>\n >;\n }) as typeof _withTranslation;\n"],"mappings":";;;;;;;;;;;AAYA,MAAa,oBACX,IACA,aAGE,qBACG;CACH,MAAM,mBAAmB,UAA4C;EACnE,MAAM,EAAE,GAAG,SAASA,sCAAe,IAAI,QAAQ;AAC/C,SACE,2CAAC,kBAAD;GACE,GAAK;GACF;GACG;GACN;GACA;;AAGN,iBAAgB,cAAc,mBAC5B,iBAAiB,eAAe,iBAAiB,QAAQ,YAC1D;AACD,QAAO"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { IntlayerProvider } from "react-intlayer";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
import { log } from "@intlayer/config/built";
|
|
6
|
+
import { CYAN } from "@intlayer/config/colors";
|
|
7
|
+
import { colorize, getAppLogger } from "@intlayer/config/logger";
|
|
8
|
+
|
|
9
|
+
//#region src/I18nextProvider.tsx
|
|
10
|
+
/**
|
|
11
|
+
* Drop-in for react-i18next's `I18nextProvider`.
|
|
12
|
+
* The `i18n` prop is accepted for API compatibility but has no effect —
|
|
13
|
+
* Intlayer uses its own instance. A development warning is emitted when
|
|
14
|
+
* the `i18n` prop is provided so consumers know to remove it.
|
|
15
|
+
*/
|
|
16
|
+
const I18nextProvider = ({ children, i18n: _i18n }) => {
|
|
17
|
+
if (_i18n !== void 0) getAppLogger({ log })(`${colorize("I18nextProvider", CYAN)}: the \`i18n\` prop has no effect with intlayer. Intlayer manages its own i18n instance — you can safely remove the prop.`);
|
|
18
|
+
return /* @__PURE__ */ jsx(IntlayerProvider, { children });
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
//#endregion
|
|
22
|
+
export { I18nextProvider };
|
|
23
|
+
//# sourceMappingURL=I18nextProvider.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"I18nextProvider.mjs","names":[],"sources":["../../src/I18nextProvider.tsx"],"sourcesContent":["'use client';\n\nimport { log } from '@intlayer/config/built';\nimport { CYAN } from '@intlayer/config/colors';\nimport { colorize, getAppLogger } from '@intlayer/config/logger';\nimport type {\n I18nextProvider as _I18nextProvider,\n I18nextProviderProps,\n} from 'react-i18next';\nimport { IntlayerProvider } from 'react-intlayer';\n\n/**\n * Drop-in for react-i18next's `I18nextProvider`.\n * The `i18n` prop is accepted for API compatibility but has no effect —\n * Intlayer uses its own instance. A development warning is emitted when\n * the `i18n` prop is provided so consumers know to remove it.\n */\nexport const I18nextProvider: typeof _I18nextProvider = ({\n children,\n i18n: _i18n,\n}: I18nextProviderProps) => {\n if (process.env.NODE_ENV === 'development' && _i18n !== undefined) {\n const appLogger = getAppLogger({ log });\n appLogger(\n `${colorize('I18nextProvider', CYAN)}: the \\`i18n\\` prop has no effect with intlayer. Intlayer manages its own i18n instance — you can safely remove the prop.`\n );\n }\n\n return <IntlayerProvider>{children}</IntlayerProvider>;\n};\n"],"mappings":";;;;;;;;;;;;;;;AAiBA,MAAa,mBAA4C,EACvD,UACA,MAAM,YACoB;AAC1B,KAA8C,UAAU,OAEtD,CADkB,aAAa,EAAE,KAAK,CAC7B,CACP,GAAG,SAAS,mBAAmB,KAAK,CAAC,2HACtC;AAGH,QAAO,oBAAC,kBAAD,EAAmB,UAA4B"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useTranslation } from "./useTranslation.mjs";
|
|
4
|
+
import { parseTaggedMessage } from "@intlayer/core/messageFormat";
|
|
5
|
+
import { Children, Fragment, cloneElement, isValidElement } from "react";
|
|
6
|
+
import { Fragment as Fragment$1, jsx } from "react/jsx-runtime";
|
|
7
|
+
|
|
8
|
+
//#region src/Trans.tsx
|
|
9
|
+
/**
|
|
10
|
+
* Renders parsed message tokens to React nodes, mapping tags to the
|
|
11
|
+
* `components` prop (object or array form) or to the JSX children
|
|
12
|
+
* (numbered tags `<1>…</1>` index into the children, react-i18next style).
|
|
13
|
+
*/
|
|
14
|
+
const renderTokens = (tokens, components, childrenArray) => tokens.map((token, tokenIndex) => {
|
|
15
|
+
if (typeof token === "string") return token;
|
|
16
|
+
const renderedChildren = renderTokens(token.children, components, childrenArray);
|
|
17
|
+
const mappedComponent = Array.isArray(components) ? components[Number(token.tag)] : components[token.tag];
|
|
18
|
+
const fallbackChild = /^\d+$/.test(token.tag) ? childrenArray[Number(token.tag)] : void 0;
|
|
19
|
+
const elementToClone = mappedComponent ?? fallbackChild;
|
|
20
|
+
if (isValidElement(elementToClone)) return cloneElement(elementToClone, { key: tokenIndex }, ...renderedChildren.length ? renderedChildren : [elementToClone.props.children]);
|
|
21
|
+
if (/^[a-z][\w-]*$/.test(token.tag)) {
|
|
22
|
+
const NativeTag = token.tag;
|
|
23
|
+
return /* @__PURE__ */ jsx(NativeTag, { children: renderedChildren }, tokenIndex);
|
|
24
|
+
}
|
|
25
|
+
return /* @__PURE__ */ jsx(Fragment, { children: renderedChildren }, tokenIndex);
|
|
26
|
+
});
|
|
27
|
+
/**
|
|
28
|
+
* Drop-in for react-i18next's `<Trans>`.
|
|
29
|
+
*
|
|
30
|
+
* Supports `values` interpolation, `count` plural resolution, `context`,
|
|
31
|
+
* `defaults` fallback, and component interpolation through the `components`
|
|
32
|
+
* prop (object or array form) or numbered tags mapped onto JSX children.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```tsx
|
|
36
|
+
* <Trans i18nKey="richText" components={{ bold: <strong /> }} />
|
|
37
|
+
* <Trans i18nKey="hello" count={2} values={{ name: 'John' }}>
|
|
38
|
+
* Hello <b>{'{{name}}'}</b>
|
|
39
|
+
* </Trans>
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
const Trans = function Trans({ i18nKey, ns, values, count, context, defaults, components, tOptions, children }) {
|
|
43
|
+
const { t } = useTranslation(Array.isArray(ns) ? ns[0] : ns);
|
|
44
|
+
const translateOptions = {
|
|
45
|
+
...tOptions ?? {},
|
|
46
|
+
...values ?? {}
|
|
47
|
+
};
|
|
48
|
+
if (count !== void 0) translateOptions.count = count;
|
|
49
|
+
if (context !== void 0) translateOptions.context = context;
|
|
50
|
+
if (typeof defaults === "string") translateOptions.defaultValue = defaults;
|
|
51
|
+
const message = i18nKey ? t(i18nKey, translateOptions) : defaults ?? "";
|
|
52
|
+
const resolvedMessage = typeof message === "string" && message !== i18nKey ? message : defaults ?? message;
|
|
53
|
+
if (typeof resolvedMessage !== "string") return /* @__PURE__ */ jsx(Fragment$1, { children: children ?? resolvedMessage });
|
|
54
|
+
const childrenArray = Children.toArray(children);
|
|
55
|
+
const tokens = parseTaggedMessage(resolvedMessage);
|
|
56
|
+
if (!tokens.some((token) => typeof token !== "string") && !components) return /* @__PURE__ */ jsx(Fragment$1, { children: resolvedMessage });
|
|
57
|
+
return /* @__PURE__ */ jsx(Fragment$1, { children: renderTokens(tokens, components ?? {}, childrenArray) });
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
//#endregion
|
|
61
|
+
export { Trans };
|
|
62
|
+
//# sourceMappingURL=Trans.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Trans.mjs","names":["translateFunction"],"sources":["../../src/Trans.tsx"],"sourcesContent":["'use client';\n\nimport {\n parseTaggedMessage,\n type TaggedMessageToken,\n} from '@intlayer/core/messageFormat';\nimport type { DictionaryKeys } from '@intlayer/types/module_augmentation';\nimport type { Namespace, TOptions } from 'i18next';\nimport {\n Children,\n cloneElement,\n Fragment,\n isValidElement,\n type ReactElement,\n type ReactNode,\n} from 'react';\nimport type { Trans as _Trans, TransProps } from 'react-i18next';\nimport { useTranslation } from './useTranslation';\n\n/**\n * Renders parsed message tokens to React nodes, mapping tags to the\n * `components` prop (object or array form) or to the JSX children\n * (numbered tags `<1>…</1>` index into the children, react-i18next style).\n */\nconst renderTokens = (\n tokens: TaggedMessageToken[],\n components: Record<string, ReactElement> | readonly ReactElement[],\n childrenArray: ReactNode[]\n): ReactNode[] =>\n tokens.map((token, tokenIndex) => {\n if (typeof token === 'string') return token;\n\n const renderedChildren = renderTokens(\n token.children,\n components,\n childrenArray\n );\n\n const mappedComponent = Array.isArray(components)\n ? components[Number(token.tag)]\n : (components as Record<string, ReactElement>)[token.tag];\n\n const fallbackChild = /^\\d+$/.test(token.tag)\n ? childrenArray[Number(token.tag)]\n : undefined;\n\n const elementToClone = mappedComponent ?? fallbackChild;\n\n if (isValidElement(elementToClone)) {\n return cloneElement(\n elementToClone as ReactElement<{ children?: ReactNode }>,\n { key: tokenIndex },\n ...(renderedChildren.length\n ? renderedChildren\n : [\n (elementToClone as ReactElement<{ children?: ReactNode }>).props\n .children,\n ])\n );\n }\n\n // Native HTML tag in the message without a mapping — render it as-is\n if (/^[a-z][\\w-]*$/.test(token.tag)) {\n const NativeTag = token.tag as keyof React.JSX.IntrinsicElements;\n return <NativeTag key={tokenIndex}>{renderedChildren}</NativeTag>;\n }\n\n // Unknown numbered tag — render its children unwrapped\n return <Fragment key={tokenIndex}>{renderedChildren}</Fragment>;\n });\n\n/**\n * Drop-in for react-i18next's `<Trans>`.\n *\n * Supports `values` interpolation, `count` plural resolution, `context`,\n * `defaults` fallback, and component interpolation through the `components`\n * prop (object or array form) or numbered tags mapped onto JSX children.\n *\n * @example\n * ```tsx\n * <Trans i18nKey=\"richText\" components={{ bold: <strong /> }} />\n * <Trans i18nKey=\"hello\" count={2} values={{ name: 'John' }}>\n * Hello <b>{'{{name}}'}</b>\n * </Trans>\n * ```\n */\nexport const Trans: typeof _Trans = function Trans<\n Key extends string = string,\n Ns extends Namespace = Namespace,\n>({\n i18nKey,\n ns,\n values,\n count,\n context,\n defaults,\n components,\n tOptions,\n children,\n}: TransProps<Key, Ns>): React.ReactElement {\n const { t } = useTranslation(\n (Array.isArray(ns) ? ns[0] : ns) as DictionaryKeys\n );\n\n const translateOptions: TOptions = {\n ...((tOptions ?? {}) as TOptions),\n ...((values ?? {}) as Record<string, unknown>),\n };\n if (count !== undefined) translateOptions.count = count;\n if (context !== undefined) translateOptions.context = context;\n if (typeof defaults === 'string') translateOptions.defaultValue = defaults;\n\n const translateFunction = t as unknown as (\n key: string,\n options?: TOptions\n ) => string;\n\n const message = i18nKey\n ? translateFunction(i18nKey as string, translateOptions)\n : (defaults ?? '');\n\n const resolvedMessage =\n typeof message === 'string' && message !== i18nKey\n ? message\n : (defaults ?? message);\n\n if (typeof resolvedMessage !== 'string') {\n return <>{children ?? resolvedMessage}</>;\n }\n\n const childrenArray = Children.toArray(children);\n\n const tokens = parseTaggedMessage(resolvedMessage);\n const hasTags = tokens.some((token) => typeof token !== 'string');\n\n if (!hasTags && !components) {\n return <>{resolvedMessage}</>;\n }\n\n return (\n <>\n {renderTokens(\n tokens,\n (components ?? {}) as Record<string, ReactElement>,\n childrenArray\n )}\n </>\n );\n} as typeof _Trans;\n"],"mappings":";;;;;;;;;;;;;AAwBA,MAAM,gBACJ,QACA,YACA,kBAEA,OAAO,KAAK,OAAO,eAAe;AAChC,KAAI,OAAO,UAAU,SAAU,QAAO;CAEtC,MAAM,mBAAmB,aACvB,MAAM,UACN,YACA,cACD;CAED,MAAM,kBAAkB,MAAM,QAAQ,WAAW,GAC7C,WAAW,OAAO,MAAM,IAAI,IAC3B,WAA4C,MAAM;CAEvD,MAAM,gBAAgB,QAAQ,KAAK,MAAM,IAAI,GACzC,cAAc,OAAO,MAAM,IAAI,IAC/B;CAEJ,MAAM,iBAAiB,mBAAmB;AAE1C,KAAI,eAAe,eAAe,CAChC,QAAO,aACL,gBACA,EAAE,KAAK,YAAY,EACnB,GAAI,iBAAiB,SACjB,mBACA,CACG,eAA0D,MACxD,SACJ,CACN;AAIH,KAAI,gBAAgB,KAAK,MAAM,IAAI,EAAE;EACnC,MAAM,YAAY,MAAM;AACxB,SAAO,oBAAC,WAAD,YAA6B,kBAA6B,EAA1C,WAA0C;;AAInE,QAAO,oBAAC,UAAD,YAA4B,kBAA4B,EAAzC,WAAyC;EAC/D;;;;;;;;;;;;;;;;AAiBJ,MAAa,QAAuB,SAAS,MAG3C,EACA,SACA,IACA,QACA,OACA,SACA,UACA,YACA,UACA,YAC0C;CAC1C,MAAM,EAAE,MAAM,eACX,MAAM,QAAQ,GAAG,GAAG,GAAG,KAAK,GAC9B;CAED,MAAM,mBAA6B;EACjC,GAAK,YAAY,EAAE;EACnB,GAAK,UAAU,EAAE;EAClB;AACD,KAAI,UAAU,OAAW,kBAAiB,QAAQ;AAClD,KAAI,YAAY,OAAW,kBAAiB,UAAU;AACtD,KAAI,OAAO,aAAa,SAAU,kBAAiB,eAAe;CAOlE,MAAM,UAAU,UACZA,EAAkB,SAAmB,iBAAiB,GACrD,YAAY;CAEjB,MAAM,kBACJ,OAAO,YAAY,YAAY,YAAY,UACvC,UACC,YAAY;AAEnB,KAAI,OAAO,oBAAoB,SAC7B,QAAO,4CAAG,YAAY,iBAAmB;CAG3C,MAAM,gBAAgB,SAAS,QAAQ,SAAS;CAEhD,MAAM,SAAS,mBAAmB,gBAAgB;AAGlD,KAAI,CAFY,OAAO,MAAM,UAAU,OAAO,UAAU,SAE5C,IAAI,CAAC,WACf,QAAO,4CAAG,iBAAmB;AAG/B,QACE,4CACG,aACC,QACC,cAAc,EAAE,EACjB,cACD,EACA"}
|