-
Notifications
You must be signed in to change notification settings - Fork 604
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: type import * chore: refactor I18nContext usage * Revert "chore: refactor I18nContext usage" This reverts commit b9f4536. * chore: refactor useTranslation hook * feat: locale tree shaking * fix: i18nprovider * fix: default import en-US and en * revert: i18n.t style changes * revert: I18nProvider, useTranslation * fix: i18n test files * fix: typo * fix: missing locale bug & small cleanups * fix: type import * chore: refactor I18nContext usage * Revert "chore: refactor I18nContext usage" This reverts commit b9f4536. * chore: refactor useTranslation hook * feat: locale tree shaking * fix: i18nprovider * fix: default import en-US and en * revert: i18n.t style changes * revert: I18nProvider, useTranslation * revert: misc changes * fix: i18n test files * fix: typo * fix: missing locale bug & small cleanups * fix: create custom json loader for vitest * fix: waitFor instead of fake timers * fix: style tweaks * chore: changeset --------- Co-authored-by: Magomed Khamidov <53529533+KosmosKey@users.noreply.github.com>
- Loading branch information
1 parent
34572bb
commit 7ba94f4
Showing
19 changed files
with
171 additions
and
108 deletions.
There are no files selected for viewing
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,5 @@ | ||
--- | ||
"@rainbow-me/rainbowkit": patch | ||
--- | ||
|
||
Optimized bundle size for localization feature |
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 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 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 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 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 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
45 changes: 34 additions & 11 deletions
45
packages/rainbowkit/src/components/RainbowKitProvider/I18nContext.tsx
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,30 +1,53 @@ | ||
import React, { ReactNode, createContext, useMemo } from 'react'; | ||
import React, { | ||
ReactNode, | ||
createContext, | ||
useEffect, | ||
useMemo, | ||
useState, | ||
} from 'react'; | ||
|
||
import { Locale, i18n as _i18n } from '../../locales'; | ||
import { Locale, i18n, setLocale } from '../../locales'; | ||
import { detectedBrowserLocale } from '../../utils/locale'; | ||
|
||
export const I18nContext = createContext<typeof _i18n>(_i18n); | ||
export const I18nContext = createContext<{ i18n: typeof i18n }>({ i18n }); | ||
|
||
interface I18nProviderProps { | ||
children: ReactNode; | ||
locale?: Locale; | ||
} | ||
|
||
export const I18nProvider = ({ children, locale }: I18nProviderProps) => { | ||
const [updateCount, setUpdateCount] = useState(0); | ||
|
||
const browserLocale: Locale = useMemo( | ||
() => detectedBrowserLocale() as Locale, | ||
[], | ||
); | ||
|
||
const i18n = useMemo(() => { | ||
if (locale) { | ||
_i18n.locale = locale; | ||
} else if (!locale && browserLocale) { | ||
_i18n.locale = browserLocale; | ||
} | ||
useEffect(() => { | ||
const unsubscribe = i18n.onChange(() => { | ||
setUpdateCount((count) => count + 1); | ||
}); | ||
return unsubscribe; | ||
}, []); | ||
|
||
return _i18n; | ||
useEffect(() => { | ||
if (locale && locale !== i18n.locale) { | ||
setLocale(locale); | ||
} else if (!locale && browserLocale && browserLocale !== i18n.locale) { | ||
setLocale(browserLocale); | ||
} | ||
}, [locale, browserLocale]); | ||
|
||
return <I18nContext.Provider value={i18n}>{children}</I18nContext.Provider>; | ||
// biome-ignore lint/correctness/useExhaustiveDependencies: <explanation> | ||
const memoizedValue = useMemo(() => { | ||
const t = (key: string, options?: any) => i18n.t(key, options); | ||
return { t, i18n }; | ||
}, [updateCount]); | ||
|
||
return ( | ||
<I18nContext.Provider value={memoizedValue}> | ||
{children} | ||
</I18nContext.Provider> | ||
); | ||
}; |
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 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
Oops, something went wrong.
7ba94f4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
rainbowkit-example – ./
rainbowkit-example-git-main-rainbowdotme.vercel.app
rainbowkit-example-rainbowdotme.vercel.app
rainbowkit-example.vercel.app
7ba94f4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
rainbowkit-site – ./
rainbowkit.com
v2-rainbowkit.vercel.app
rainbowkit.vercel.app
www.rainbowkit.com
v1-rainbowkit.vercel.app
rainbowkit-site-rainbowdotme.vercel.app
rainbowkit-site-git-main-rainbowdotme.vercel.app