@arronqzy/i18n 0.1.14 → 0.1.15
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/package.json +1 -1
- package/src/react/I18nProvider.tsx +19 -0
- package/src/react/index.ts +1 -1
package/package.json
CHANGED
|
@@ -81,6 +81,25 @@ export function useI18n(): I18nContextValue {
|
|
|
81
81
|
return ctx;
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
+
/** Whether the nearest I18nProvider from *this* package instance is present. */
|
|
85
|
+
export function useHasI18nProvider(): boolean {
|
|
86
|
+
return useContext(I18nContext) != null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* If no I18nProvider from this package is above, wrap children with one.
|
|
91
|
+
* Fixes Umi/npm duplicate-`@arronqzy/i18n` cases where the panel Provider
|
|
92
|
+
* comes from a different copy than react-blueprint's `useI18n`.
|
|
93
|
+
*/
|
|
94
|
+
export function EnsureI18nProvider({
|
|
95
|
+
children,
|
|
96
|
+
...providerProps
|
|
97
|
+
}: I18nProviderProps) {
|
|
98
|
+
const hasProvider = useHasI18nProvider();
|
|
99
|
+
if (hasProvider) return <>{children}</>;
|
|
100
|
+
return <I18nProvider {...providerProps}>{children}</I18nProvider>;
|
|
101
|
+
}
|
|
102
|
+
|
|
84
103
|
/** Safe hook: returns zh-CN translator when outside provider (for utilities). */
|
|
85
104
|
export function useI18nOptional(): I18nContextValue {
|
|
86
105
|
const ctx = useContext(I18nContext);
|
package/src/react/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { I18nProvider, useI18n, useI18nOptional } from "./I18nProvider";
|
|
1
|
+
export { I18nProvider, EnsureI18nProvider, useI18n, useI18nOptional, useHasI18nProvider } from "./I18nProvider";
|
|
2
2
|
export type { I18nContextValue, I18nProviderProps } from "./I18nProvider";
|