-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(i18n): move to external store (#6006)
- Loading branch information
1 parent
c2c3730
commit 068e441
Showing
8 changed files
with
106 additions
and
151 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,18 @@ | ||
'use client'; | ||
|
||
import I18nBundle, { getI18nBundle } from '@ui5/webcomponents-base/dist/i18nBundle.js'; | ||
import { useRef } from 'react'; | ||
import { useI18nContext } from '../context/I18nContext.js'; | ||
import { useIsomorphicLayoutEffect } from '../hooks/index.js'; | ||
import I18nBundle from '@ui5/webcomponents-base/dist/i18nBundle.js'; | ||
import { useEffect } from 'react'; | ||
import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js'; | ||
import { I18nStore } from '../stores/I18nStore.js'; | ||
|
||
const defaultBundle = new I18nBundle('defaultBundle'); | ||
|
||
export const useI18nBundle = (bundleName: string): I18nBundle => { | ||
const i18nContext = useI18nContext(); | ||
const bundles = useSyncExternalStore(I18nStore.subscribe, I18nStore.getSnapshot, I18nStore.getServerSnapshot); | ||
|
||
if (!i18nContext) { | ||
throw new Error(`'useI18nBundle()' may be used only in the context of a '<ThemeProvider>' component.`); | ||
} | ||
const i18nRef = useRef(i18nContext); | ||
|
||
useIsomorphicLayoutEffect(() => { | ||
const { i18nBundles, setI18nBundle } = i18nRef.current; | ||
let isMounted = true; | ||
if (!i18nBundles.hasOwnProperty(bundleName)) { | ||
getI18nBundle(bundleName).then( | ||
(internalBundle) => { | ||
if (isMounted) { | ||
setI18nBundle(bundleName, internalBundle); | ||
} | ||
}, | ||
() => { | ||
// noop | ||
} | ||
); | ||
} | ||
return () => { | ||
isMounted = false; | ||
}; | ||
useEffect(() => { | ||
I18nStore.loadBundle(bundleName); | ||
}, [bundleName]); | ||
|
||
return i18nContext.i18nBundles[bundleName] ?? defaultBundle; | ||
return bundles[bundleName] ?? defaultBundle; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { getI18nContext, I18nContext } from './context/I18nContext.js'; | ||
import * as Device from './Device/index.js'; | ||
import * as hooks from './hooks/index.js'; | ||
import { I18nStore } from './stores/I18nStore.js'; | ||
import { StyleStore } from './stores/StyleStore.js'; | ||
import { ThemingParameters } from './styling/ThemingParameters.js'; | ||
|
||
export * from './styling/CssSizeVariables.js'; | ||
export * from './utils/index.js'; | ||
export * from './hooks/index.js'; | ||
|
||
export { getI18nContext, I18nContext, StyleStore, ThemingParameters, Device, hooks }; | ||
export { I18nStore, StyleStore, ThemingParameters, Device, hooks }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import type I18nBundle from '@ui5/webcomponents-base/dist/i18nBundle.js'; | ||
import { getI18nBundle } from '@ui5/webcomponents-base/dist/i18nBundle.js'; | ||
|
||
const STORE_SYMBOL_LISTENERS = Symbol.for('@ui5/webcomponents-react/I18nStore/Listeners'); | ||
const STORE_SYMBOL = Symbol.for('@ui5/webcomponents-react/I18nStore'); | ||
|
||
const initialStore: Record<string, I18nBundle> = {}; | ||
|
||
function getListeners(): Array<() => void> { | ||
globalThis[STORE_SYMBOL_LISTENERS] ??= []; | ||
return globalThis[STORE_SYMBOL_LISTENERS]; | ||
} | ||
|
||
function emitChange() { | ||
for (const listener of getListeners()) { | ||
listener(); | ||
} | ||
} | ||
|
||
function getSnapshot(): Record<string, I18nBundle> { | ||
globalThis[STORE_SYMBOL] ??= initialStore; | ||
return globalThis[STORE_SYMBOL]; | ||
} | ||
|
||
function subscribe(listener: () => void) { | ||
const listeners = getListeners(); | ||
globalThis[STORE_SYMBOL_LISTENERS] = [...listeners, listener]; | ||
return () => { | ||
globalThis[STORE_SYMBOL_LISTENERS] = listeners.filter((l) => l !== listener); | ||
}; | ||
} | ||
|
||
export const I18nStore = { | ||
subscribe, | ||
getSnapshot, | ||
getServerSnapshot: () => { | ||
return initialStore; | ||
}, | ||
loadBundle: (bundleName: string) => { | ||
const bundles = getSnapshot(); | ||
if (!bundles.hasOwnProperty(bundleName)) { | ||
void getI18nBundle(bundleName).then((bundle) => { | ||
globalThis[STORE_SYMBOL] = { | ||
...globalThis[STORE_SYMBOL], | ||
[bundleName]: bundle | ||
}; | ||
emitChange(); | ||
}); | ||
} | ||
}, | ||
handleLanguageChange: async () => { | ||
const bundles = getSnapshot(); | ||
const newBundles = await Promise.all(Object.keys(bundles).map((bundleName) => getI18nBundle(bundleName))); | ||
|
||
globalThis[STORE_SYMBOL] = newBundles.reduce( | ||
(acc, bundle) => ({ | ||
...acc, | ||
[bundle.packageName]: bundle | ||
}), | ||
{} | ||
); | ||
emitChange(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters