@generaltranslation/react-core 11.4.15 → 11.4.17
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/CHANGELOG.md +14 -0
- package/dist/hooks/external-store/useSubscribeToTrackedLookups.cjs +1 -1
- package/dist/hooks/external-store/useSubscribeToTrackedLookups.cjs.map +1 -1
- package/dist/hooks/external-store/useSubscribeToTrackedLookups.mjs +1 -1
- package/dist/hooks/external-store/useSubscribeToTrackedLookups.mjs.map +1 -1
- package/dist/hooks/external-store/useTrackedDictionaryObjResolver.cjs +1 -1
- package/dist/hooks/external-store/useTrackedDictionaryObjResolver.cjs.map +1 -1
- package/dist/hooks/external-store/useTrackedDictionaryObjResolver.mjs +1 -1
- package/dist/hooks/external-store/useTrackedDictionaryObjResolver.mjs.map +1 -1
- package/dist/hooks/external-store/useTrackedDictionaryResolver.cjs +1 -1
- package/dist/hooks/external-store/useTrackedDictionaryResolver.cjs.map +1 -1
- package/dist/hooks/external-store/useTrackedDictionaryResolver.mjs +1 -1
- package/dist/hooks/external-store/useTrackedDictionaryResolver.mjs.map +1 -1
- package/dist/hooks/external-store/useTrackedTranslationResolver.cjs +1 -1
- package/dist/hooks/external-store/useTrackedTranslationResolver.cjs.map +1 -1
- package/dist/hooks/external-store/useTrackedTranslationResolver.mjs +1 -1
- package/dist/hooks/external-store/useTrackedTranslationResolver.mjs.map +1 -1
- package/dist/hooks/useTranslations.cjs +1 -1
- package/dist/hooks/useTranslations.cjs.map +1 -1
- package/dist/hooks/useTranslations.d.ts.map +1 -1
- package/dist/hooks/useTranslations.mjs +1 -1
- package/dist/hooks/useTranslations.mjs.map +1 -1
- package/dist/hooks/utils/missing-translation.cjs +1 -1
- package/dist/hooks/utils/missing-translation.cjs.map +1 -1
- package/dist/hooks/utils/missing-translation.mjs +1 -1
- package/dist/hooks/utils/missing-translation.mjs.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @generaltranslation/react-core
|
|
2
2
|
|
|
3
|
+
## 11.4.17
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#2554](https://github.com/generaltranslation/gt/pull/2554) [`a9c1e61`](https://github.com/generaltranslation/gt/commit/a9c1e61c523951637ef0e480e0f6200fd2426643) Thanks [@dyc3](https://github.com/dyc3)! - Keep the `gt` function from `useGT()` (and the dictionary hooks built on the same dev hot reload queue) stable across renders in development, so effects that list it as a dependency no longer re-run after every render. Its identity still changes when a hot-reloaded translation arrives, so memoized and effect-derived strings pick up the new translation.
|
|
8
|
+
|
|
9
|
+
## 11.4.16
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- Updated dependencies []:
|
|
14
|
+
- generaltranslation@9.5.8
|
|
15
|
+
- gt-i18n@1.0.39
|
|
16
|
+
|
|
3
17
|
## 11.4.15
|
|
4
18
|
|
|
5
19
|
## 11.4.14
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
require(`../../_virtual/_rolldown/runtime.cjs`);let e=require(`react`);function t(t,n,r){let i=(0,e.useRef)(0),a=(0,e.useCallback)(e=>n(n=>{t.current.has(r(n))&&(i.current++,e())}),[n,r,t]),o=(0,e.useCallback)(()=>i.current,[]);(0,e.useSyncExternalStore)(a,o,o)}const n=process.env.NODE_ENV===`production`?()=>
|
|
1
|
+
require(`../../_virtual/_rolldown/runtime.cjs`);let e=require(`react`);function t(t,n,r){let i=(0,e.useRef)(0),a=(0,e.useCallback)(e=>n(n=>{t.current.has(r(n))&&(i.current++,e())}),[n,r,t]),o=(0,e.useCallback)(()=>i.current,[]);return(0,e.useSyncExternalStore)(a,o,o)}const n=process.env.NODE_ENV===`production`?()=>0:t;exports.useSubscribeToTrackedLookups=n;
|
|
2
2
|
//# sourceMappingURL=useSubscribeToTrackedLookups.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSubscribeToTrackedLookups.cjs","names":[],"sources":["../../../src/hooks/external-store/useSubscribeToTrackedLookups.ts"],"sourcesContent":["import {\n type RefObject,\n useCallback,\n useRef,\n useSyncExternalStore,\n} from 'react';\nimport type { StoreListener, Unsubscribe } from '../../i18n-store/storeTypes';\n\ntype UseSubscribeToTrackedLookups = <L>(\n trackedKeysRef: RefObject<Set<string> | null>,\n subscribeToEvents: (onEvent: (lookup: L) => void) => Unsubscribe,\n getListenerKey: (lookup: L) => string\n) =>
|
|
1
|
+
{"version":3,"file":"useSubscribeToTrackedLookups.cjs","names":[],"sources":["../../../src/hooks/external-store/useSubscribeToTrackedLookups.ts"],"sourcesContent":["import {\n type RefObject,\n useCallback,\n useRef,\n useSyncExternalStore,\n} from 'react';\nimport type { StoreListener, Unsubscribe } from '../../i18n-store/storeTypes';\n\ntype UseSubscribeToTrackedLookups = <L>(\n trackedKeysRef: RefObject<Set<string> | null>,\n subscribeToEvents: (onEvent: (lookup: L) => void) => Unsubscribe,\n getListenerKey: (lookup: L) => string\n) => number;\n\n/**\n * Subscribe to store events, but only trigger re-renders when the event's\n * lookup is in the tracked keys set. Shared by the tracked translation and\n * dictionary resolvers, which differ only in the store subscribe method and the\n * listener-key function.\n *\n * Returns a version that changes whenever a tracked lookup updates. Resolvers\n * include it in their memoization dependencies so the translation functions\n * they return change identity when, and only when, a tracked translation\n * arrives.\n *\n * Remember that we can make no assumptions about when the tracked set gets\n * updated. This is technically not pure, but it is an acceptable trade since it\n * only drives dev translation hot reload.\n */\nfunction useSubscribeToTrackedLookupsDev<L>(\n trackedKeysRef: RefObject<Set<string> | null>,\n subscribeToEvents: (onEvent: (lookup: L) => void) => Unsubscribe,\n getListenerKey: (lookup: L) => string\n): number {\n // invalidation counter for triggering updates\n const versionRef = useRef(0);\n const subscribe = useCallback(\n (listener: StoreListener) => {\n return subscribeToEvents((lookup) => {\n if (!trackedKeysRef.current!.has(getListenerKey(lookup))) return;\n versionRef.current++;\n listener();\n });\n },\n [subscribeToEvents, getListenerKey, trackedKeysRef]\n );\n const getSnapshot = useCallback(() => versionRef.current, []);\n\n return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n}\n\nconst useSubscribeToTrackedLookupsProd: UseSubscribeToTrackedLookups = () => 0;\n\nexport const useSubscribeToTrackedLookups: UseSubscribeToTrackedLookups =\n process.env.NODE_ENV === 'production'\n ? useSubscribeToTrackedLookupsProd\n : useSubscribeToTrackedLookupsDev;\n"],"mappings":"uEA6BA,SAAS,EACP,EACA,EACA,EACQ,CAER,IAAM,GAAA,EAAA,EAAA,QAAoB,EAAE,CACtB,GAAA,EAAA,EAAA,aACH,GACQ,EAAmB,GAAW,CAC9B,EAAe,QAAS,IAAI,EAAe,EAAO,CAAC,GACxD,EAAW,UACX,GAAU,GACV,CAEJ,CAAC,EAAmB,EAAgB,EAAe,CACpD,CACK,GAAA,EAAA,EAAA,iBAAgC,EAAW,QAAS,EAAE,CAAC,CAE7D,OAAA,EAAA,EAAA,sBAA4B,EAAW,EAAa,EAAY,CAKlE,MAAa,EACX,QAAQ,IAAI,WAAa,iBAHkD,EAKvE"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{useCallback as e,useRef as t,useSyncExternalStore as n}from"react";function r(r,i,a){let o=t(0),s=e(e=>i(t=>{r.current.has(a(t))&&(o.current++,e())}),[i,a,r]),c=e(()=>o.current,[]);n(s,c,c)}const i=process.env.NODE_ENV===`production`?()=>
|
|
1
|
+
import{useCallback as e,useRef as t,useSyncExternalStore as n}from"react";function r(r,i,a){let o=t(0),s=e(e=>i(t=>{r.current.has(a(t))&&(o.current++,e())}),[i,a,r]),c=e(()=>o.current,[]);return n(s,c,c)}const i=process.env.NODE_ENV===`production`?()=>0:r;export{i as useSubscribeToTrackedLookups};
|
|
2
2
|
//# sourceMappingURL=useSubscribeToTrackedLookups.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSubscribeToTrackedLookups.mjs","names":[],"sources":["../../../src/hooks/external-store/useSubscribeToTrackedLookups.ts"],"sourcesContent":["import {\n type RefObject,\n useCallback,\n useRef,\n useSyncExternalStore,\n} from 'react';\nimport type { StoreListener, Unsubscribe } from '../../i18n-store/storeTypes';\n\ntype UseSubscribeToTrackedLookups = <L>(\n trackedKeysRef: RefObject<Set<string> | null>,\n subscribeToEvents: (onEvent: (lookup: L) => void) => Unsubscribe,\n getListenerKey: (lookup: L) => string\n) =>
|
|
1
|
+
{"version":3,"file":"useSubscribeToTrackedLookups.mjs","names":[],"sources":["../../../src/hooks/external-store/useSubscribeToTrackedLookups.ts"],"sourcesContent":["import {\n type RefObject,\n useCallback,\n useRef,\n useSyncExternalStore,\n} from 'react';\nimport type { StoreListener, Unsubscribe } from '../../i18n-store/storeTypes';\n\ntype UseSubscribeToTrackedLookups = <L>(\n trackedKeysRef: RefObject<Set<string> | null>,\n subscribeToEvents: (onEvent: (lookup: L) => void) => Unsubscribe,\n getListenerKey: (lookup: L) => string\n) => number;\n\n/**\n * Subscribe to store events, but only trigger re-renders when the event's\n * lookup is in the tracked keys set. Shared by the tracked translation and\n * dictionary resolvers, which differ only in the store subscribe method and the\n * listener-key function.\n *\n * Returns a version that changes whenever a tracked lookup updates. Resolvers\n * include it in their memoization dependencies so the translation functions\n * they return change identity when, and only when, a tracked translation\n * arrives.\n *\n * Remember that we can make no assumptions about when the tracked set gets\n * updated. This is technically not pure, but it is an acceptable trade since it\n * only drives dev translation hot reload.\n */\nfunction useSubscribeToTrackedLookupsDev<L>(\n trackedKeysRef: RefObject<Set<string> | null>,\n subscribeToEvents: (onEvent: (lookup: L) => void) => Unsubscribe,\n getListenerKey: (lookup: L) => string\n): number {\n // invalidation counter for triggering updates\n const versionRef = useRef(0);\n const subscribe = useCallback(\n (listener: StoreListener) => {\n return subscribeToEvents((lookup) => {\n if (!trackedKeysRef.current!.has(getListenerKey(lookup))) return;\n versionRef.current++;\n listener();\n });\n },\n [subscribeToEvents, getListenerKey, trackedKeysRef]\n );\n const getSnapshot = useCallback(() => versionRef.current, []);\n\n return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n}\n\nconst useSubscribeToTrackedLookupsProd: UseSubscribeToTrackedLookups = () => 0;\n\nexport const useSubscribeToTrackedLookups: UseSubscribeToTrackedLookups =\n process.env.NODE_ENV === 'production'\n ? useSubscribeToTrackedLookupsProd\n : useSubscribeToTrackedLookupsDev;\n"],"mappings":"0EA6BA,SAAS,EACP,EACA,EACA,EACQ,CAER,IAAM,EAAa,EAAO,EAAE,CACtB,EAAY,EACf,GACQ,EAAmB,GAAW,CAC9B,EAAe,QAAS,IAAI,EAAe,EAAO,CAAC,GACxD,EAAW,UACX,GAAU,GACV,CAEJ,CAAC,EAAmB,EAAgB,EAAe,CACpD,CACK,EAAc,MAAkB,EAAW,QAAS,EAAE,CAAC,CAE7D,OAAO,EAAqB,EAAW,EAAa,EAAY,CAKlE,MAAa,EACX,QAAQ,IAAI,WAAa,iBAHkD,EAKvE"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
require(`../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../i18n-store/useI18nStore.cjs`),t=require(`../utils/missing-translation.cjs`),n=require(`./useSubscribeToTrackedLookups.cjs`);let r=require(`gt-i18n/internal`),i=require(`react`);function a(){let a=e.useDictionariesSnapshot(),o=e.useI18nStore(),s=process.env.NODE_ENV!==`production`&&(0,r.getI18nConfig)().isDevHotReloadEnabled(),c=t.useHandleMissingDictionaryObject(),l=(0,i.useRef)(null);return l.current??=new Set,
|
|
1
|
+
require(`../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../i18n-store/useI18nStore.cjs`),t=require(`../utils/missing-translation.cjs`),n=require(`./useSubscribeToTrackedLookups.cjs`);let r=require(`gt-i18n/internal`),i=require(`react`);function a(){let a=e.useDictionariesSnapshot(),o=e.useI18nStore(),s=process.env.NODE_ENV!==`production`&&(0,r.getI18nConfig)().isDevHotReloadEnabled(),c=t.useHandleMissingDictionaryObject(),l=(0,i.useRef)(null);return l.current??=new Set,(0,i.useCallback)(e=>{let t=(0,r.getDictionaryListenerKey)(e);s&&l.current.add(t);let n=o.getDictionaryObjectSnapshot(e,a);return n==null&&s&&c(e),n},[o,a,s,c,n.useSubscribeToTrackedLookups(l,o.subscribeToDictionaryObjectEvents,r.getDictionaryListenerKey)])}exports.useTrackedDictionaryObjResolver=a;
|
|
2
2
|
//# sourceMappingURL=useTrackedDictionaryObjResolver.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTrackedDictionaryObjResolver.cjs","names":["useDictionariesSnapshot","useI18nStore","useHandleMissingDictionaryObject","getDictionaryListenerKey"],"sources":["../../../src/hooks/external-store/useTrackedDictionaryObjResolver.ts"],"sourcesContent":["import { getDictionaryListenerKey, getI18nConfig } from 'gt-i18n/internal';\nimport type {\n DictionaryLookup,\n DictionaryObjectSnapshot,\n} from '../../i18n-store/storeTypes';\nimport {\n useDictionariesSnapshot,\n useI18nStore,\n} from '../../i18n-store/useI18nStore';\nimport { useCallback, useRef } from 'react';\nimport { useHandleMissingDictionaryObject } from '../utils/missing-translation';\nimport { useSubscribeToTrackedLookups } from './useSubscribeToTrackedLookups';\n\nexport type TrackedDictionaryObjResolver = (\n lookup: DictionaryLookup\n) => DictionaryObjectSnapshot;\n\n// TODO: rename to useTrackedDictionaryObjectResolver\nexport function useTrackedDictionaryObjResolver(): TrackedDictionaryObjResolver {\n const dictionariesSnapshot = useDictionariesSnapshot();\n const i18nStore = useI18nStore();\n const devHotReloadEnabled =\n process.env.NODE_ENV !== 'production' &&\n getI18nConfig().isDevHotReloadEnabled();\n const onMissingDictionaryObj = useHandleMissingDictionaryObject();\n\n const trackedKeysRef = useRef<Set<string> | null>(null);\n if (trackedKeysRef.current == null) {\n trackedKeysRef.current = new Set();\n }\n\n // subscribe to dictionary object updates\n useSubscribeToTrackedLookups(\n trackedKeysRef,\n i18nStore.subscribeToDictionaryObjectEvents,\n getDictionaryListenerKey\n );\n\n // Resolution callback\n return useCallback(\n (lookup: DictionaryLookup) => {\n // Track the lookup for dev hot reload\n const lookupKey = getDictionaryListenerKey(lookup);\n if (devHotReloadEnabled) {\n trackedKeysRef.current!.add(lookupKey);\n }\n\n // Resolve the dictionary entry from the store\n const dictionaryObject = i18nStore.getDictionaryObjectSnapshot(\n lookup,\n dictionariesSnapshot\n );\n\n // Hot reload\n if (dictionaryObject == null && devHotReloadEnabled) {\n onMissingDictionaryObj(lookup);\n }\n\n return dictionaryObject;\n },\n [\n i18nStore,\n dictionariesSnapshot,\n devHotReloadEnabled,\n onMissingDictionaryObj,\n ]\n );\n}\n"],"mappings":"wPAkBA,SAAgB,GAAgE,CAC9E,IAAM,EAAuBA,EAAAA,yBAAyB,CAChD,EAAYC,EAAAA,cAAc,CAC1B,EACJ,QAAQ,IAAI,WAAa,eAAA,EAAA,EAAA,gBACV,CAAC,uBAAuB,CACnC,EAAyBC,EAAAA,kCAAkC,CAE3D,GAAA,EAAA,EAAA,QAA4C,KAAK,CAavD,MAZA,CACE,EAAe,UAAU,IAAI,
|
|
1
|
+
{"version":3,"file":"useTrackedDictionaryObjResolver.cjs","names":["useDictionariesSnapshot","useI18nStore","useHandleMissingDictionaryObject","useSubscribeToTrackedLookups","getDictionaryListenerKey"],"sources":["../../../src/hooks/external-store/useTrackedDictionaryObjResolver.ts"],"sourcesContent":["import { getDictionaryListenerKey, getI18nConfig } from 'gt-i18n/internal';\nimport type {\n DictionaryLookup,\n DictionaryObjectSnapshot,\n} from '../../i18n-store/storeTypes';\nimport {\n useDictionariesSnapshot,\n useI18nStore,\n} from '../../i18n-store/useI18nStore';\nimport { useCallback, useRef } from 'react';\nimport { useHandleMissingDictionaryObject } from '../utils/missing-translation';\nimport { useSubscribeToTrackedLookups } from './useSubscribeToTrackedLookups';\n\nexport type TrackedDictionaryObjResolver = (\n lookup: DictionaryLookup\n) => DictionaryObjectSnapshot;\n\n// TODO: rename to useTrackedDictionaryObjectResolver\nexport function useTrackedDictionaryObjResolver(): TrackedDictionaryObjResolver {\n const dictionariesSnapshot = useDictionariesSnapshot();\n const i18nStore = useI18nStore();\n const devHotReloadEnabled =\n process.env.NODE_ENV !== 'production' &&\n getI18nConfig().isDevHotReloadEnabled();\n const onMissingDictionaryObj = useHandleMissingDictionaryObject();\n\n const trackedKeysRef = useRef<Set<string> | null>(null);\n if (trackedKeysRef.current == null) {\n trackedKeysRef.current = new Set();\n }\n\n // subscribe to dictionary object updates\n const trackedLookupsVersion = useSubscribeToTrackedLookups(\n trackedKeysRef,\n i18nStore.subscribeToDictionaryObjectEvents,\n getDictionaryListenerKey\n );\n\n // Resolution callback\n return useCallback(\n (lookup: DictionaryLookup) => {\n // Track the lookup for dev hot reload\n const lookupKey = getDictionaryListenerKey(lookup);\n if (devHotReloadEnabled) {\n trackedKeysRef.current!.add(lookupKey);\n }\n\n // Resolve the dictionary entry from the store\n const dictionaryObject = i18nStore.getDictionaryObjectSnapshot(\n lookup,\n dictionariesSnapshot\n );\n\n // Hot reload\n if (dictionaryObject == null && devHotReloadEnabled) {\n onMissingDictionaryObj(lookup);\n }\n\n return dictionaryObject;\n },\n // trackedLookupsVersion: change identity when a tracked entry arrives\n [\n i18nStore,\n dictionariesSnapshot,\n devHotReloadEnabled,\n onMissingDictionaryObj,\n trackedLookupsVersion,\n ]\n );\n}\n"],"mappings":"wPAkBA,SAAgB,GAAgE,CAC9E,IAAM,EAAuBA,EAAAA,yBAAyB,CAChD,EAAYC,EAAAA,cAAc,CAC1B,EACJ,QAAQ,IAAI,WAAa,eAAA,EAAA,EAAA,gBACV,CAAC,uBAAuB,CACnC,EAAyBC,EAAAA,kCAAkC,CAE3D,GAAA,EAAA,EAAA,QAA4C,KAAK,CAavD,MAZA,CACE,EAAe,UAAU,IAAI,KAW/B,EAAA,EAAA,aACG,GAA6B,CAE5B,IAAM,GAAA,EAAA,EAAA,0BAAqC,EAAO,CAC9C,GACF,EAAe,QAAS,IAAI,EAAU,CAIxC,IAAM,EAAmB,EAAU,4BACjC,EACA,EACD,CAOD,OAJI,GAAoB,MAAQ,GAC9B,EAAuB,EAAO,CAGzB,GAGT,CACE,EACA,EACA,EACA,EAjC0BC,EAAAA,6BAC5B,EACA,EAAU,kCACVC,EAAAA,yBA+BuB,CACtB,CACF"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{useDictionariesSnapshot as e,useI18nStore as t}from"../../i18n-store/useI18nStore.mjs";import{useHandleMissingDictionaryObject as n}from"../utils/missing-translation.mjs";import{useSubscribeToTrackedLookups as r}from"./useSubscribeToTrackedLookups.mjs";import{getDictionaryListenerKey as i,getI18nConfig as a}from"gt-i18n/internal";import{useCallback as o,useRef as s}from"react";function c(){let c=e(),l=t(),u=process.env.NODE_ENV!==`production`&&a().isDevHotReloadEnabled(),d=n(),f=s(null);return f.current??=new Set,
|
|
1
|
+
import{useDictionariesSnapshot as e,useI18nStore as t}from"../../i18n-store/useI18nStore.mjs";import{useHandleMissingDictionaryObject as n}from"../utils/missing-translation.mjs";import{useSubscribeToTrackedLookups as r}from"./useSubscribeToTrackedLookups.mjs";import{getDictionaryListenerKey as i,getI18nConfig as a}from"gt-i18n/internal";import{useCallback as o,useRef as s}from"react";function c(){let c=e(),l=t(),u=process.env.NODE_ENV!==`production`&&a().isDevHotReloadEnabled(),d=n(),f=s(null);return f.current??=new Set,o(e=>{let t=i(e);u&&f.current.add(t);let n=l.getDictionaryObjectSnapshot(e,c);return n==null&&u&&d(e),n},[l,c,u,d,r(f,l.subscribeToDictionaryObjectEvents,i)])}export{c as useTrackedDictionaryObjResolver};
|
|
2
2
|
//# sourceMappingURL=useTrackedDictionaryObjResolver.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTrackedDictionaryObjResolver.mjs","names":[],"sources":["../../../src/hooks/external-store/useTrackedDictionaryObjResolver.ts"],"sourcesContent":["import { getDictionaryListenerKey, getI18nConfig } from 'gt-i18n/internal';\nimport type {\n DictionaryLookup,\n DictionaryObjectSnapshot,\n} from '../../i18n-store/storeTypes';\nimport {\n useDictionariesSnapshot,\n useI18nStore,\n} from '../../i18n-store/useI18nStore';\nimport { useCallback, useRef } from 'react';\nimport { useHandleMissingDictionaryObject } from '../utils/missing-translation';\nimport { useSubscribeToTrackedLookups } from './useSubscribeToTrackedLookups';\n\nexport type TrackedDictionaryObjResolver = (\n lookup: DictionaryLookup\n) => DictionaryObjectSnapshot;\n\n// TODO: rename to useTrackedDictionaryObjectResolver\nexport function useTrackedDictionaryObjResolver(): TrackedDictionaryObjResolver {\n const dictionariesSnapshot = useDictionariesSnapshot();\n const i18nStore = useI18nStore();\n const devHotReloadEnabled =\n process.env.NODE_ENV !== 'production' &&\n getI18nConfig().isDevHotReloadEnabled();\n const onMissingDictionaryObj = useHandleMissingDictionaryObject();\n\n const trackedKeysRef = useRef<Set<string> | null>(null);\n if (trackedKeysRef.current == null) {\n trackedKeysRef.current = new Set();\n }\n\n // subscribe to dictionary object updates\n useSubscribeToTrackedLookups(\n trackedKeysRef,\n i18nStore.subscribeToDictionaryObjectEvents,\n getDictionaryListenerKey\n );\n\n // Resolution callback\n return useCallback(\n (lookup: DictionaryLookup) => {\n // Track the lookup for dev hot reload\n const lookupKey = getDictionaryListenerKey(lookup);\n if (devHotReloadEnabled) {\n trackedKeysRef.current!.add(lookupKey);\n }\n\n // Resolve the dictionary entry from the store\n const dictionaryObject = i18nStore.getDictionaryObjectSnapshot(\n lookup,\n dictionariesSnapshot\n );\n\n // Hot reload\n if (dictionaryObject == null && devHotReloadEnabled) {\n onMissingDictionaryObj(lookup);\n }\n\n return dictionaryObject;\n },\n [\n i18nStore,\n dictionariesSnapshot,\n devHotReloadEnabled,\n onMissingDictionaryObj,\n ]\n );\n}\n"],"mappings":"mYAkBA,SAAgB,GAAgE,CAC9E,IAAM,EAAuB,GAAyB,CAChD,EAAY,GAAc,CAC1B,EACJ,QAAQ,IAAI,WAAa,cACzB,GAAe,CAAC,uBAAuB,CACnC,EAAyB,GAAkC,CAE3D,EAAiB,EAA2B,KAAK,CAavD,MAZA,CACE,EAAe,UAAU,IAAI,
|
|
1
|
+
{"version":3,"file":"useTrackedDictionaryObjResolver.mjs","names":[],"sources":["../../../src/hooks/external-store/useTrackedDictionaryObjResolver.ts"],"sourcesContent":["import { getDictionaryListenerKey, getI18nConfig } from 'gt-i18n/internal';\nimport type {\n DictionaryLookup,\n DictionaryObjectSnapshot,\n} from '../../i18n-store/storeTypes';\nimport {\n useDictionariesSnapshot,\n useI18nStore,\n} from '../../i18n-store/useI18nStore';\nimport { useCallback, useRef } from 'react';\nimport { useHandleMissingDictionaryObject } from '../utils/missing-translation';\nimport { useSubscribeToTrackedLookups } from './useSubscribeToTrackedLookups';\n\nexport type TrackedDictionaryObjResolver = (\n lookup: DictionaryLookup\n) => DictionaryObjectSnapshot;\n\n// TODO: rename to useTrackedDictionaryObjectResolver\nexport function useTrackedDictionaryObjResolver(): TrackedDictionaryObjResolver {\n const dictionariesSnapshot = useDictionariesSnapshot();\n const i18nStore = useI18nStore();\n const devHotReloadEnabled =\n process.env.NODE_ENV !== 'production' &&\n getI18nConfig().isDevHotReloadEnabled();\n const onMissingDictionaryObj = useHandleMissingDictionaryObject();\n\n const trackedKeysRef = useRef<Set<string> | null>(null);\n if (trackedKeysRef.current == null) {\n trackedKeysRef.current = new Set();\n }\n\n // subscribe to dictionary object updates\n const trackedLookupsVersion = useSubscribeToTrackedLookups(\n trackedKeysRef,\n i18nStore.subscribeToDictionaryObjectEvents,\n getDictionaryListenerKey\n );\n\n // Resolution callback\n return useCallback(\n (lookup: DictionaryLookup) => {\n // Track the lookup for dev hot reload\n const lookupKey = getDictionaryListenerKey(lookup);\n if (devHotReloadEnabled) {\n trackedKeysRef.current!.add(lookupKey);\n }\n\n // Resolve the dictionary entry from the store\n const dictionaryObject = i18nStore.getDictionaryObjectSnapshot(\n lookup,\n dictionariesSnapshot\n );\n\n // Hot reload\n if (dictionaryObject == null && devHotReloadEnabled) {\n onMissingDictionaryObj(lookup);\n }\n\n return dictionaryObject;\n },\n // trackedLookupsVersion: change identity when a tracked entry arrives\n [\n i18nStore,\n dictionariesSnapshot,\n devHotReloadEnabled,\n onMissingDictionaryObj,\n trackedLookupsVersion,\n ]\n );\n}\n"],"mappings":"mYAkBA,SAAgB,GAAgE,CAC9E,IAAM,EAAuB,GAAyB,CAChD,EAAY,GAAc,CAC1B,EACJ,QAAQ,IAAI,WAAa,cACzB,GAAe,CAAC,uBAAuB,CACnC,EAAyB,GAAkC,CAE3D,EAAiB,EAA2B,KAAK,CAavD,MAZA,CACE,EAAe,UAAU,IAAI,IAWxB,EACJ,GAA6B,CAE5B,IAAM,EAAY,EAAyB,EAAO,CAC9C,GACF,EAAe,QAAS,IAAI,EAAU,CAIxC,IAAM,EAAmB,EAAU,4BACjC,EACA,EACD,CAOD,OAJI,GAAoB,MAAQ,GAC9B,EAAuB,EAAO,CAGzB,GAGT,CACE,EACA,EACA,EACA,EAjC0B,EAC5B,EACA,EAAU,kCACV,EA+BuB,CACtB,CACF"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
require(`../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../i18n-store/useI18nStore.cjs`),t=require(`../utils/missing-translation.cjs`),n=require(`./useSubscribeToTrackedLookups.cjs`);let r=require(`gt-i18n/internal`),i=require(`react`);function a(){let a=e.useDictionariesSnapshot(),o=e.useI18nStore(),s=process.env.NODE_ENV!==`production`&&(0,r.getI18nConfig)().isDevHotReloadEnabled(),c=t.useHandleMissingDictionaryEntry(),l=(0,i.useRef)(null);return l.current??=new Set,
|
|
1
|
+
require(`../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../i18n-store/useI18nStore.cjs`),t=require(`../utils/missing-translation.cjs`),n=require(`./useSubscribeToTrackedLookups.cjs`);let r=require(`gt-i18n/internal`),i=require(`react`);function a(){let a=e.useDictionariesSnapshot(),o=e.useI18nStore(),s=process.env.NODE_ENV!==`production`&&(0,r.getI18nConfig)().isDevHotReloadEnabled(),c=t.useHandleMissingDictionaryEntry(),l=(0,i.useRef)(null);return l.current??=new Set,(0,i.useCallback)(e=>{let t=(0,r.getDictionaryListenerKey)(e);s&&l.current.add(t);let n=o.getDictionaryEntrySnapshot(e,a);return n==null&&s&&c(e),n},[o,a,s,c,n.useSubscribeToTrackedLookups(l,o.subscribeToDictionaryEntryEvents,r.getDictionaryListenerKey)])}exports.useTrackedDictionaryResolver=a;
|
|
2
2
|
//# sourceMappingURL=useTrackedDictionaryResolver.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTrackedDictionaryResolver.cjs","names":["useDictionariesSnapshot","useI18nStore","useHandleMissingDictionaryEntry","getDictionaryListenerKey"],"sources":["../../../src/hooks/external-store/useTrackedDictionaryResolver.ts"],"sourcesContent":["import { useCallback, useRef } from 'react';\nimport {\n useDictionariesSnapshot,\n useI18nStore,\n} from '../../i18n-store/useI18nStore';\nimport type {\n DictionaryEntrySnapshot,\n DictionaryLookup,\n} from '../../i18n-store/storeTypes';\nimport { getDictionaryListenerKey, getI18nConfig } from 'gt-i18n/internal';\nimport { useHandleMissingDictionaryEntry } from '../utils/missing-translation';\nimport { useSubscribeToTrackedLookups } from './useSubscribeToTrackedLookups';\n\nexport type TrackedDictionaryEntryResolver = (\n lookup: DictionaryLookup\n) => DictionaryEntrySnapshot;\n\n// TODO: rename to useTrackedDictionaryEntryResolver\nexport function useTrackedDictionaryResolver(): TrackedDictionaryEntryResolver {\n const dictionariesSnapshot = useDictionariesSnapshot();\n const i18nStore = useI18nStore();\n const devHotReloadEnabled =\n process.env.NODE_ENV !== 'production' &&\n getI18nConfig().isDevHotReloadEnabled();\n const onMissingDictionaryEntry = useHandleMissingDictionaryEntry();\n\n const trackedKeysRef = useRef<Set<string> | null>(null);\n if (trackedKeysRef.current == null) {\n trackedKeysRef.current = new Set();\n }\n\n // subscribe to dictionary entry updates\n useSubscribeToTrackedLookups(\n trackedKeysRef,\n i18nStore.subscribeToDictionaryEntryEvents,\n getDictionaryListenerKey\n );\n\n // Resolution callback\n return useCallback(\n (lookup: DictionaryLookup) => {\n // Track the lookup for dev hot reload\n const lookupKey = getDictionaryListenerKey(lookup);\n if (devHotReloadEnabled) {\n trackedKeysRef.current!.add(lookupKey);\n }\n\n // Resolve the dictionary entry from the store\n const dictionaryEntry = i18nStore.getDictionaryEntrySnapshot(\n lookup,\n dictionariesSnapshot\n );\n\n // Hot reload\n if (dictionaryEntry == null && devHotReloadEnabled) {\n onMissingDictionaryEntry(lookup);\n }\n\n return dictionaryEntry;\n },\n [\n i18nStore,\n dictionariesSnapshot,\n devHotReloadEnabled,\n onMissingDictionaryEntry,\n ]\n );\n}\n"],"mappings":"wPAkBA,SAAgB,GAA+D,CAC7E,IAAM,EAAuBA,EAAAA,yBAAyB,CAChD,EAAYC,EAAAA,cAAc,CAC1B,EACJ,QAAQ,IAAI,WAAa,eAAA,EAAA,EAAA,gBACV,CAAC,uBAAuB,CACnC,EAA2BC,EAAAA,iCAAiC,CAE5D,GAAA,EAAA,EAAA,QAA4C,KAAK,CAavD,MAZA,CACE,EAAe,UAAU,IAAI,
|
|
1
|
+
{"version":3,"file":"useTrackedDictionaryResolver.cjs","names":["useDictionariesSnapshot","useI18nStore","useHandleMissingDictionaryEntry","useSubscribeToTrackedLookups","getDictionaryListenerKey"],"sources":["../../../src/hooks/external-store/useTrackedDictionaryResolver.ts"],"sourcesContent":["import { useCallback, useRef } from 'react';\nimport {\n useDictionariesSnapshot,\n useI18nStore,\n} from '../../i18n-store/useI18nStore';\nimport type {\n DictionaryEntrySnapshot,\n DictionaryLookup,\n} from '../../i18n-store/storeTypes';\nimport { getDictionaryListenerKey, getI18nConfig } from 'gt-i18n/internal';\nimport { useHandleMissingDictionaryEntry } from '../utils/missing-translation';\nimport { useSubscribeToTrackedLookups } from './useSubscribeToTrackedLookups';\n\nexport type TrackedDictionaryEntryResolver = (\n lookup: DictionaryLookup\n) => DictionaryEntrySnapshot;\n\n// TODO: rename to useTrackedDictionaryEntryResolver\nexport function useTrackedDictionaryResolver(): TrackedDictionaryEntryResolver {\n const dictionariesSnapshot = useDictionariesSnapshot();\n const i18nStore = useI18nStore();\n const devHotReloadEnabled =\n process.env.NODE_ENV !== 'production' &&\n getI18nConfig().isDevHotReloadEnabled();\n const onMissingDictionaryEntry = useHandleMissingDictionaryEntry();\n\n const trackedKeysRef = useRef<Set<string> | null>(null);\n if (trackedKeysRef.current == null) {\n trackedKeysRef.current = new Set();\n }\n\n // subscribe to dictionary entry updates\n const trackedLookupsVersion = useSubscribeToTrackedLookups(\n trackedKeysRef,\n i18nStore.subscribeToDictionaryEntryEvents,\n getDictionaryListenerKey\n );\n\n // Resolution callback\n return useCallback(\n (lookup: DictionaryLookup) => {\n // Track the lookup for dev hot reload\n const lookupKey = getDictionaryListenerKey(lookup);\n if (devHotReloadEnabled) {\n trackedKeysRef.current!.add(lookupKey);\n }\n\n // Resolve the dictionary entry from the store\n const dictionaryEntry = i18nStore.getDictionaryEntrySnapshot(\n lookup,\n dictionariesSnapshot\n );\n\n // Hot reload\n if (dictionaryEntry == null && devHotReloadEnabled) {\n onMissingDictionaryEntry(lookup);\n }\n\n return dictionaryEntry;\n },\n // trackedLookupsVersion: change identity when a tracked entry arrives\n [\n i18nStore,\n dictionariesSnapshot,\n devHotReloadEnabled,\n onMissingDictionaryEntry,\n trackedLookupsVersion,\n ]\n );\n}\n"],"mappings":"wPAkBA,SAAgB,GAA+D,CAC7E,IAAM,EAAuBA,EAAAA,yBAAyB,CAChD,EAAYC,EAAAA,cAAc,CAC1B,EACJ,QAAQ,IAAI,WAAa,eAAA,EAAA,EAAA,gBACV,CAAC,uBAAuB,CACnC,EAA2BC,EAAAA,iCAAiC,CAE5D,GAAA,EAAA,EAAA,QAA4C,KAAK,CAavD,MAZA,CACE,EAAe,UAAU,IAAI,KAW/B,EAAA,EAAA,aACG,GAA6B,CAE5B,IAAM,GAAA,EAAA,EAAA,0BAAqC,EAAO,CAC9C,GACF,EAAe,QAAS,IAAI,EAAU,CAIxC,IAAM,EAAkB,EAAU,2BAChC,EACA,EACD,CAOD,OAJI,GAAmB,MAAQ,GAC7B,EAAyB,EAAO,CAG3B,GAGT,CACE,EACA,EACA,EACA,EAjC0BC,EAAAA,6BAC5B,EACA,EAAU,iCACVC,EAAAA,yBA+BuB,CACtB,CACF"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{useDictionariesSnapshot as e,useI18nStore as t}from"../../i18n-store/useI18nStore.mjs";import{useHandleMissingDictionaryEntry as n}from"../utils/missing-translation.mjs";import{useSubscribeToTrackedLookups as r}from"./useSubscribeToTrackedLookups.mjs";import{getDictionaryListenerKey as i,getI18nConfig as a}from"gt-i18n/internal";import{useCallback as o,useRef as s}from"react";function c(){let c=e(),l=t(),u=process.env.NODE_ENV!==`production`&&a().isDevHotReloadEnabled(),d=n(),f=s(null);return f.current??=new Set,
|
|
1
|
+
import{useDictionariesSnapshot as e,useI18nStore as t}from"../../i18n-store/useI18nStore.mjs";import{useHandleMissingDictionaryEntry as n}from"../utils/missing-translation.mjs";import{useSubscribeToTrackedLookups as r}from"./useSubscribeToTrackedLookups.mjs";import{getDictionaryListenerKey as i,getI18nConfig as a}from"gt-i18n/internal";import{useCallback as o,useRef as s}from"react";function c(){let c=e(),l=t(),u=process.env.NODE_ENV!==`production`&&a().isDevHotReloadEnabled(),d=n(),f=s(null);return f.current??=new Set,o(e=>{let t=i(e);u&&f.current.add(t);let n=l.getDictionaryEntrySnapshot(e,c);return n==null&&u&&d(e),n},[l,c,u,d,r(f,l.subscribeToDictionaryEntryEvents,i)])}export{c as useTrackedDictionaryResolver};
|
|
2
2
|
//# sourceMappingURL=useTrackedDictionaryResolver.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTrackedDictionaryResolver.mjs","names":[],"sources":["../../../src/hooks/external-store/useTrackedDictionaryResolver.ts"],"sourcesContent":["import { useCallback, useRef } from 'react';\nimport {\n useDictionariesSnapshot,\n useI18nStore,\n} from '../../i18n-store/useI18nStore';\nimport type {\n DictionaryEntrySnapshot,\n DictionaryLookup,\n} from '../../i18n-store/storeTypes';\nimport { getDictionaryListenerKey, getI18nConfig } from 'gt-i18n/internal';\nimport { useHandleMissingDictionaryEntry } from '../utils/missing-translation';\nimport { useSubscribeToTrackedLookups } from './useSubscribeToTrackedLookups';\n\nexport type TrackedDictionaryEntryResolver = (\n lookup: DictionaryLookup\n) => DictionaryEntrySnapshot;\n\n// TODO: rename to useTrackedDictionaryEntryResolver\nexport function useTrackedDictionaryResolver(): TrackedDictionaryEntryResolver {\n const dictionariesSnapshot = useDictionariesSnapshot();\n const i18nStore = useI18nStore();\n const devHotReloadEnabled =\n process.env.NODE_ENV !== 'production' &&\n getI18nConfig().isDevHotReloadEnabled();\n const onMissingDictionaryEntry = useHandleMissingDictionaryEntry();\n\n const trackedKeysRef = useRef<Set<string> | null>(null);\n if (trackedKeysRef.current == null) {\n trackedKeysRef.current = new Set();\n }\n\n // subscribe to dictionary entry updates\n useSubscribeToTrackedLookups(\n trackedKeysRef,\n i18nStore.subscribeToDictionaryEntryEvents,\n getDictionaryListenerKey\n );\n\n // Resolution callback\n return useCallback(\n (lookup: DictionaryLookup) => {\n // Track the lookup for dev hot reload\n const lookupKey = getDictionaryListenerKey(lookup);\n if (devHotReloadEnabled) {\n trackedKeysRef.current!.add(lookupKey);\n }\n\n // Resolve the dictionary entry from the store\n const dictionaryEntry = i18nStore.getDictionaryEntrySnapshot(\n lookup,\n dictionariesSnapshot\n );\n\n // Hot reload\n if (dictionaryEntry == null && devHotReloadEnabled) {\n onMissingDictionaryEntry(lookup);\n }\n\n return dictionaryEntry;\n },\n [\n i18nStore,\n dictionariesSnapshot,\n devHotReloadEnabled,\n onMissingDictionaryEntry,\n ]\n );\n}\n"],"mappings":"kYAkBA,SAAgB,GAA+D,CAC7E,IAAM,EAAuB,GAAyB,CAChD,EAAY,GAAc,CAC1B,EACJ,QAAQ,IAAI,WAAa,cACzB,GAAe,CAAC,uBAAuB,CACnC,EAA2B,GAAiC,CAE5D,EAAiB,EAA2B,KAAK,CAavD,MAZA,CACE,EAAe,UAAU,IAAI,
|
|
1
|
+
{"version":3,"file":"useTrackedDictionaryResolver.mjs","names":[],"sources":["../../../src/hooks/external-store/useTrackedDictionaryResolver.ts"],"sourcesContent":["import { useCallback, useRef } from 'react';\nimport {\n useDictionariesSnapshot,\n useI18nStore,\n} from '../../i18n-store/useI18nStore';\nimport type {\n DictionaryEntrySnapshot,\n DictionaryLookup,\n} from '../../i18n-store/storeTypes';\nimport { getDictionaryListenerKey, getI18nConfig } from 'gt-i18n/internal';\nimport { useHandleMissingDictionaryEntry } from '../utils/missing-translation';\nimport { useSubscribeToTrackedLookups } from './useSubscribeToTrackedLookups';\n\nexport type TrackedDictionaryEntryResolver = (\n lookup: DictionaryLookup\n) => DictionaryEntrySnapshot;\n\n// TODO: rename to useTrackedDictionaryEntryResolver\nexport function useTrackedDictionaryResolver(): TrackedDictionaryEntryResolver {\n const dictionariesSnapshot = useDictionariesSnapshot();\n const i18nStore = useI18nStore();\n const devHotReloadEnabled =\n process.env.NODE_ENV !== 'production' &&\n getI18nConfig().isDevHotReloadEnabled();\n const onMissingDictionaryEntry = useHandleMissingDictionaryEntry();\n\n const trackedKeysRef = useRef<Set<string> | null>(null);\n if (trackedKeysRef.current == null) {\n trackedKeysRef.current = new Set();\n }\n\n // subscribe to dictionary entry updates\n const trackedLookupsVersion = useSubscribeToTrackedLookups(\n trackedKeysRef,\n i18nStore.subscribeToDictionaryEntryEvents,\n getDictionaryListenerKey\n );\n\n // Resolution callback\n return useCallback(\n (lookup: DictionaryLookup) => {\n // Track the lookup for dev hot reload\n const lookupKey = getDictionaryListenerKey(lookup);\n if (devHotReloadEnabled) {\n trackedKeysRef.current!.add(lookupKey);\n }\n\n // Resolve the dictionary entry from the store\n const dictionaryEntry = i18nStore.getDictionaryEntrySnapshot(\n lookup,\n dictionariesSnapshot\n );\n\n // Hot reload\n if (dictionaryEntry == null && devHotReloadEnabled) {\n onMissingDictionaryEntry(lookup);\n }\n\n return dictionaryEntry;\n },\n // trackedLookupsVersion: change identity when a tracked entry arrives\n [\n i18nStore,\n dictionariesSnapshot,\n devHotReloadEnabled,\n onMissingDictionaryEntry,\n trackedLookupsVersion,\n ]\n );\n}\n"],"mappings":"kYAkBA,SAAgB,GAA+D,CAC7E,IAAM,EAAuB,GAAyB,CAChD,EAAY,GAAc,CAC1B,EACJ,QAAQ,IAAI,WAAa,cACzB,GAAe,CAAC,uBAAuB,CACnC,EAA2B,GAAiC,CAE5D,EAAiB,EAA2B,KAAK,CAavD,MAZA,CACE,EAAe,UAAU,IAAI,IAWxB,EACJ,GAA6B,CAE5B,IAAM,EAAY,EAAyB,EAAO,CAC9C,GACF,EAAe,QAAS,IAAI,EAAU,CAIxC,IAAM,EAAkB,EAAU,2BAChC,EACA,EACD,CAOD,OAJI,GAAmB,MAAQ,GAC7B,EAAyB,EAAO,CAG3B,GAGT,CACE,EACA,EACA,EACA,EAjC0B,EAC5B,EACA,EAAU,iCACV,EA+BuB,CACtB,CACF"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
require(`../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../i18n-store/useI18nStore.cjs`),t=require(`../utils/missing-translation.cjs`),n=require(`./useSubscribeToTrackedLookups.cjs`);let r=require(`gt-i18n/internal`),i=require(`react`);function a(a=[],s,c){let l=e.useTranslationsSnapshot(),u=e.useI18nStore(),d=process.env.NODE_ENV!==`production`&&(0,r.getI18nConfig)().isDevHotReloadEnabled(),f=t.useHandleMissingTranslationWithConditions(c),p=(0,i.useRef)(null);return p.current??=new Set,o(a,p,s,c),
|
|
1
|
+
require(`../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../i18n-store/useI18nStore.cjs`),t=require(`../utils/missing-translation.cjs`),n=require(`./useSubscribeToTrackedLookups.cjs`);let r=require(`gt-i18n/internal`),i=require(`react`);function a(a=[],s,c){let l=e.useTranslationsSnapshot(),u=e.useI18nStore(),d=process.env.NODE_ENV!==`production`&&(0,r.getI18nConfig)().isDevHotReloadEnabled(),f=t.useHandleMissingTranslationWithConditions(c),p=(0,i.useRef)(null);return p.current??=new Set,o(a,p,s,c),(0,i.useCallback)(e=>{let t=(0,r.getTranslateListenerKey)(e);d&&p.current.add(t);let n=u.getTranslateSnapshot(e,l);return n==null&&d&&f(e),n},[u,l,f,d,n.useSubscribeToTrackedLookups(p,u.subscribeToTranslationEvents,r.getTranslateListenerKey)])}function o(t,n,a,o){let s=e.useI18nStore(),c=process.env.NODE_ENV!==`production`&&(0,r.getI18nConfig)().isDevHotReloadEnabled(),l=e.useTranslationsSnapshot(),u=(0,i.useMemo)(()=>t?.length>0&&o&&c,[t,o,c]),d=(0,i.useMemo)(()=>u?t.map(({message:e,...t})=>{let n=t.$locale??a,i={locale:n,message:e,options:(0,r.createLookupOptions)(n,t,`ICU`)};return[i,(0,r.getTranslateListenerKey)(i)]}):[],[a,u,t]);u&&d.forEach(([,e])=>{n.current.add(e)}),(0,i.useEffect)(()=>{u&&d.filter(([e])=>s.getTranslateSnapshot(e,l)==null).forEach(([e])=>s.translate(e))},[u,s,d,l])}exports.useTrackedTranslationResolver=a;
|
|
2
2
|
//# sourceMappingURL=useTrackedTranslationResolver.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTrackedTranslationResolver.cjs","names":["useTranslationsSnapshot","useI18nStore","useHandleMissingTranslationWithConditions","getTranslateListenerKey"],"sources":["../../../src/hooks/external-store/useTrackedTranslationResolver.ts"],"sourcesContent":["import { type RefObject, useCallback, useEffect, useMemo, useRef } from 'react';\nimport {\n createLookupOptions,\n getI18nConfig,\n getTranslateListenerKey,\n} from 'gt-i18n/internal';\nimport type { Translation } from 'gt-i18n/types';\nimport type {\n TranslateLookup,\n TranslateSnapshot,\n} from '../../i18n-store/storeTypes';\nimport type { TranslationMetadata } from 'gt-i18n/internal/types';\nimport type { StringFormat } from '@generaltranslation/format';\nimport {\n useI18nStore,\n useTranslationsSnapshot,\n} from '../../i18n-store/useI18nStore';\nimport { useHandleMissingTranslationWithConditions } from '../utils/missing-translation';\nimport { useSubscribeToTrackedLookups } from './useSubscribeToTrackedLookups';\n\n/**\n * Returns the translation, but also triggers a translation if it is not found\n * when dev hot reload is enabled.\n */\nexport type TrackedTranslationResolver = <T extends Translation>(\n lookup: TranslateLookup<T>\n) => TranslateSnapshot<T>;\n\nexport type Message = TranslationMetadata & {\n message: string;\n};\n\n/**\n * NOTE:\n * gt() may be called during render, so tracking intentionally only mutates\n * hook-local refs. This widens the event filter used by our existing\n * useSyncExternalStore subscription, but does not update React state or mutate\n * the translation cache during render.\n *\n * A render that is later aborted may leave behind an extra tracked key. That is\n * acceptable for dev hot reload because the worst case is an unnecessary\n * invalidation for this hook instance, not incorrect rendered output.\n */\n\nexport function useTrackedTranslationResolver(\n messages: Message[] = [],\n locale: string,\n shouldTranslate: boolean\n): TrackedTranslationResolver {\n const translationsSnapshot = useTranslationsSnapshot();\n const i18nStore = useI18nStore();\n const devHotReloadEnabled =\n process.env.NODE_ENV !== 'production' &&\n getI18nConfig().isDevHotReloadEnabled();\n const onMissingTranslation =\n useHandleMissingTranslationWithConditions(shouldTranslate);\n\n /**\n * Track lookups per hook instance without updating React state during render.\n */\n const trackedKeysRef = useRef<Set<string> | null>(null);\n if (trackedKeysRef.current == null) {\n trackedKeysRef.current = new Set();\n }\n\n // (optimization) Pre-subscribe to compiler-injected lookups\n usePreloadCompilerLookups(messages, trackedKeysRef, locale, shouldTranslate);\n\n // (tx hot reload) Subscribe to translation updates\n useSubscribeToTrackedLookups(\n trackedKeysRef,\n i18nStore.subscribeToTranslationEvents,\n getTranslateListenerKey\n );\n\n /**\n * Remember that we can make no assumptions about when this cb gets invoked\n * - Resolves translation from lookup\n * - enqueues dev hot reload (if needed)\n */\n return useCallback(\n <T extends Translation>(\n lookup: TranslateLookup<T>\n ): TranslateSnapshot<T> => {\n // Track the lookup for dev hot reload\n const lookupKey = getTranslateListenerKey(lookup);\n if (devHotReloadEnabled) {\n trackedKeysRef.current!.add(lookupKey);\n }\n\n // Resolve the translation from the store\n const translation = i18nStore.getTranslateSnapshot(\n lookup,\n translationsSnapshot\n );\n\n // Trigger a hot reload if the translation is not found\n if (translation == null && devHotReloadEnabled) {\n onMissingTranslation(lookup);\n }\n return translation;\n },\n [i18nStore
|
|
1
|
+
{"version":3,"file":"useTrackedTranslationResolver.cjs","names":["useTranslationsSnapshot","useI18nStore","useHandleMissingTranslationWithConditions","useSubscribeToTrackedLookups","getTranslateListenerKey"],"sources":["../../../src/hooks/external-store/useTrackedTranslationResolver.ts"],"sourcesContent":["import { type RefObject, useCallback, useEffect, useMemo, useRef } from 'react';\nimport {\n createLookupOptions,\n getI18nConfig,\n getTranslateListenerKey,\n} from 'gt-i18n/internal';\nimport type { Translation } from 'gt-i18n/types';\nimport type {\n TranslateLookup,\n TranslateSnapshot,\n} from '../../i18n-store/storeTypes';\nimport type { TranslationMetadata } from 'gt-i18n/internal/types';\nimport type { StringFormat } from '@generaltranslation/format';\nimport {\n useI18nStore,\n useTranslationsSnapshot,\n} from '../../i18n-store/useI18nStore';\nimport { useHandleMissingTranslationWithConditions } from '../utils/missing-translation';\nimport { useSubscribeToTrackedLookups } from './useSubscribeToTrackedLookups';\n\n/**\n * Returns the translation, but also triggers a translation if it is not found\n * when dev hot reload is enabled.\n */\nexport type TrackedTranslationResolver = <T extends Translation>(\n lookup: TranslateLookup<T>\n) => TranslateSnapshot<T>;\n\nexport type Message = TranslationMetadata & {\n message: string;\n};\n\n/**\n * NOTE:\n * gt() may be called during render, so tracking intentionally only mutates\n * hook-local refs. This widens the event filter used by our existing\n * useSyncExternalStore subscription, but does not update React state or mutate\n * the translation cache during render.\n *\n * A render that is later aborted may leave behind an extra tracked key. That is\n * acceptable for dev hot reload because the worst case is an unnecessary\n * invalidation for this hook instance, not incorrect rendered output.\n */\n\nexport function useTrackedTranslationResolver(\n messages: Message[] = [],\n locale: string,\n shouldTranslate: boolean\n): TrackedTranslationResolver {\n const translationsSnapshot = useTranslationsSnapshot();\n const i18nStore = useI18nStore();\n const devHotReloadEnabled =\n process.env.NODE_ENV !== 'production' &&\n getI18nConfig().isDevHotReloadEnabled();\n const onMissingTranslation =\n useHandleMissingTranslationWithConditions(shouldTranslate);\n\n /**\n * Track lookups per hook instance without updating React state during render.\n */\n const trackedKeysRef = useRef<Set<string> | null>(null);\n if (trackedKeysRef.current == null) {\n trackedKeysRef.current = new Set();\n }\n\n // (optimization) Pre-subscribe to compiler-injected lookups\n usePreloadCompilerLookups(messages, trackedKeysRef, locale, shouldTranslate);\n\n // (tx hot reload) Subscribe to translation updates\n const trackedLookupsVersion = useSubscribeToTrackedLookups(\n trackedKeysRef,\n i18nStore.subscribeToTranslationEvents,\n getTranslateListenerKey\n );\n\n /**\n * Remember that we can make no assumptions about when this cb gets invoked\n * - Resolves translation from lookup\n * - enqueues dev hot reload (if needed)\n */\n return useCallback(\n <T extends Translation>(\n lookup: TranslateLookup<T>\n ): TranslateSnapshot<T> => {\n // Track the lookup for dev hot reload\n const lookupKey = getTranslateListenerKey(lookup);\n if (devHotReloadEnabled) {\n trackedKeysRef.current!.add(lookupKey);\n }\n\n // Resolve the translation from the store\n const translation = i18nStore.getTranslateSnapshot(\n lookup,\n translationsSnapshot\n );\n\n // Trigger a hot reload if the translation is not found\n if (translation == null && devHotReloadEnabled) {\n onMissingTranslation(lookup);\n }\n return translation;\n },\n // trackedLookupsVersion: change identity when a tracked translation arrives\n [\n i18nStore,\n translationsSnapshot,\n onMissingTranslation,\n devHotReloadEnabled,\n trackedLookupsVersion,\n ]\n );\n}\n\n/**\n * Pre-subscribe to compiler-injected lookups\n * trigger translations for them\n *\n * This ref mutation is safe because this is translation hot reload only\n * and effects are deterministic\n */\nfunction usePreloadCompilerLookups(\n messages: Message[],\n trackedKeysRef: RefObject<Set<string> | null>,\n locale: string,\n shouldTranslate: boolean\n) {\n const i18nStore = useI18nStore();\n const devHotReloadEnabled =\n process.env.NODE_ENV !== 'production' &&\n getI18nConfig().isDevHotReloadEnabled();\n const translationsSnapshot = useTranslationsSnapshot();\n const txHotReloadEnabled = useMemo(() => {\n return messages?.length > 0 && shouldTranslate && devHotReloadEnabled;\n }, [messages, shouldTranslate, devHotReloadEnabled]);\n\n // Precompute as much as we can\n const lookups: [TranslateLookup<string>, string][] = useMemo(() => {\n if (!txHotReloadEnabled) return [];\n return messages.map(({ message, ...options }) => {\n const targetLocale = options.$locale ?? locale;\n const lookupOptions = createLookupOptions<StringFormat>(\n targetLocale,\n options,\n 'ICU'\n );\n const lookup = {\n locale: targetLocale,\n message,\n options: lookupOptions,\n };\n return [lookup, getTranslateListenerKey(lookup)];\n });\n }, [locale, txHotReloadEnabled, messages]);\n\n // Pre-subscribe to the lookups\n if (txHotReloadEnabled) {\n lookups.forEach(([, lookupKey]) => {\n trackedKeysRef.current!.add(lookupKey);\n });\n }\n\n /**\n * Delegate non-pure operations to post commit\n *\n * (Optimization) Trigger a hot reload if the translation is not found\n * for compiler-injected lookups\n *\n * TODO: for use() + suspense strategy, use a Promise.all()\n * TODO: server side translation invocation\n */\n useEffect(() => {\n if (!txHotReloadEnabled) return;\n\n lookups\n .filter(\n ([lookup]) =>\n i18nStore.getTranslateSnapshot(lookup, translationsSnapshot) == null\n )\n .forEach(([lookup]) => i18nStore.translate(lookup));\n }, [txHotReloadEnabled, i18nStore, lookups, translationsSnapshot]);\n}\n"],"mappings":"wPA4CA,SAAgB,EACd,EAAsB,EAAE,CACxB,EACA,EAC4B,CAC5B,IAAM,EAAuBA,EAAAA,yBAAyB,CAChD,EAAYC,EAAAA,cAAc,CAC1B,EACJ,QAAQ,IAAI,WAAa,eAAA,EAAA,EAAA,gBACV,CAAC,uBAAuB,CACnC,EACJC,EAAAA,0CAA0C,EAAgB,CAKtD,GAAA,EAAA,EAAA,QAA4C,KAAK,CAoBvD,MAnBA,CACE,EAAe,UAAU,IAAI,IAI/B,EAA0B,EAAU,EAAgB,EAAQ,EAAgB,EAc5E,EAAA,EAAA,aAEI,GACyB,CAEzB,IAAM,GAAA,EAAA,EAAA,yBAAoC,EAAO,CAC7C,GACF,EAAe,QAAS,IAAI,EAAU,CAIxC,IAAM,EAAc,EAAU,qBAC5B,EACA,EACD,CAMD,OAHI,GAAe,MAAQ,GACzB,EAAqB,EAAO,CAEvB,GAGT,CACE,EACA,EACA,EACA,EAtC0BC,EAAAA,6BAC5B,EACA,EAAU,6BACVC,EAAAA,wBAoCuB,CACtB,CACF,CAUH,SAAS,EACP,EACA,EACA,EACA,EACA,CACA,IAAM,EAAYH,EAAAA,cAAc,CAC1B,EACJ,QAAQ,IAAI,WAAa,eAAA,EAAA,EAAA,gBACV,CAAC,uBAAuB,CACnC,EAAuBD,EAAAA,yBAAyB,CAChD,GAAA,EAAA,EAAA,aACG,GAAU,OAAS,GAAK,GAAmB,EACjD,CAAC,EAAU,EAAiB,EAAoB,CAAC,CAG9C,GAAA,EAAA,EAAA,aACC,EACE,EAAS,KAAK,CAAE,UAAS,GAAG,KAAc,CAC/C,IAAM,EAAe,EAAQ,SAAW,EAMlC,EAAS,CACb,OAAQ,EACR,UACA,SAAA,EAAA,EAAA,qBAPA,EACA,EACA,MAKsB,CACvB,CACD,MAAO,CAAC,GAAA,EAAA,EAAA,yBAAgC,EAAO,CAAC,EAChD,CAd8B,EAAE,CAejC,CAAC,EAAQ,EAAoB,EAAS,CAAC,CAGtC,GACF,EAAQ,SAAS,EAAG,KAAe,CACjC,EAAe,QAAS,IAAI,EAAU,EACtC,EAYJ,EAAA,EAAA,eAAgB,CACT,GAEL,EACG,QACE,CAAC,KACA,EAAU,qBAAqB,EAAQ,EAAqB,EAAI,KACnE,CACA,SAAS,CAAC,KAAY,EAAU,UAAU,EAAO,CAAC,EACpD,CAAC,EAAoB,EAAW,EAAS,EAAqB,CAAC"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{useI18nStore as e,useTranslationsSnapshot as t}from"../../i18n-store/useI18nStore.mjs";import{useHandleMissingTranslationWithConditions as n}from"../utils/missing-translation.mjs";import{useSubscribeToTrackedLookups as r}from"./useSubscribeToTrackedLookups.mjs";import{createLookupOptions as i,getI18nConfig as a,getTranslateListenerKey as o}from"gt-i18n/internal";import{useCallback as s,useEffect as c,useMemo as l,useRef as u}from"react";function d(i=[],c,l){let d=t(),p=e(),m=process.env.NODE_ENV!==`production`&&a().isDevHotReloadEnabled(),h=n(l),g=u(null);return g.current??=new Set,f(i,g,c,l),
|
|
1
|
+
import{useI18nStore as e,useTranslationsSnapshot as t}from"../../i18n-store/useI18nStore.mjs";import{useHandleMissingTranslationWithConditions as n}from"../utils/missing-translation.mjs";import{useSubscribeToTrackedLookups as r}from"./useSubscribeToTrackedLookups.mjs";import{createLookupOptions as i,getI18nConfig as a,getTranslateListenerKey as o}from"gt-i18n/internal";import{useCallback as s,useEffect as c,useMemo as l,useRef as u}from"react";function d(i=[],c,l){let d=t(),p=e(),m=process.env.NODE_ENV!==`production`&&a().isDevHotReloadEnabled(),h=n(l),g=u(null);return g.current??=new Set,f(i,g,c,l),s(e=>{let t=o(e);m&&g.current.add(t);let n=p.getTranslateSnapshot(e,d);return n==null&&m&&h(e),n},[p,d,h,m,r(g,p.subscribeToTranslationEvents,o)])}function f(n,r,s,u){let d=e(),f=process.env.NODE_ENV!==`production`&&a().isDevHotReloadEnabled(),p=t(),m=l(()=>n?.length>0&&u&&f,[n,u,f]),h=l(()=>m?n.map(({message:e,...t})=>{let n=t.$locale??s,r={locale:n,message:e,options:i(n,t,`ICU`)};return[r,o(r)]}):[],[s,m,n]);m&&h.forEach(([,e])=>{r.current.add(e)}),c(()=>{m&&h.filter(([e])=>d.getTranslateSnapshot(e,p)==null).forEach(([e])=>d.translate(e))},[m,d,h,p])}export{d as useTrackedTranslationResolver};
|
|
2
2
|
//# sourceMappingURL=useTrackedTranslationResolver.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTrackedTranslationResolver.mjs","names":[],"sources":["../../../src/hooks/external-store/useTrackedTranslationResolver.ts"],"sourcesContent":["import { type RefObject, useCallback, useEffect, useMemo, useRef } from 'react';\nimport {\n createLookupOptions,\n getI18nConfig,\n getTranslateListenerKey,\n} from 'gt-i18n/internal';\nimport type { Translation } from 'gt-i18n/types';\nimport type {\n TranslateLookup,\n TranslateSnapshot,\n} from '../../i18n-store/storeTypes';\nimport type { TranslationMetadata } from 'gt-i18n/internal/types';\nimport type { StringFormat } from '@generaltranslation/format';\nimport {\n useI18nStore,\n useTranslationsSnapshot,\n} from '../../i18n-store/useI18nStore';\nimport { useHandleMissingTranslationWithConditions } from '../utils/missing-translation';\nimport { useSubscribeToTrackedLookups } from './useSubscribeToTrackedLookups';\n\n/**\n * Returns the translation, but also triggers a translation if it is not found\n * when dev hot reload is enabled.\n */\nexport type TrackedTranslationResolver = <T extends Translation>(\n lookup: TranslateLookup<T>\n) => TranslateSnapshot<T>;\n\nexport type Message = TranslationMetadata & {\n message: string;\n};\n\n/**\n * NOTE:\n * gt() may be called during render, so tracking intentionally only mutates\n * hook-local refs. This widens the event filter used by our existing\n * useSyncExternalStore subscription, but does not update React state or mutate\n * the translation cache during render.\n *\n * A render that is later aborted may leave behind an extra tracked key. That is\n * acceptable for dev hot reload because the worst case is an unnecessary\n * invalidation for this hook instance, not incorrect rendered output.\n */\n\nexport function useTrackedTranslationResolver(\n messages: Message[] = [],\n locale: string,\n shouldTranslate: boolean\n): TrackedTranslationResolver {\n const translationsSnapshot = useTranslationsSnapshot();\n const i18nStore = useI18nStore();\n const devHotReloadEnabled =\n process.env.NODE_ENV !== 'production' &&\n getI18nConfig().isDevHotReloadEnabled();\n const onMissingTranslation =\n useHandleMissingTranslationWithConditions(shouldTranslate);\n\n /**\n * Track lookups per hook instance without updating React state during render.\n */\n const trackedKeysRef = useRef<Set<string> | null>(null);\n if (trackedKeysRef.current == null) {\n trackedKeysRef.current = new Set();\n }\n\n // (optimization) Pre-subscribe to compiler-injected lookups\n usePreloadCompilerLookups(messages, trackedKeysRef, locale, shouldTranslate);\n\n // (tx hot reload) Subscribe to translation updates\n useSubscribeToTrackedLookups(\n trackedKeysRef,\n i18nStore.subscribeToTranslationEvents,\n getTranslateListenerKey\n );\n\n /**\n * Remember that we can make no assumptions about when this cb gets invoked\n * - Resolves translation from lookup\n * - enqueues dev hot reload (if needed)\n */\n return useCallback(\n <T extends Translation>(\n lookup: TranslateLookup<T>\n ): TranslateSnapshot<T> => {\n // Track the lookup for dev hot reload\n const lookupKey = getTranslateListenerKey(lookup);\n if (devHotReloadEnabled) {\n trackedKeysRef.current!.add(lookupKey);\n }\n\n // Resolve the translation from the store\n const translation = i18nStore.getTranslateSnapshot(\n lookup,\n translationsSnapshot\n );\n\n // Trigger a hot reload if the translation is not found\n if (translation == null && devHotReloadEnabled) {\n onMissingTranslation(lookup);\n }\n return translation;\n },\n [i18nStore
|
|
1
|
+
{"version":3,"file":"useTrackedTranslationResolver.mjs","names":[],"sources":["../../../src/hooks/external-store/useTrackedTranslationResolver.ts"],"sourcesContent":["import { type RefObject, useCallback, useEffect, useMemo, useRef } from 'react';\nimport {\n createLookupOptions,\n getI18nConfig,\n getTranslateListenerKey,\n} from 'gt-i18n/internal';\nimport type { Translation } from 'gt-i18n/types';\nimport type {\n TranslateLookup,\n TranslateSnapshot,\n} from '../../i18n-store/storeTypes';\nimport type { TranslationMetadata } from 'gt-i18n/internal/types';\nimport type { StringFormat } from '@generaltranslation/format';\nimport {\n useI18nStore,\n useTranslationsSnapshot,\n} from '../../i18n-store/useI18nStore';\nimport { useHandleMissingTranslationWithConditions } from '../utils/missing-translation';\nimport { useSubscribeToTrackedLookups } from './useSubscribeToTrackedLookups';\n\n/**\n * Returns the translation, but also triggers a translation if it is not found\n * when dev hot reload is enabled.\n */\nexport type TrackedTranslationResolver = <T extends Translation>(\n lookup: TranslateLookup<T>\n) => TranslateSnapshot<T>;\n\nexport type Message = TranslationMetadata & {\n message: string;\n};\n\n/**\n * NOTE:\n * gt() may be called during render, so tracking intentionally only mutates\n * hook-local refs. This widens the event filter used by our existing\n * useSyncExternalStore subscription, but does not update React state or mutate\n * the translation cache during render.\n *\n * A render that is later aborted may leave behind an extra tracked key. That is\n * acceptable for dev hot reload because the worst case is an unnecessary\n * invalidation for this hook instance, not incorrect rendered output.\n */\n\nexport function useTrackedTranslationResolver(\n messages: Message[] = [],\n locale: string,\n shouldTranslate: boolean\n): TrackedTranslationResolver {\n const translationsSnapshot = useTranslationsSnapshot();\n const i18nStore = useI18nStore();\n const devHotReloadEnabled =\n process.env.NODE_ENV !== 'production' &&\n getI18nConfig().isDevHotReloadEnabled();\n const onMissingTranslation =\n useHandleMissingTranslationWithConditions(shouldTranslate);\n\n /**\n * Track lookups per hook instance without updating React state during render.\n */\n const trackedKeysRef = useRef<Set<string> | null>(null);\n if (trackedKeysRef.current == null) {\n trackedKeysRef.current = new Set();\n }\n\n // (optimization) Pre-subscribe to compiler-injected lookups\n usePreloadCompilerLookups(messages, trackedKeysRef, locale, shouldTranslate);\n\n // (tx hot reload) Subscribe to translation updates\n const trackedLookupsVersion = useSubscribeToTrackedLookups(\n trackedKeysRef,\n i18nStore.subscribeToTranslationEvents,\n getTranslateListenerKey\n );\n\n /**\n * Remember that we can make no assumptions about when this cb gets invoked\n * - Resolves translation from lookup\n * - enqueues dev hot reload (if needed)\n */\n return useCallback(\n <T extends Translation>(\n lookup: TranslateLookup<T>\n ): TranslateSnapshot<T> => {\n // Track the lookup for dev hot reload\n const lookupKey = getTranslateListenerKey(lookup);\n if (devHotReloadEnabled) {\n trackedKeysRef.current!.add(lookupKey);\n }\n\n // Resolve the translation from the store\n const translation = i18nStore.getTranslateSnapshot(\n lookup,\n translationsSnapshot\n );\n\n // Trigger a hot reload if the translation is not found\n if (translation == null && devHotReloadEnabled) {\n onMissingTranslation(lookup);\n }\n return translation;\n },\n // trackedLookupsVersion: change identity when a tracked translation arrives\n [\n i18nStore,\n translationsSnapshot,\n onMissingTranslation,\n devHotReloadEnabled,\n trackedLookupsVersion,\n ]\n );\n}\n\n/**\n * Pre-subscribe to compiler-injected lookups\n * trigger translations for them\n *\n * This ref mutation is safe because this is translation hot reload only\n * and effects are deterministic\n */\nfunction usePreloadCompilerLookups(\n messages: Message[],\n trackedKeysRef: RefObject<Set<string> | null>,\n locale: string,\n shouldTranslate: boolean\n) {\n const i18nStore = useI18nStore();\n const devHotReloadEnabled =\n process.env.NODE_ENV !== 'production' &&\n getI18nConfig().isDevHotReloadEnabled();\n const translationsSnapshot = useTranslationsSnapshot();\n const txHotReloadEnabled = useMemo(() => {\n return messages?.length > 0 && shouldTranslate && devHotReloadEnabled;\n }, [messages, shouldTranslate, devHotReloadEnabled]);\n\n // Precompute as much as we can\n const lookups: [TranslateLookup<string>, string][] = useMemo(() => {\n if (!txHotReloadEnabled) return [];\n return messages.map(({ message, ...options }) => {\n const targetLocale = options.$locale ?? locale;\n const lookupOptions = createLookupOptions<StringFormat>(\n targetLocale,\n options,\n 'ICU'\n );\n const lookup = {\n locale: targetLocale,\n message,\n options: lookupOptions,\n };\n return [lookup, getTranslateListenerKey(lookup)];\n });\n }, [locale, txHotReloadEnabled, messages]);\n\n // Pre-subscribe to the lookups\n if (txHotReloadEnabled) {\n lookups.forEach(([, lookupKey]) => {\n trackedKeysRef.current!.add(lookupKey);\n });\n }\n\n /**\n * Delegate non-pure operations to post commit\n *\n * (Optimization) Trigger a hot reload if the translation is not found\n * for compiler-injected lookups\n *\n * TODO: for use() + suspense strategy, use a Promise.all()\n * TODO: server side translation invocation\n */\n useEffect(() => {\n if (!txHotReloadEnabled) return;\n\n lookups\n .filter(\n ([lookup]) =>\n i18nStore.getTranslateSnapshot(lookup, translationsSnapshot) == null\n )\n .forEach(([lookup]) => i18nStore.translate(lookup));\n }, [txHotReloadEnabled, i18nStore, lookups, translationsSnapshot]);\n}\n"],"mappings":"gcA4CA,SAAgB,EACd,EAAsB,EAAE,CACxB,EACA,EAC4B,CAC5B,IAAM,EAAuB,GAAyB,CAChD,EAAY,GAAc,CAC1B,EACJ,QAAQ,IAAI,WAAa,cACzB,GAAe,CAAC,uBAAuB,CACnC,EACJ,EAA0C,EAAgB,CAKtD,EAAiB,EAA2B,KAAK,CAoBvD,MAnBA,CACE,EAAe,UAAU,IAAI,IAI/B,EAA0B,EAAU,EAAgB,EAAQ,EAAgB,CAcrE,EAEH,GACyB,CAEzB,IAAM,EAAY,EAAwB,EAAO,CAC7C,GACF,EAAe,QAAS,IAAI,EAAU,CAIxC,IAAM,EAAc,EAAU,qBAC5B,EACA,EACD,CAMD,OAHI,GAAe,MAAQ,GACzB,EAAqB,EAAO,CAEvB,GAGT,CACE,EACA,EACA,EACA,EAtC0B,EAC5B,EACA,EAAU,6BACV,EAoCuB,CACtB,CACF,CAUH,SAAS,EACP,EACA,EACA,EACA,EACA,CACA,IAAM,EAAY,GAAc,CAC1B,EACJ,QAAQ,IAAI,WAAa,cACzB,GAAe,CAAC,uBAAuB,CACnC,EAAuB,GAAyB,CAChD,EAAqB,MAClB,GAAU,OAAS,GAAK,GAAmB,EACjD,CAAC,EAAU,EAAiB,EAAoB,CAAC,CAG9C,EAA+C,MAC9C,EACE,EAAS,KAAK,CAAE,UAAS,GAAG,KAAc,CAC/C,IAAM,EAAe,EAAQ,SAAW,EAMlC,EAAS,CACb,OAAQ,EACR,UACA,QARoB,EACpB,EACA,EACA,MAKsB,CACvB,CACD,MAAO,CAAC,EAAQ,EAAwB,EAAO,CAAC,EAChD,CAd8B,EAAE,CAejC,CAAC,EAAQ,EAAoB,EAAS,CAAC,CAGtC,GACF,EAAQ,SAAS,EAAG,KAAe,CACjC,EAAe,QAAS,IAAI,EAAU,EACtC,CAYJ,MAAgB,CACT,GAEL,EACG,QACE,CAAC,KACA,EAAU,qBAAqB,EAAQ,EAAqB,EAAI,KACnE,CACA,SAAS,CAAC,KAAY,EAAU,UAAU,EAAO,CAAC,EACpD,CAAC,EAAoB,EAAW,EAAS,EAAqB,CAAC"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
require(`../_virtual/_rolldown/runtime.cjs`);const e=require(`./condition-store.cjs`),t=require(`./i18n-config.cjs`),n=require(`./utils.cjs`),r=require(`./useGT.cjs`),i=require(`./external-store/useTrackedDictionaryResolver.cjs`),a=require(`./external-store/useTrackedDictionaryObjResolver.cjs`);let o=require(`gt-i18n/internal`),s=require(`react`);function c(a){let c=e.useLocale(),d=t.useDefaultLocale(),f=n.useShouldTranslate(),p=r.useGT(),m=i.useTrackedDictionaryResolver(),h=l(a),g=(0,s.useCallback)((e,t={})=>{let n=u(a,e),r=m({locale:d,id:n});if(r===void 0)throw Error(`Dictionary entry ${n} cannot be found`);let i=(0,o.resolveDictionaryLookupOptions)(r.options);if(!f)return p(r.entry,{...i,...(0,o.extractVariables)(t),$locale:d});let s=m({locale:c,id:n});return s?.entry==null?p(r.entry,{...i,...(0,o.extractVariables)(t),$locale:c}):(0,o.renderDictionaryEntry)({sourceLocale:d,targetLocale:c,sourceEntry:r,target:s.entry,dictionaryOptions:i,options:t})},[d,p,a,c,m,f]);return(0,s.useMemo)(()=>Object.assign(g,{obj:h}),[g,h])}function l(i){let c=e.useLocale(),l=t.useDefaultLocale(),d=n.useShouldTranslate(),f=r.useGT(),p=a.useTrackedDictionaryObjResolver();return(0,s.useCallback)(e=>{let t=u(i,e),n=p({locale:l,id:t});if(n===void 0)throw Error(`Dictionary entry ${t} cannot be found`);let r;return d&&(r=p({locale:c,id:t})),(0,o.renderDictionaryObject)({sourceObject:n,targetObject:r,translate:(e,t)=>f(e.entry,{...t,$locale:d?c:l})})},[l,i,c,p,d,f])}function u(e,t){return e?`${e}.${t}`:t}exports.useTranslations=c;
|
|
1
|
+
require(`../_virtual/_rolldown/runtime.cjs`);const e=require(`./condition-store.cjs`),t=require(`./i18n-config.cjs`),n=require(`./utils.cjs`),r=require(`./useGT.cjs`),i=require(`./external-store/useTrackedDictionaryResolver.cjs`),a=require(`./external-store/useTrackedDictionaryObjResolver.cjs`);let o=require(`gt-i18n/internal`),s=require(`react`);function c(a){let c=e.useLocale(),d=t.useDefaultLocale(),f=n.useShouldTranslate(),p=r.useGT(),m=i.useTrackedDictionaryResolver(),h=l(a),g=(0,s.useCallback)((e,t={})=>{let n=u(a,e),r=m({locale:d,id:n});if(r===void 0)throw Error(`Dictionary entry ${n} cannot be found`);let i=(0,o.resolveDictionaryLookupOptions)(r.options);if(!f)return p(r.entry,{...i,...(0,o.extractVariables)(t),$locale:d});let s=m({locale:c,id:n});return s?.entry==null?p(r.entry,{...i,...(0,o.extractVariables)(t),$locale:c}):(0,o.renderDictionaryEntry)({sourceLocale:d,targetLocale:c,sourceEntry:r,target:s.entry,dictionaryOptions:i,options:t})},[d,p,a,c,m,f]);return(0,s.useMemo)(()=>Object.assign((e,t)=>g(e,t),{obj:h}),[g,h])}function l(i){let c=e.useLocale(),l=t.useDefaultLocale(),d=n.useShouldTranslate(),f=r.useGT(),p=a.useTrackedDictionaryObjResolver();return(0,s.useCallback)(e=>{let t=u(i,e),n=p({locale:l,id:t});if(n===void 0)throw Error(`Dictionary entry ${t} cannot be found`);let r;return d&&(r=p({locale:c,id:t})),(0,o.renderDictionaryObject)({sourceObject:n,targetObject:r,translate:(e,t)=>f(e.entry,{...t,$locale:d?c:l})})},[l,i,c,p,d,f])}function u(e,t){return e?`${e}.${t}`:t}exports.useTranslations=c;
|
|
2
2
|
//# sourceMappingURL=useTranslations.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTranslations.cjs","names":["useLocale","useDefaultLocale","useShouldTranslate","useGT","useTrackedDictionaryResolver","useTrackedDictionaryObjResolver"],"sources":["../../src/hooks/useTranslations.ts"],"sourcesContent":["import { useCallback, useMemo } from 'react';\nimport {\n extractVariables,\n renderDictionaryEntry,\n renderDictionaryObject,\n resolveDictionaryLookupOptions,\n} from 'gt-i18n/internal';\nimport { useLocale } from './condition-store';\nimport { useGT } from './useGT';\nimport type {\n DictionaryObjectTranslation,\n TranslationVariables,\n} from 'gt-i18n/types';\nimport { useDefaultLocale } from './i18n-config';\nimport { useShouldTranslate } from './utils';\nimport { useTrackedDictionaryResolver } from './external-store/useTrackedDictionaryResolver';\nimport { useTrackedDictionaryObjResolver } from './external-store/useTrackedDictionaryObjResolver';\n\n// ===== Hook ===== //\n\nexport function useTranslations(rootId?: string): UseTranslationsFunction {\n const locale = useLocale();\n const defaultLocale = useDefaultLocale();\n const shouldTranslate = useShouldTranslate();\n const gt = useGT();\n const resolveDictionaryEntry = useTrackedDictionaryResolver();\n const translateObject = useTranslationsObj(rootId);\n\n const translateEntry = useCallback(\n (suffix: string, options: TranslationVariables = {}) => {\n const id = getId(rootId, suffix);\n\n const sourceEntry = resolveDictionaryEntry({\n locale: defaultLocale,\n id,\n });\n if (sourceEntry === undefined) {\n throw new Error(`Dictionary entry ${id} cannot be found`);\n }\n const sourceOptions = resolveDictionaryLookupOptions(sourceEntry.options);\n if (!shouldTranslate) {\n return gt(sourceEntry.entry, {\n ...sourceOptions,\n ...extractVariables(options),\n $locale: defaultLocale,\n });\n }\n\n const targetEntry = resolveDictionaryEntry({ locale, id });\n if (targetEntry?.entry != null) {\n return renderDictionaryEntry({\n sourceLocale: defaultLocale,\n targetLocale: locale,\n sourceEntry,\n target: targetEntry.entry,\n dictionaryOptions: sourceOptions,\n options,\n });\n }\n\n return gt(sourceEntry.entry, {\n ...sourceOptions,\n ...extractVariables(options),\n $locale: locale,\n });\n },\n [defaultLocale, gt, rootId, locale, resolveDictionaryEntry, shouldTranslate]\n );\n\n return useMemo(\n ()
|
|
1
|
+
{"version":3,"file":"useTranslations.cjs","names":["useLocale","useDefaultLocale","useShouldTranslate","useGT","useTrackedDictionaryResolver","useTrackedDictionaryObjResolver"],"sources":["../../src/hooks/useTranslations.ts"],"sourcesContent":["import { useCallback, useMemo } from 'react';\nimport {\n extractVariables,\n renderDictionaryEntry,\n renderDictionaryObject,\n resolveDictionaryLookupOptions,\n} from 'gt-i18n/internal';\nimport { useLocale } from './condition-store';\nimport { useGT } from './useGT';\nimport type {\n DictionaryObjectTranslation,\n TranslationVariables,\n} from 'gt-i18n/types';\nimport { useDefaultLocale } from './i18n-config';\nimport { useShouldTranslate } from './utils';\nimport { useTrackedDictionaryResolver } from './external-store/useTrackedDictionaryResolver';\nimport { useTrackedDictionaryObjResolver } from './external-store/useTrackedDictionaryObjResolver';\n\n// ===== Hook ===== //\n\nexport function useTranslations(rootId?: string): UseTranslationsFunction {\n const locale = useLocale();\n const defaultLocale = useDefaultLocale();\n const shouldTranslate = useShouldTranslate();\n const gt = useGT();\n const resolveDictionaryEntry = useTrackedDictionaryResolver();\n const translateObject = useTranslationsObj(rootId);\n\n const translateEntry = useCallback(\n (suffix: string, options: TranslationVariables = {}) => {\n const id = getId(rootId, suffix);\n\n const sourceEntry = resolveDictionaryEntry({\n locale: defaultLocale,\n id,\n });\n if (sourceEntry === undefined) {\n throw new Error(`Dictionary entry ${id} cannot be found`);\n }\n const sourceOptions = resolveDictionaryLookupOptions(sourceEntry.options);\n if (!shouldTranslate) {\n return gt(sourceEntry.entry, {\n ...sourceOptions,\n ...extractVariables(options),\n $locale: defaultLocale,\n });\n }\n\n const targetEntry = resolveDictionaryEntry({ locale, id });\n if (targetEntry?.entry != null) {\n return renderDictionaryEntry({\n sourceLocale: defaultLocale,\n targetLocale: locale,\n sourceEntry,\n target: targetEntry.entry,\n dictionaryOptions: sourceOptions,\n options,\n });\n }\n\n return gt(sourceEntry.entry, {\n ...sourceOptions,\n ...extractVariables(options),\n $locale: locale,\n });\n },\n [defaultLocale, gt, rootId, locale, resolveDictionaryEntry, shouldTranslate]\n );\n\n // Wrap instead of mutating translateEntry, so the returned function changes\n // identity when either translateEntry or translateObject changes\n return useMemo(\n () =>\n Object.assign(\n (suffix: string, options?: TranslationVariables) =>\n translateEntry(suffix, options),\n { obj: translateObject }\n ),\n [translateEntry, translateObject]\n );\n}\n\nfunction useTranslationsObj(rootId?: string): UseTranslationsObjFunction {\n const locale = useLocale();\n const defaultLocale = useDefaultLocale();\n const shouldTranslate = useShouldTranslate();\n const gt = useGT();\n const resolveDictionaryObject = useTrackedDictionaryObjResolver();\n\n return useCallback(\n (suffix: string) => {\n const entryId = getId(rootId, suffix);\n const sourceObject = resolveDictionaryObject({\n locale: defaultLocale,\n id: entryId,\n });\n if (sourceObject === undefined) {\n throw new Error(`Dictionary entry ${entryId} cannot be found`);\n }\n\n let targetObject = undefined;\n if (shouldTranslate) {\n const targetLookup = { locale, id: entryId };\n targetObject = resolveDictionaryObject(targetLookup);\n }\n\n return renderDictionaryObject({\n sourceObject,\n targetObject,\n translate: (sourceEntry, dictionaryOptions) =>\n gt(sourceEntry.entry, {\n ...dictionaryOptions,\n $locale: shouldTranslate ? locale : defaultLocale,\n }),\n });\n },\n [\n defaultLocale,\n rootId,\n locale,\n resolveDictionaryObject,\n shouldTranslate,\n gt,\n ]\n );\n}\n\n// ===== Lookup Helpers ===== //\n\nfunction getId(prefix: string | undefined, suffix: string): string {\n return prefix ? `${prefix}.${suffix}` : suffix;\n}\n\n// ===== Types ===== //\n\nexport type UseTranslationsFunction = ((\n id: string,\n options?: TranslationVariables\n) => string) & {\n obj: (id: string) => DictionaryObjectTranslation;\n};\n\ntype UseTranslationsObjFunction = (id: string) => DictionaryObjectTranslation;\n"],"mappings":"6VAoBA,SAAgB,EAAgB,EAA0C,CACxE,IAAM,EAASA,EAAAA,WAAW,CACpB,EAAgBC,EAAAA,kBAAkB,CAClC,EAAkBC,EAAAA,oBAAoB,CACtC,EAAKC,EAAAA,OAAO,CACZ,EAAyBC,EAAAA,8BAA8B,CACvD,EAAkB,EAAmB,EAAO,CAE5C,GAAA,EAAA,EAAA,cACH,EAAgB,EAAgC,EAAE,GAAK,CACtD,IAAM,EAAK,EAAM,EAAQ,EAAO,CAE1B,EAAc,EAAuB,CACzC,OAAQ,EACR,KACD,CAAC,CACF,GAAI,IAAgB,IAAA,GAClB,MAAU,MAAM,oBAAoB,EAAG,kBAAkB,CAE3D,IAAM,GAAA,EAAA,EAAA,gCAA+C,EAAY,QAAQ,CACzE,GAAI,CAAC,EACH,OAAO,EAAG,EAAY,MAAO,CAC3B,GAAG,EACH,IAAA,EAAA,EAAA,kBAAoB,EAAQ,CAC5B,QAAS,EACV,CAAC,CAGJ,IAAM,EAAc,EAAuB,CAAE,SAAQ,KAAI,CAAC,CAY1D,OAXI,GAAa,OAAS,KAWnB,EAAG,EAAY,MAAO,CAC3B,GAAG,EACH,IAAA,EAAA,EAAA,kBAAoB,EAAQ,CAC5B,QAAS,EACV,CAAC,EAdA,EAAA,EAAA,uBAA6B,CAC3B,aAAc,EACd,aAAc,EACd,cACA,OAAQ,EAAY,MACpB,kBAAmB,EACnB,UACD,CAAC,EASN,CAAC,EAAe,EAAI,EAAQ,EAAQ,EAAwB,EAAgB,CAC7E,CAID,OAAA,EAAA,EAAA,aAEI,OAAO,QACJ,EAAgB,IACf,EAAe,EAAQ,EAAQ,CACjC,CAAE,IAAK,EAAiB,CACzB,CACH,CAAC,EAAgB,EAAgB,CAClC,CAGH,SAAS,EAAmB,EAA6C,CACvE,IAAM,EAASJ,EAAAA,WAAW,CACpB,EAAgBC,EAAAA,kBAAkB,CAClC,EAAkBC,EAAAA,oBAAoB,CACtC,EAAKC,EAAAA,OAAO,CACZ,EAA0BE,EAAAA,iCAAiC,CAEjE,OAAA,EAAA,EAAA,aACG,GAAmB,CAClB,IAAM,EAAU,EAAM,EAAQ,EAAO,CAC/B,EAAe,EAAwB,CAC3C,OAAQ,EACR,GAAI,EACL,CAAC,CACF,GAAI,IAAiB,IAAA,GACnB,MAAU,MAAM,oBAAoB,EAAQ,kBAAkB,CAGhE,IAAI,EAMJ,OALI,IAEF,EAAe,EAAwB,CADhB,SAAQ,GAAI,EACgB,CAAC,GAGtD,EAAA,EAAA,wBAA8B,CAC5B,eACA,eACA,WAAY,EAAa,IACvB,EAAG,EAAY,MAAO,CACpB,GAAG,EACH,QAAS,EAAkB,EAAS,EACrC,CAAC,CACL,CAAC,EAEJ,CACE,EACA,EACA,EACA,EACA,EACA,EACD,CACF,CAKH,SAAS,EAAM,EAA4B,EAAwB,CACjE,OAAO,EAAS,GAAG,EAAO,GAAG,IAAW"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTranslations.d.ts","names":[],"sources":["../../src/hooks/useTranslations.ts"],"mappings":";;;iBAoBgB,eAAA,CAAgB,MAAA,YAAkB,uBAAA;AAAA,
|
|
1
|
+
{"version":3,"file":"useTranslations.d.ts","names":[],"sources":["../../src/hooks/useTranslations.ts"],"mappings":";;;iBAoBgB,eAAA,CAAgB,MAAA,YAAkB,uBAAA;AAAA,KAmHtC,uBAAA,KACV,EAAA,UACA,OAAA,GAAU,oBAAA;EAEV,GAAA,GAAM,EAAA,aAAe,2BAAA;AAAA"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{useLocale as e}from"./condition-store.mjs";import{useDefaultLocale as t}from"./i18n-config.mjs";import{useShouldTranslate as n}from"./utils.mjs";import{useGT as r}from"./useGT.mjs";import{useTrackedDictionaryResolver as i}from"./external-store/useTrackedDictionaryResolver.mjs";import{useTrackedDictionaryObjResolver as a}from"./external-store/useTrackedDictionaryObjResolver.mjs";import{extractVariables as o,renderDictionaryEntry as s,renderDictionaryObject as c,resolveDictionaryLookupOptions as l}from"gt-i18n/internal";import{useCallback as u,useMemo as d}from"react";function f(a){let c=e(),f=t(),h=n(),g=r(),_=i(),v=p(a),y=u((e,t={})=>{let n=m(a,e),r=_({locale:f,id:n});if(r===void 0)throw Error(`Dictionary entry ${n} cannot be found`);let i=l(r.options);if(!h)return g(r.entry,{...i,...o(t),$locale:f});let u=_({locale:c,id:n});return u?.entry==null?g(r.entry,{...i,...o(t),$locale:c}):s({sourceLocale:f,targetLocale:c,sourceEntry:r,target:u.entry,dictionaryOptions:i,options:t})},[f,g,a,c,_,h]);return d(()=>Object.assign(y,{obj:v}),[y,v])}function p(i){let o=e(),s=t(),l=n(),d=r(),f=a();return u(e=>{let t=m(i,e),n=f({locale:s,id:t});if(n===void 0)throw Error(`Dictionary entry ${t} cannot be found`);let r;return l&&(r=f({locale:o,id:t})),c({sourceObject:n,targetObject:r,translate:(e,t)=>d(e.entry,{...t,$locale:l?o:s})})},[s,i,o,f,l,d])}function m(e,t){return e?`${e}.${t}`:t}export{f as useTranslations};
|
|
1
|
+
import{useLocale as e}from"./condition-store.mjs";import{useDefaultLocale as t}from"./i18n-config.mjs";import{useShouldTranslate as n}from"./utils.mjs";import{useGT as r}from"./useGT.mjs";import{useTrackedDictionaryResolver as i}from"./external-store/useTrackedDictionaryResolver.mjs";import{useTrackedDictionaryObjResolver as a}from"./external-store/useTrackedDictionaryObjResolver.mjs";import{extractVariables as o,renderDictionaryEntry as s,renderDictionaryObject as c,resolveDictionaryLookupOptions as l}from"gt-i18n/internal";import{useCallback as u,useMemo as d}from"react";function f(a){let c=e(),f=t(),h=n(),g=r(),_=i(),v=p(a),y=u((e,t={})=>{let n=m(a,e),r=_({locale:f,id:n});if(r===void 0)throw Error(`Dictionary entry ${n} cannot be found`);let i=l(r.options);if(!h)return g(r.entry,{...i,...o(t),$locale:f});let u=_({locale:c,id:n});return u?.entry==null?g(r.entry,{...i,...o(t),$locale:c}):s({sourceLocale:f,targetLocale:c,sourceEntry:r,target:u.entry,dictionaryOptions:i,options:t})},[f,g,a,c,_,h]);return d(()=>Object.assign((e,t)=>y(e,t),{obj:v}),[y,v])}function p(i){let o=e(),s=t(),l=n(),d=r(),f=a();return u(e=>{let t=m(i,e),n=f({locale:s,id:t});if(n===void 0)throw Error(`Dictionary entry ${t} cannot be found`);let r;return l&&(r=f({locale:o,id:t})),c({sourceObject:n,targetObject:r,translate:(e,t)=>d(e.entry,{...t,$locale:l?o:s})})},[s,i,o,f,l,d])}function m(e,t){return e?`${e}.${t}`:t}export{f as useTranslations};
|
|
2
2
|
//# sourceMappingURL=useTranslations.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTranslations.mjs","names":[],"sources":["../../src/hooks/useTranslations.ts"],"sourcesContent":["import { useCallback, useMemo } from 'react';\nimport {\n extractVariables,\n renderDictionaryEntry,\n renderDictionaryObject,\n resolveDictionaryLookupOptions,\n} from 'gt-i18n/internal';\nimport { useLocale } from './condition-store';\nimport { useGT } from './useGT';\nimport type {\n DictionaryObjectTranslation,\n TranslationVariables,\n} from 'gt-i18n/types';\nimport { useDefaultLocale } from './i18n-config';\nimport { useShouldTranslate } from './utils';\nimport { useTrackedDictionaryResolver } from './external-store/useTrackedDictionaryResolver';\nimport { useTrackedDictionaryObjResolver } from './external-store/useTrackedDictionaryObjResolver';\n\n// ===== Hook ===== //\n\nexport function useTranslations(rootId?: string): UseTranslationsFunction {\n const locale = useLocale();\n const defaultLocale = useDefaultLocale();\n const shouldTranslate = useShouldTranslate();\n const gt = useGT();\n const resolveDictionaryEntry = useTrackedDictionaryResolver();\n const translateObject = useTranslationsObj(rootId);\n\n const translateEntry = useCallback(\n (suffix: string, options: TranslationVariables = {}) => {\n const id = getId(rootId, suffix);\n\n const sourceEntry = resolveDictionaryEntry({\n locale: defaultLocale,\n id,\n });\n if (sourceEntry === undefined) {\n throw new Error(`Dictionary entry ${id} cannot be found`);\n }\n const sourceOptions = resolveDictionaryLookupOptions(sourceEntry.options);\n if (!shouldTranslate) {\n return gt(sourceEntry.entry, {\n ...sourceOptions,\n ...extractVariables(options),\n $locale: defaultLocale,\n });\n }\n\n const targetEntry = resolveDictionaryEntry({ locale, id });\n if (targetEntry?.entry != null) {\n return renderDictionaryEntry({\n sourceLocale: defaultLocale,\n targetLocale: locale,\n sourceEntry,\n target: targetEntry.entry,\n dictionaryOptions: sourceOptions,\n options,\n });\n }\n\n return gt(sourceEntry.entry, {\n ...sourceOptions,\n ...extractVariables(options),\n $locale: locale,\n });\n },\n [defaultLocale, gt, rootId, locale, resolveDictionaryEntry, shouldTranslate]\n );\n\n return useMemo(\n ()
|
|
1
|
+
{"version":3,"file":"useTranslations.mjs","names":[],"sources":["../../src/hooks/useTranslations.ts"],"sourcesContent":["import { useCallback, useMemo } from 'react';\nimport {\n extractVariables,\n renderDictionaryEntry,\n renderDictionaryObject,\n resolveDictionaryLookupOptions,\n} from 'gt-i18n/internal';\nimport { useLocale } from './condition-store';\nimport { useGT } from './useGT';\nimport type {\n DictionaryObjectTranslation,\n TranslationVariables,\n} from 'gt-i18n/types';\nimport { useDefaultLocale } from './i18n-config';\nimport { useShouldTranslate } from './utils';\nimport { useTrackedDictionaryResolver } from './external-store/useTrackedDictionaryResolver';\nimport { useTrackedDictionaryObjResolver } from './external-store/useTrackedDictionaryObjResolver';\n\n// ===== Hook ===== //\n\nexport function useTranslations(rootId?: string): UseTranslationsFunction {\n const locale = useLocale();\n const defaultLocale = useDefaultLocale();\n const shouldTranslate = useShouldTranslate();\n const gt = useGT();\n const resolveDictionaryEntry = useTrackedDictionaryResolver();\n const translateObject = useTranslationsObj(rootId);\n\n const translateEntry = useCallback(\n (suffix: string, options: TranslationVariables = {}) => {\n const id = getId(rootId, suffix);\n\n const sourceEntry = resolveDictionaryEntry({\n locale: defaultLocale,\n id,\n });\n if (sourceEntry === undefined) {\n throw new Error(`Dictionary entry ${id} cannot be found`);\n }\n const sourceOptions = resolveDictionaryLookupOptions(sourceEntry.options);\n if (!shouldTranslate) {\n return gt(sourceEntry.entry, {\n ...sourceOptions,\n ...extractVariables(options),\n $locale: defaultLocale,\n });\n }\n\n const targetEntry = resolveDictionaryEntry({ locale, id });\n if (targetEntry?.entry != null) {\n return renderDictionaryEntry({\n sourceLocale: defaultLocale,\n targetLocale: locale,\n sourceEntry,\n target: targetEntry.entry,\n dictionaryOptions: sourceOptions,\n options,\n });\n }\n\n return gt(sourceEntry.entry, {\n ...sourceOptions,\n ...extractVariables(options),\n $locale: locale,\n });\n },\n [defaultLocale, gt, rootId, locale, resolveDictionaryEntry, shouldTranslate]\n );\n\n // Wrap instead of mutating translateEntry, so the returned function changes\n // identity when either translateEntry or translateObject changes\n return useMemo(\n () =>\n Object.assign(\n (suffix: string, options?: TranslationVariables) =>\n translateEntry(suffix, options),\n { obj: translateObject }\n ),\n [translateEntry, translateObject]\n );\n}\n\nfunction useTranslationsObj(rootId?: string): UseTranslationsObjFunction {\n const locale = useLocale();\n const defaultLocale = useDefaultLocale();\n const shouldTranslate = useShouldTranslate();\n const gt = useGT();\n const resolveDictionaryObject = useTrackedDictionaryObjResolver();\n\n return useCallback(\n (suffix: string) => {\n const entryId = getId(rootId, suffix);\n const sourceObject = resolveDictionaryObject({\n locale: defaultLocale,\n id: entryId,\n });\n if (sourceObject === undefined) {\n throw new Error(`Dictionary entry ${entryId} cannot be found`);\n }\n\n let targetObject = undefined;\n if (shouldTranslate) {\n const targetLookup = { locale, id: entryId };\n targetObject = resolveDictionaryObject(targetLookup);\n }\n\n return renderDictionaryObject({\n sourceObject,\n targetObject,\n translate: (sourceEntry, dictionaryOptions) =>\n gt(sourceEntry.entry, {\n ...dictionaryOptions,\n $locale: shouldTranslate ? locale : defaultLocale,\n }),\n });\n },\n [\n defaultLocale,\n rootId,\n locale,\n resolveDictionaryObject,\n shouldTranslate,\n gt,\n ]\n );\n}\n\n// ===== Lookup Helpers ===== //\n\nfunction getId(prefix: string | undefined, suffix: string): string {\n return prefix ? `${prefix}.${suffix}` : suffix;\n}\n\n// ===== Types ===== //\n\nexport type UseTranslationsFunction = ((\n id: string,\n options?: TranslationVariables\n) => string) & {\n obj: (id: string) => DictionaryObjectTranslation;\n};\n\ntype UseTranslationsObjFunction = (id: string) => DictionaryObjectTranslation;\n"],"mappings":"okBAoBA,SAAgB,EAAgB,EAA0C,CACxE,IAAM,EAAS,GAAW,CACpB,EAAgB,GAAkB,CAClC,EAAkB,GAAoB,CACtC,EAAK,GAAO,CACZ,EAAyB,GAA8B,CACvD,EAAkB,EAAmB,EAAO,CAE5C,EAAiB,GACpB,EAAgB,EAAgC,EAAE,GAAK,CACtD,IAAM,EAAK,EAAM,EAAQ,EAAO,CAE1B,EAAc,EAAuB,CACzC,OAAQ,EACR,KACD,CAAC,CACF,GAAI,IAAgB,IAAA,GAClB,MAAU,MAAM,oBAAoB,EAAG,kBAAkB,CAE3D,IAAM,EAAgB,EAA+B,EAAY,QAAQ,CACzE,GAAI,CAAC,EACH,OAAO,EAAG,EAAY,MAAO,CAC3B,GAAG,EACH,GAAG,EAAiB,EAAQ,CAC5B,QAAS,EACV,CAAC,CAGJ,IAAM,EAAc,EAAuB,CAAE,SAAQ,KAAI,CAAC,CAY1D,OAXI,GAAa,OAAS,KAWnB,EAAG,EAAY,MAAO,CAC3B,GAAG,EACH,GAAG,EAAiB,EAAQ,CAC5B,QAAS,EACV,CAAC,CAdO,EAAsB,CAC3B,aAAc,EACd,aAAc,EACd,cACA,OAAQ,EAAY,MACpB,kBAAmB,EACnB,UACD,CAAC,EASN,CAAC,EAAe,EAAI,EAAQ,EAAQ,EAAwB,EAAgB,CAC7E,CAID,OAAO,MAEH,OAAO,QACJ,EAAgB,IACf,EAAe,EAAQ,EAAQ,CACjC,CAAE,IAAK,EAAiB,CACzB,CACH,CAAC,EAAgB,EAAgB,CAClC,CAGH,SAAS,EAAmB,EAA6C,CACvE,IAAM,EAAS,GAAW,CACpB,EAAgB,GAAkB,CAClC,EAAkB,GAAoB,CACtC,EAAK,GAAO,CACZ,EAA0B,GAAiC,CAEjE,OAAO,EACJ,GAAmB,CAClB,IAAM,EAAU,EAAM,EAAQ,EAAO,CAC/B,EAAe,EAAwB,CAC3C,OAAQ,EACR,GAAI,EACL,CAAC,CACF,GAAI,IAAiB,IAAA,GACnB,MAAU,MAAM,oBAAoB,EAAQ,kBAAkB,CAGhE,IAAI,EAMJ,OALI,IAEF,EAAe,EAAwB,CADhB,SAAQ,GAAI,EACgB,CAAC,EAG/C,EAAuB,CAC5B,eACA,eACA,WAAY,EAAa,IACvB,EAAG,EAAY,MAAO,CACpB,GAAG,EACH,QAAS,EAAkB,EAAS,EACrC,CAAC,CACL,CAAC,EAEJ,CACE,EACA,EACA,EACA,EACA,EACA,EACD,CACF,CAKH,SAAS,EAAM,EAA4B,EAAwB,CACjE,OAAO,EAAS,GAAG,EAAO,GAAG,IAAW"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
require(`../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../context/context.cjs`),t=require(`../utils.cjs`),n=require(`../../i18n-store/useI18nStore.cjs`);let r=require(`gt-i18n/internal`),i=require(`react`);const a=()=>{},o=()=>{},s=()=>{};function c(){return a}function l(){return u(t.useShouldTranslate())}function u(t){let n=e.useGTContext()?.onMissingTranslation,a=y(t);return(0,i.useCallback)(e=>{n?n(e):a((0,r.getTranslateListenerKey)(e),{type:`translation`,lookup:e})},[n,a])}function d(){return o}function f(){let n=e.useGTContext()?.onMissingDictionaryEntry,a=y(t.useShouldTranslate());return(0,i.useCallback)(e=>{n?n(e):a((0,r.getDictionaryListenerKey)(e),{type:`dictionaryEntry`,lookup:e})},[n,a])}function p(){return s}function m(){let n=e.useGTContext()?.onMissingDictionaryObj,a=y(t.useShouldTranslate());return(0,i.useCallback)(e=>{n?n(e):a((0,r.getDictionaryListenerKey)(e),{type:`dictionaryObject`,lookup:e})},[n,a])}const h=process.env.NODE_ENV===`production`?c:l,g=process.env.NODE_ENV===`production`?c:u,_=process.env.NODE_ENV===`production`?d:f,v=process.env.NODE_ENV===`production`?p:m;function y(e){let t=process.env.NODE_ENV!==`production`&&(0,r.getI18nConfig)().isDevHotReloadEnabled(),a=n.useI18nStore(),o=new Map
|
|
1
|
+
require(`../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../context/context.cjs`),t=require(`../utils.cjs`),n=require(`../../i18n-store/useI18nStore.cjs`);let r=require(`gt-i18n/internal`),i=require(`react`);const a=()=>{},o=()=>{},s=()=>{};function c(){return a}function l(){return u(t.useShouldTranslate())}function u(t){let n=e.useGTContext()?.onMissingTranslation,a=y(t);return(0,i.useCallback)(e=>{n?n(e):a((0,r.getTranslateListenerKey)(e),{type:`translation`,lookup:e})},[n,a])}function d(){return o}function f(){let n=e.useGTContext()?.onMissingDictionaryEntry,a=y(t.useShouldTranslate());return(0,i.useCallback)(e=>{n?n(e):a((0,r.getDictionaryListenerKey)(e),{type:`dictionaryEntry`,lookup:e})},[n,a])}function p(){return s}function m(){let n=e.useGTContext()?.onMissingDictionaryObj,a=y(t.useShouldTranslate());return(0,i.useCallback)(e=>{n?n(e):a((0,r.getDictionaryListenerKey)(e),{type:`dictionaryObject`,lookup:e})},[n,a])}const h=process.env.NODE_ENV===`production`?c:l,g=process.env.NODE_ENV===`production`?c:u,_=process.env.NODE_ENV===`production`?d:f,v=process.env.NODE_ENV===`production`?p:m;function y(e){let t=process.env.NODE_ENV!==`production`&&(0,r.getI18nConfig)().isDevHotReloadEnabled(),a=n.useI18nStore(),o=(0,i.useRef)(null);return o.current??=new Map,(0,i.useEffect)(()=>{let n=o.current;if(n.size===0)return;let r=Array.from(n.values());n.clear(),!(!t||!e)&&r.forEach(({type:e,lookup:t})=>{switch(e){case`translation`:a.translate(t);break;case`dictionaryEntry`:a.translateDictionaryEntry(t);break;case`dictionaryObject`:a.translateDictionaryObject(t);break}})}),(0,i.useCallback)((e,t)=>{o.current.set(e,t)},[])}exports.useHandleMissingDictionaryEntry=_,exports.useHandleMissingDictionaryObject=v,exports.useHandleMissingTranslation=h,exports.useHandleMissingTranslationWithConditions=g;
|
|
2
2
|
//# sourceMappingURL=missing-translation.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"missing-translation.cjs","names":["useShouldTranslate","useGTContext","useI18nStore"],"sources":["../../../src/hooks/utils/missing-translation.ts"],"sourcesContent":["import {\n getDictionaryListenerKey,\n getI18nConfig,\n getTranslateListenerKey,\n} from 'gt-i18n/internal';\nimport { useShouldTranslate } from '../utils';\nimport { useI18nStore } from '../../i18n-store/useI18nStore';\nimport { DictionaryLookup, TranslateLookup } from '../../i18n-store/storeTypes';\nimport { useCallback, useEffect } from 'react';\nimport { Translation } from 'gt-i18n/types';\nimport { useGTContext } from '../../context/context';\n\nexport type OnMissingTranslation = <T extends Translation>(\n lookup: TranslateLookup<T>\n) => void;\nexport type OnMissingDictionaryEntry = (lookup: DictionaryLookup) => void;\n// TODO: rename to OnMissingDictionaryObject\nexport type OnMissingDictionaryObj = (lookup: DictionaryLookup) => void;\n\ntype PendingLookup =\n | {\n type: 'translation';\n lookup: TranslateLookup;\n }\n | {\n type: 'dictionaryEntry' | 'dictionaryObject';\n lookup: DictionaryLookup;\n };\n\n/**\n * Why have custom handleMissing functions?\n *\n * Some runtimes (like server) cannot make useEffect calls, so\n * we need to give them access to a callback where they can embed\n * their own translation calls\n *\n * While this is technnically not pure behavior, this is acceptable\n * in development hot reload\n */\n\nconst noopOnMissingTranslation: OnMissingTranslation = () => {};\nconst noopOnMissingDictionaryEntry: OnMissingDictionaryEntry = () => {};\nconst noopOnMissingDictionaryObj: OnMissingDictionaryObj = () => {};\n\n// TODO: reduce code duplication with the three below functions\nfunction useHandleMissingTranslationProd(): OnMissingTranslation {\n return noopOnMissingTranslation;\n}\n\nfunction useHandleMissingTranslationDev(): OnMissingTranslation {\n return useHandleMissingTranslationWithConditionsDev(useShouldTranslate());\n}\n\nfunction useHandleMissingTranslationWithConditionsDev(\n shouldTranslate: boolean\n): OnMissingTranslation {\n const customHandleMissing = useGTContext()?.onMissingTranslation;\n const pureHandleMissing = useDevHotReloadQueue(shouldTranslate);\n\n return useCallback(\n (lookup: TranslateLookup) => {\n if (customHandleMissing) {\n customHandleMissing(lookup);\n } else {\n pureHandleMissing(getTranslateListenerKey(lookup), {\n type: 'translation',\n lookup,\n });\n }\n },\n [customHandleMissing, pureHandleMissing]\n );\n}\n\nfunction useHandleMissingDictionaryEntryProd(): OnMissingDictionaryEntry {\n return noopOnMissingDictionaryEntry;\n}\n\nfunction useHandleMissingDictionaryEntryDev(): OnMissingDictionaryEntry {\n const customHandleMissing = useGTContext()?.onMissingDictionaryEntry;\n const pureHandleMissing = useDevHotReloadQueue(useShouldTranslate());\n\n return useCallback(\n (lookup: DictionaryLookup) => {\n if (customHandleMissing) {\n customHandleMissing(lookup);\n } else {\n pureHandleMissing(getDictionaryListenerKey(lookup), {\n type: 'dictionaryEntry',\n lookup,\n });\n }\n },\n [customHandleMissing, pureHandleMissing]\n );\n}\n\nfunction useHandleMissingDictionaryObjectProd(): OnMissingDictionaryObj {\n return noopOnMissingDictionaryObj;\n}\n\nfunction useHandleMissingDictionaryObjectDev(): OnMissingDictionaryObj {\n const customHandleMissing = useGTContext()?.onMissingDictionaryObj;\n const pureHandleMissing = useDevHotReloadQueue(useShouldTranslate());\n return useCallback(\n (lookup: DictionaryLookup) => {\n if (customHandleMissing) {\n customHandleMissing(lookup);\n } else {\n pureHandleMissing(getDictionaryListenerKey(lookup), {\n type: 'dictionaryObject',\n lookup,\n });\n }\n },\n [customHandleMissing, pureHandleMissing]\n );\n}\n\nexport const useHandleMissingTranslation: () => OnMissingTranslation =\n process.env.NODE_ENV === 'production'\n ? useHandleMissingTranslationProd\n : useHandleMissingTranslationDev;\n\nexport const useHandleMissingTranslationWithConditions: (\n shouldTranslate: boolean\n) => OnMissingTranslation =\n process.env.NODE_ENV === 'production'\n ? useHandleMissingTranslationProd\n : useHandleMissingTranslationWithConditionsDev;\n\nexport const useHandleMissingDictionaryEntry: () => OnMissingDictionaryEntry =\n process.env.NODE_ENV === 'production'\n ? useHandleMissingDictionaryEntryProd\n : useHandleMissingDictionaryEntryDev;\n\nexport const useHandleMissingDictionaryObject: () => OnMissingDictionaryObj =\n process.env.NODE_ENV === 'production'\n ? useHandleMissingDictionaryObjectProd\n : useHandleMissingDictionaryObjectDev;\n\n/**\n * HMR translation needs to be deferred to post-commit phase\n */\nfunction useDevHotReloadQueue(shouldTranslate: boolean) {\n // Statically gated so bundlers can drop dev hot-reload work from\n // production builds.\n const devHotReloadEnabled =\n process.env.NODE_ENV !== 'production' &&\n getI18nConfig().isDevHotReloadEnabled();\n const i18nStore = useI18nStore();\n\n //
|
|
1
|
+
{"version":3,"file":"missing-translation.cjs","names":["useShouldTranslate","useGTContext","useI18nStore"],"sources":["../../../src/hooks/utils/missing-translation.ts"],"sourcesContent":["import {\n getDictionaryListenerKey,\n getI18nConfig,\n getTranslateListenerKey,\n} from 'gt-i18n/internal';\nimport { useShouldTranslate } from '../utils';\nimport { useI18nStore } from '../../i18n-store/useI18nStore';\nimport { DictionaryLookup, TranslateLookup } from '../../i18n-store/storeTypes';\nimport { useCallback, useEffect, useRef } from 'react';\nimport { Translation } from 'gt-i18n/types';\nimport { useGTContext } from '../../context/context';\n\nexport type OnMissingTranslation = <T extends Translation>(\n lookup: TranslateLookup<T>\n) => void;\nexport type OnMissingDictionaryEntry = (lookup: DictionaryLookup) => void;\n// TODO: rename to OnMissingDictionaryObject\nexport type OnMissingDictionaryObj = (lookup: DictionaryLookup) => void;\n\ntype PendingLookup =\n | {\n type: 'translation';\n lookup: TranslateLookup;\n }\n | {\n type: 'dictionaryEntry' | 'dictionaryObject';\n lookup: DictionaryLookup;\n };\n\n/**\n * Why have custom handleMissing functions?\n *\n * Some runtimes (like server) cannot make useEffect calls, so\n * we need to give them access to a callback where they can embed\n * their own translation calls\n *\n * While this is technnically not pure behavior, this is acceptable\n * in development hot reload\n */\n\nconst noopOnMissingTranslation: OnMissingTranslation = () => {};\nconst noopOnMissingDictionaryEntry: OnMissingDictionaryEntry = () => {};\nconst noopOnMissingDictionaryObj: OnMissingDictionaryObj = () => {};\n\n// TODO: reduce code duplication with the three below functions\nfunction useHandleMissingTranslationProd(): OnMissingTranslation {\n return noopOnMissingTranslation;\n}\n\nfunction useHandleMissingTranslationDev(): OnMissingTranslation {\n return useHandleMissingTranslationWithConditionsDev(useShouldTranslate());\n}\n\nfunction useHandleMissingTranslationWithConditionsDev(\n shouldTranslate: boolean\n): OnMissingTranslation {\n const customHandleMissing = useGTContext()?.onMissingTranslation;\n const pureHandleMissing = useDevHotReloadQueue(shouldTranslate);\n\n return useCallback(\n (lookup: TranslateLookup) => {\n if (customHandleMissing) {\n customHandleMissing(lookup);\n } else {\n pureHandleMissing(getTranslateListenerKey(lookup), {\n type: 'translation',\n lookup,\n });\n }\n },\n [customHandleMissing, pureHandleMissing]\n );\n}\n\nfunction useHandleMissingDictionaryEntryProd(): OnMissingDictionaryEntry {\n return noopOnMissingDictionaryEntry;\n}\n\nfunction useHandleMissingDictionaryEntryDev(): OnMissingDictionaryEntry {\n const customHandleMissing = useGTContext()?.onMissingDictionaryEntry;\n const pureHandleMissing = useDevHotReloadQueue(useShouldTranslate());\n\n return useCallback(\n (lookup: DictionaryLookup) => {\n if (customHandleMissing) {\n customHandleMissing(lookup);\n } else {\n pureHandleMissing(getDictionaryListenerKey(lookup), {\n type: 'dictionaryEntry',\n lookup,\n });\n }\n },\n [customHandleMissing, pureHandleMissing]\n );\n}\n\nfunction useHandleMissingDictionaryObjectProd(): OnMissingDictionaryObj {\n return noopOnMissingDictionaryObj;\n}\n\nfunction useHandleMissingDictionaryObjectDev(): OnMissingDictionaryObj {\n const customHandleMissing = useGTContext()?.onMissingDictionaryObj;\n const pureHandleMissing = useDevHotReloadQueue(useShouldTranslate());\n return useCallback(\n (lookup: DictionaryLookup) => {\n if (customHandleMissing) {\n customHandleMissing(lookup);\n } else {\n pureHandleMissing(getDictionaryListenerKey(lookup), {\n type: 'dictionaryObject',\n lookup,\n });\n }\n },\n [customHandleMissing, pureHandleMissing]\n );\n}\n\nexport const useHandleMissingTranslation: () => OnMissingTranslation =\n process.env.NODE_ENV === 'production'\n ? useHandleMissingTranslationProd\n : useHandleMissingTranslationDev;\n\nexport const useHandleMissingTranslationWithConditions: (\n shouldTranslate: boolean\n) => OnMissingTranslation =\n process.env.NODE_ENV === 'production'\n ? useHandleMissingTranslationProd\n : useHandleMissingTranslationWithConditionsDev;\n\nexport const useHandleMissingDictionaryEntry: () => OnMissingDictionaryEntry =\n process.env.NODE_ENV === 'production'\n ? useHandleMissingDictionaryEntryProd\n : useHandleMissingDictionaryEntryDev;\n\nexport const useHandleMissingDictionaryObject: () => OnMissingDictionaryObj =\n process.env.NODE_ENV === 'production'\n ? useHandleMissingDictionaryObjectProd\n : useHandleMissingDictionaryObjectDev;\n\n/**\n * HMR translation needs to be deferred to post-commit phase\n */\nfunction useDevHotReloadQueue(shouldTranslate: boolean) {\n // Statically gated so bundlers can drop dev hot-reload work from\n // production builds.\n const devHotReloadEnabled =\n process.env.NODE_ENV !== 'production' &&\n getI18nConfig().isDevHotReloadEnabled();\n const i18nStore = useI18nStore();\n\n // Persist the queue across renders so the enqueue callback keeps a stable\n // identity. An unstable callback would change the identity of every\n // translation function built on top of it (e.g. gt() from useGT()), which\n // re-runs user effects that depend on it after every render.\n const pendingLookupsRef = useRef<Map<string, PendingLookup> | null>(null);\n if (pendingLookupsRef.current == null) {\n pendingLookupsRef.current = new Map();\n }\n\n // Pure react: effects run after render\n // No dependency array: flush whatever was enqueued after every commit\n useEffect(() => {\n const pendingLookups = pendingLookupsRef.current!;\n if (pendingLookups.size === 0) return;\n const lookups = Array.from(pendingLookups.values());\n pendingLookups.clear();\n if (!devHotReloadEnabled || !shouldTranslate) return;\n\n lookups.forEach(({ type, lookup }) => {\n switch (type) {\n case 'translation':\n i18nStore.translate(lookup);\n break;\n case 'dictionaryEntry':\n i18nStore.translateDictionaryEntry(lookup);\n break;\n case 'dictionaryObject':\n i18nStore.translateDictionaryObject(lookup);\n break;\n }\n });\n });\n\n return useCallback((key: string, pendingLookup: PendingLookup) => {\n pendingLookupsRef.current!.set(key, pendingLookup);\n }, []);\n}\n"],"mappings":"2NAwCA,MAAM,MAAuD,GACvD,MAA+D,GAC/D,MAA2D,GAGjE,SAAS,GAAwD,CAC/D,OAAO,EAGT,SAAS,GAAuD,CAC9D,OAAO,EAA6CA,EAAAA,oBAAoB,CAAC,CAG3E,SAAS,EACP,EACsB,CACtB,IAAM,EAAsBC,EAAAA,cAAc,EAAE,qBACtC,EAAoB,EAAqB,EAAgB,CAE/D,OAAA,EAAA,EAAA,aACG,GAA4B,CACvB,EACF,EAAoB,EAAO,CAE3B,GAAA,EAAA,EAAA,yBAA0C,EAAO,CAAE,CACjD,KAAM,cACN,SACD,CAAC,EAGN,CAAC,EAAqB,EAAkB,CACzC,CAGH,SAAS,GAAgE,CACvE,OAAO,EAGT,SAAS,GAA+D,CACtE,IAAM,EAAsBA,EAAAA,cAAc,EAAE,yBACtC,EAAoB,EAAqBD,EAAAA,oBAAoB,CAAC,CAEpE,OAAA,EAAA,EAAA,aACG,GAA6B,CACxB,EACF,EAAoB,EAAO,CAE3B,GAAA,EAAA,EAAA,0BAA2C,EAAO,CAAE,CAClD,KAAM,kBACN,SACD,CAAC,EAGN,CAAC,EAAqB,EAAkB,CACzC,CAGH,SAAS,GAA+D,CACtE,OAAO,EAGT,SAAS,GAA8D,CACrE,IAAM,EAAsBC,EAAAA,cAAc,EAAE,uBACtC,EAAoB,EAAqBD,EAAAA,oBAAoB,CAAC,CACpE,OAAA,EAAA,EAAA,aACG,GAA6B,CACxB,EACF,EAAoB,EAAO,CAE3B,GAAA,EAAA,EAAA,0BAA2C,EAAO,CAAE,CAClD,KAAM,mBACN,SACD,CAAC,EAGN,CAAC,EAAqB,EAAkB,CACzC,CAGH,MAAa,EACX,QAAQ,IAAI,WAAa,aACrB,EACA,EAEO,EAGX,QAAQ,IAAI,WAAa,aACrB,EACA,EAEO,EACX,QAAQ,IAAI,WAAa,aACrB,EACA,EAEO,EACX,QAAQ,IAAI,WAAa,aACrB,EACA,EAKN,SAAS,EAAqB,EAA0B,CAGtD,IAAM,EACJ,QAAQ,IAAI,WAAa,eAAA,EAAA,EAAA,gBACV,CAAC,uBAAuB,CACnC,EAAYE,EAAAA,cAAc,CAM1B,GAAA,EAAA,EAAA,QAA8D,KAAK,CA6BzE,MA5BA,CACE,EAAkB,UAAU,IAAI,KAKlC,EAAA,EAAA,eAAgB,CACd,IAAM,EAAiB,EAAkB,QACzC,GAAI,EAAe,OAAS,EAAG,OAC/B,IAAM,EAAU,MAAM,KAAK,EAAe,QAAQ,CAAC,CACnD,EAAe,OAAO,CAClB,GAAC,GAAuB,CAAC,IAE7B,EAAQ,SAAS,CAAE,OAAM,YAAa,CACpC,OAAQ,EAAR,CACE,IAAK,cACH,EAAU,UAAU,EAAO,CAC3B,MACF,IAAK,kBACH,EAAU,yBAAyB,EAAO,CAC1C,MACF,IAAK,mBACH,EAAU,0BAA0B,EAAO,CAC3C,QAEJ,EACF,EAEF,EAAA,EAAA,cAAoB,EAAa,IAAiC,CAChE,EAAkB,QAAS,IAAI,EAAK,EAAc,EACjD,EAAE,CAAC"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{useGTContext as e}from"../../context/context.mjs";import{useShouldTranslate as t}from"../utils.mjs";import{useI18nStore as n}from"../../i18n-store/useI18nStore.mjs";import{getDictionaryListenerKey as r,getI18nConfig as i,getTranslateListenerKey as a}from"gt-i18n/internal";import{useCallback as o,useEffect as s}from"react";const
|
|
1
|
+
import{useGTContext as e}from"../../context/context.mjs";import{useShouldTranslate as t}from"../utils.mjs";import{useI18nStore as n}from"../../i18n-store/useI18nStore.mjs";import{getDictionaryListenerKey as r,getI18nConfig as i,getTranslateListenerKey as a}from"gt-i18n/internal";import{useCallback as o,useEffect as s,useRef as c}from"react";const l=()=>{},u=()=>{},d=()=>{};function f(){return l}function p(){return m(t())}function m(t){let n=e()?.onMissingTranslation,r=C(t);return o(e=>{n?n(e):r(a(e),{type:`translation`,lookup:e})},[n,r])}function h(){return u}function g(){let n=e()?.onMissingDictionaryEntry,i=C(t());return o(e=>{n?n(e):i(r(e),{type:`dictionaryEntry`,lookup:e})},[n,i])}function _(){return d}function v(){let n=e()?.onMissingDictionaryObj,i=C(t());return o(e=>{n?n(e):i(r(e),{type:`dictionaryObject`,lookup:e})},[n,i])}const y=process.env.NODE_ENV===`production`?f:p,b=process.env.NODE_ENV===`production`?f:m,x=process.env.NODE_ENV===`production`?h:g,S=process.env.NODE_ENV===`production`?_:v;function C(e){let t=process.env.NODE_ENV!==`production`&&i().isDevHotReloadEnabled(),r=n(),a=c(null);return a.current??=new Map,s(()=>{let n=a.current;if(n.size===0)return;let i=Array.from(n.values());n.clear(),!(!t||!e)&&i.forEach(({type:e,lookup:t})=>{switch(e){case`translation`:r.translate(t);break;case`dictionaryEntry`:r.translateDictionaryEntry(t);break;case`dictionaryObject`:r.translateDictionaryObject(t);break}})}),o((e,t)=>{a.current.set(e,t)},[])}export{x as useHandleMissingDictionaryEntry,S as useHandleMissingDictionaryObject,y as useHandleMissingTranslation,b as useHandleMissingTranslationWithConditions};
|
|
2
2
|
//# sourceMappingURL=missing-translation.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"missing-translation.mjs","names":[],"sources":["../../../src/hooks/utils/missing-translation.ts"],"sourcesContent":["import {\n getDictionaryListenerKey,\n getI18nConfig,\n getTranslateListenerKey,\n} from 'gt-i18n/internal';\nimport { useShouldTranslate } from '../utils';\nimport { useI18nStore } from '../../i18n-store/useI18nStore';\nimport { DictionaryLookup, TranslateLookup } from '../../i18n-store/storeTypes';\nimport { useCallback, useEffect } from 'react';\nimport { Translation } from 'gt-i18n/types';\nimport { useGTContext } from '../../context/context';\n\nexport type OnMissingTranslation = <T extends Translation>(\n lookup: TranslateLookup<T>\n) => void;\nexport type OnMissingDictionaryEntry = (lookup: DictionaryLookup) => void;\n// TODO: rename to OnMissingDictionaryObject\nexport type OnMissingDictionaryObj = (lookup: DictionaryLookup) => void;\n\ntype PendingLookup =\n | {\n type: 'translation';\n lookup: TranslateLookup;\n }\n | {\n type: 'dictionaryEntry' | 'dictionaryObject';\n lookup: DictionaryLookup;\n };\n\n/**\n * Why have custom handleMissing functions?\n *\n * Some runtimes (like server) cannot make useEffect calls, so\n * we need to give them access to a callback where they can embed\n * their own translation calls\n *\n * While this is technnically not pure behavior, this is acceptable\n * in development hot reload\n */\n\nconst noopOnMissingTranslation: OnMissingTranslation = () => {};\nconst noopOnMissingDictionaryEntry: OnMissingDictionaryEntry = () => {};\nconst noopOnMissingDictionaryObj: OnMissingDictionaryObj = () => {};\n\n// TODO: reduce code duplication with the three below functions\nfunction useHandleMissingTranslationProd(): OnMissingTranslation {\n return noopOnMissingTranslation;\n}\n\nfunction useHandleMissingTranslationDev(): OnMissingTranslation {\n return useHandleMissingTranslationWithConditionsDev(useShouldTranslate());\n}\n\nfunction useHandleMissingTranslationWithConditionsDev(\n shouldTranslate: boolean\n): OnMissingTranslation {\n const customHandleMissing = useGTContext()?.onMissingTranslation;\n const pureHandleMissing = useDevHotReloadQueue(shouldTranslate);\n\n return useCallback(\n (lookup: TranslateLookup) => {\n if (customHandleMissing) {\n customHandleMissing(lookup);\n } else {\n pureHandleMissing(getTranslateListenerKey(lookup), {\n type: 'translation',\n lookup,\n });\n }\n },\n [customHandleMissing, pureHandleMissing]\n );\n}\n\nfunction useHandleMissingDictionaryEntryProd(): OnMissingDictionaryEntry {\n return noopOnMissingDictionaryEntry;\n}\n\nfunction useHandleMissingDictionaryEntryDev(): OnMissingDictionaryEntry {\n const customHandleMissing = useGTContext()?.onMissingDictionaryEntry;\n const pureHandleMissing = useDevHotReloadQueue(useShouldTranslate());\n\n return useCallback(\n (lookup: DictionaryLookup) => {\n if (customHandleMissing) {\n customHandleMissing(lookup);\n } else {\n pureHandleMissing(getDictionaryListenerKey(lookup), {\n type: 'dictionaryEntry',\n lookup,\n });\n }\n },\n [customHandleMissing, pureHandleMissing]\n );\n}\n\nfunction useHandleMissingDictionaryObjectProd(): OnMissingDictionaryObj {\n return noopOnMissingDictionaryObj;\n}\n\nfunction useHandleMissingDictionaryObjectDev(): OnMissingDictionaryObj {\n const customHandleMissing = useGTContext()?.onMissingDictionaryObj;\n const pureHandleMissing = useDevHotReloadQueue(useShouldTranslate());\n return useCallback(\n (lookup: DictionaryLookup) => {\n if (customHandleMissing) {\n customHandleMissing(lookup);\n } else {\n pureHandleMissing(getDictionaryListenerKey(lookup), {\n type: 'dictionaryObject',\n lookup,\n });\n }\n },\n [customHandleMissing, pureHandleMissing]\n );\n}\n\nexport const useHandleMissingTranslation: () => OnMissingTranslation =\n process.env.NODE_ENV === 'production'\n ? useHandleMissingTranslationProd\n : useHandleMissingTranslationDev;\n\nexport const useHandleMissingTranslationWithConditions: (\n shouldTranslate: boolean\n) => OnMissingTranslation =\n process.env.NODE_ENV === 'production'\n ? useHandleMissingTranslationProd\n : useHandleMissingTranslationWithConditionsDev;\n\nexport const useHandleMissingDictionaryEntry: () => OnMissingDictionaryEntry =\n process.env.NODE_ENV === 'production'\n ? useHandleMissingDictionaryEntryProd\n : useHandleMissingDictionaryEntryDev;\n\nexport const useHandleMissingDictionaryObject: () => OnMissingDictionaryObj =\n process.env.NODE_ENV === 'production'\n ? useHandleMissingDictionaryObjectProd\n : useHandleMissingDictionaryObjectDev;\n\n/**\n * HMR translation needs to be deferred to post-commit phase\n */\nfunction useDevHotReloadQueue(shouldTranslate: boolean) {\n // Statically gated so bundlers can drop dev hot-reload work from\n // production builds.\n const devHotReloadEnabled =\n process.env.NODE_ENV !== 'production' &&\n getI18nConfig().isDevHotReloadEnabled();\n const i18nStore = useI18nStore();\n\n //
|
|
1
|
+
{"version":3,"file":"missing-translation.mjs","names":[],"sources":["../../../src/hooks/utils/missing-translation.ts"],"sourcesContent":["import {\n getDictionaryListenerKey,\n getI18nConfig,\n getTranslateListenerKey,\n} from 'gt-i18n/internal';\nimport { useShouldTranslate } from '../utils';\nimport { useI18nStore } from '../../i18n-store/useI18nStore';\nimport { DictionaryLookup, TranslateLookup } from '../../i18n-store/storeTypes';\nimport { useCallback, useEffect, useRef } from 'react';\nimport { Translation } from 'gt-i18n/types';\nimport { useGTContext } from '../../context/context';\n\nexport type OnMissingTranslation = <T extends Translation>(\n lookup: TranslateLookup<T>\n) => void;\nexport type OnMissingDictionaryEntry = (lookup: DictionaryLookup) => void;\n// TODO: rename to OnMissingDictionaryObject\nexport type OnMissingDictionaryObj = (lookup: DictionaryLookup) => void;\n\ntype PendingLookup =\n | {\n type: 'translation';\n lookup: TranslateLookup;\n }\n | {\n type: 'dictionaryEntry' | 'dictionaryObject';\n lookup: DictionaryLookup;\n };\n\n/**\n * Why have custom handleMissing functions?\n *\n * Some runtimes (like server) cannot make useEffect calls, so\n * we need to give them access to a callback where they can embed\n * their own translation calls\n *\n * While this is technnically not pure behavior, this is acceptable\n * in development hot reload\n */\n\nconst noopOnMissingTranslation: OnMissingTranslation = () => {};\nconst noopOnMissingDictionaryEntry: OnMissingDictionaryEntry = () => {};\nconst noopOnMissingDictionaryObj: OnMissingDictionaryObj = () => {};\n\n// TODO: reduce code duplication with the three below functions\nfunction useHandleMissingTranslationProd(): OnMissingTranslation {\n return noopOnMissingTranslation;\n}\n\nfunction useHandleMissingTranslationDev(): OnMissingTranslation {\n return useHandleMissingTranslationWithConditionsDev(useShouldTranslate());\n}\n\nfunction useHandleMissingTranslationWithConditionsDev(\n shouldTranslate: boolean\n): OnMissingTranslation {\n const customHandleMissing = useGTContext()?.onMissingTranslation;\n const pureHandleMissing = useDevHotReloadQueue(shouldTranslate);\n\n return useCallback(\n (lookup: TranslateLookup) => {\n if (customHandleMissing) {\n customHandleMissing(lookup);\n } else {\n pureHandleMissing(getTranslateListenerKey(lookup), {\n type: 'translation',\n lookup,\n });\n }\n },\n [customHandleMissing, pureHandleMissing]\n );\n}\n\nfunction useHandleMissingDictionaryEntryProd(): OnMissingDictionaryEntry {\n return noopOnMissingDictionaryEntry;\n}\n\nfunction useHandleMissingDictionaryEntryDev(): OnMissingDictionaryEntry {\n const customHandleMissing = useGTContext()?.onMissingDictionaryEntry;\n const pureHandleMissing = useDevHotReloadQueue(useShouldTranslate());\n\n return useCallback(\n (lookup: DictionaryLookup) => {\n if (customHandleMissing) {\n customHandleMissing(lookup);\n } else {\n pureHandleMissing(getDictionaryListenerKey(lookup), {\n type: 'dictionaryEntry',\n lookup,\n });\n }\n },\n [customHandleMissing, pureHandleMissing]\n );\n}\n\nfunction useHandleMissingDictionaryObjectProd(): OnMissingDictionaryObj {\n return noopOnMissingDictionaryObj;\n}\n\nfunction useHandleMissingDictionaryObjectDev(): OnMissingDictionaryObj {\n const customHandleMissing = useGTContext()?.onMissingDictionaryObj;\n const pureHandleMissing = useDevHotReloadQueue(useShouldTranslate());\n return useCallback(\n (lookup: DictionaryLookup) => {\n if (customHandleMissing) {\n customHandleMissing(lookup);\n } else {\n pureHandleMissing(getDictionaryListenerKey(lookup), {\n type: 'dictionaryObject',\n lookup,\n });\n }\n },\n [customHandleMissing, pureHandleMissing]\n );\n}\n\nexport const useHandleMissingTranslation: () => OnMissingTranslation =\n process.env.NODE_ENV === 'production'\n ? useHandleMissingTranslationProd\n : useHandleMissingTranslationDev;\n\nexport const useHandleMissingTranslationWithConditions: (\n shouldTranslate: boolean\n) => OnMissingTranslation =\n process.env.NODE_ENV === 'production'\n ? useHandleMissingTranslationProd\n : useHandleMissingTranslationWithConditionsDev;\n\nexport const useHandleMissingDictionaryEntry: () => OnMissingDictionaryEntry =\n process.env.NODE_ENV === 'production'\n ? useHandleMissingDictionaryEntryProd\n : useHandleMissingDictionaryEntryDev;\n\nexport const useHandleMissingDictionaryObject: () => OnMissingDictionaryObj =\n process.env.NODE_ENV === 'production'\n ? useHandleMissingDictionaryObjectProd\n : useHandleMissingDictionaryObjectDev;\n\n/**\n * HMR translation needs to be deferred to post-commit phase\n */\nfunction useDevHotReloadQueue(shouldTranslate: boolean) {\n // Statically gated so bundlers can drop dev hot-reload work from\n // production builds.\n const devHotReloadEnabled =\n process.env.NODE_ENV !== 'production' &&\n getI18nConfig().isDevHotReloadEnabled();\n const i18nStore = useI18nStore();\n\n // Persist the queue across renders so the enqueue callback keeps a stable\n // identity. An unstable callback would change the identity of every\n // translation function built on top of it (e.g. gt() from useGT()), which\n // re-runs user effects that depend on it after every render.\n const pendingLookupsRef = useRef<Map<string, PendingLookup> | null>(null);\n if (pendingLookupsRef.current == null) {\n pendingLookupsRef.current = new Map();\n }\n\n // Pure react: effects run after render\n // No dependency array: flush whatever was enqueued after every commit\n useEffect(() => {\n const pendingLookups = pendingLookupsRef.current!;\n if (pendingLookups.size === 0) return;\n const lookups = Array.from(pendingLookups.values());\n pendingLookups.clear();\n if (!devHotReloadEnabled || !shouldTranslate) return;\n\n lookups.forEach(({ type, lookup }) => {\n switch (type) {\n case 'translation':\n i18nStore.translate(lookup);\n break;\n case 'dictionaryEntry':\n i18nStore.translateDictionaryEntry(lookup);\n break;\n case 'dictionaryObject':\n i18nStore.translateDictionaryObject(lookup);\n break;\n }\n });\n });\n\n return useCallback((key: string, pendingLookup: PendingLookup) => {\n pendingLookupsRef.current!.set(key, pendingLookup);\n }, []);\n}\n"],"mappings":"uVAwCA,MAAM,MAAuD,GACvD,MAA+D,GAC/D,MAA2D,GAGjE,SAAS,GAAwD,CAC/D,OAAO,EAGT,SAAS,GAAuD,CAC9D,OAAO,EAA6C,GAAoB,CAAC,CAG3E,SAAS,EACP,EACsB,CACtB,IAAM,EAAsB,GAAc,EAAE,qBACtC,EAAoB,EAAqB,EAAgB,CAE/D,OAAO,EACJ,GAA4B,CACvB,EACF,EAAoB,EAAO,CAE3B,EAAkB,EAAwB,EAAO,CAAE,CACjD,KAAM,cACN,SACD,CAAC,EAGN,CAAC,EAAqB,EAAkB,CACzC,CAGH,SAAS,GAAgE,CACvE,OAAO,EAGT,SAAS,GAA+D,CACtE,IAAM,EAAsB,GAAc,EAAE,yBACtC,EAAoB,EAAqB,GAAoB,CAAC,CAEpE,OAAO,EACJ,GAA6B,CACxB,EACF,EAAoB,EAAO,CAE3B,EAAkB,EAAyB,EAAO,CAAE,CAClD,KAAM,kBACN,SACD,CAAC,EAGN,CAAC,EAAqB,EAAkB,CACzC,CAGH,SAAS,GAA+D,CACtE,OAAO,EAGT,SAAS,GAA8D,CACrE,IAAM,EAAsB,GAAc,EAAE,uBACtC,EAAoB,EAAqB,GAAoB,CAAC,CACpE,OAAO,EACJ,GAA6B,CACxB,EACF,EAAoB,EAAO,CAE3B,EAAkB,EAAyB,EAAO,CAAE,CAClD,KAAM,mBACN,SACD,CAAC,EAGN,CAAC,EAAqB,EAAkB,CACzC,CAGH,MAAa,EACX,QAAQ,IAAI,WAAa,aACrB,EACA,EAEO,EAGX,QAAQ,IAAI,WAAa,aACrB,EACA,EAEO,EACX,QAAQ,IAAI,WAAa,aACrB,EACA,EAEO,EACX,QAAQ,IAAI,WAAa,aACrB,EACA,EAKN,SAAS,EAAqB,EAA0B,CAGtD,IAAM,EACJ,QAAQ,IAAI,WAAa,cACzB,GAAe,CAAC,uBAAuB,CACnC,EAAY,GAAc,CAM1B,EAAoB,EAA0C,KAAK,CA6BzE,MA5BA,CACE,EAAkB,UAAU,IAAI,IAKlC,MAAgB,CACd,IAAM,EAAiB,EAAkB,QACzC,GAAI,EAAe,OAAS,EAAG,OAC/B,IAAM,EAAU,MAAM,KAAK,EAAe,QAAQ,CAAC,CACnD,EAAe,OAAO,CAClB,GAAC,GAAuB,CAAC,IAE7B,EAAQ,SAAS,CAAE,OAAM,YAAa,CACpC,OAAQ,EAAR,CACE,IAAK,cACH,EAAU,UAAU,EAAO,CAC3B,MACF,IAAK,kBACH,EAAU,yBAAyB,EAAO,CAC1C,MACF,IAAK,mBACH,EAAU,0BAA0B,EAAO,CAC3C,QAEJ,EACF,CAEK,GAAa,EAAa,IAAiC,CAChE,EAAkB,QAAS,IAAI,EAAK,EAAc,EACjD,EAAE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@generaltranslation/react-core",
|
|
3
|
-
"version": "11.4.
|
|
3
|
+
"version": "11.4.17",
|
|
4
4
|
"description": "A pure React library for internationalization",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"@generaltranslation/format": "0.1.9",
|
|
15
|
-
"generaltranslation": "9.5.
|
|
16
|
-
"gt-i18n": "1.0.
|
|
15
|
+
"generaltranslation": "9.5.8",
|
|
16
|
+
"gt-i18n": "1.0.39"
|
|
17
17
|
},
|
|
18
18
|
"repository": {
|
|
19
19
|
"type": "git",
|