-
Notifications
You must be signed in to change notification settings - Fork 32
feat: Preserve locale with cookie & react-intl #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9c26580
2c817f8
56616b9
4578412
f168ee3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { createContext } from 'preact'; | ||
| import { useContext, useState, useEffect } from 'preact/hooks'; | ||
|
|
||
| import { defaultLocale } from '../../site.json' with { type: 'json' }; | ||
|
|
||
| const LOCALE_COOKIE = 'NEXT_LOCALE'; | ||
|
|
||
| /** | ||
| * Replaces the default locale prefix in a link with the given locale. | ||
| * | ||
| * @param {string} link | ||
| * @param {string} locale | ||
| * @returns {string} | ||
| */ | ||
| const localizeLink = (link, locale) => | ||
| link.replace(new RegExp(`^/${defaultLocale}(?=/|$)`), `/${locale}`); | ||
|
|
||
| /** | ||
| * Detects the locale from the NEXT_LOCALE cookie. | ||
| * Falls back to the default locale when the cookie is missing. | ||
| * | ||
| * @returns {string} | ||
| */ | ||
| export const detectLocaleFromCookies = () => { | ||
| if (typeof document === 'undefined') { | ||
| return defaultLocale; | ||
| } | ||
|
|
||
| const match = document.cookie | ||
| .split(';') | ||
| .map(cookie => cookie.trim()) | ||
| .find(cookie => cookie.startsWith(`${LOCALE_COOKIE}=`)); | ||
|
|
||
| return match | ||
| ? decodeURIComponent(match.slice(LOCALE_COOKIE.length + 1)) | ||
| : defaultLocale; | ||
| }; | ||
|
|
||
| const LocaleContext = createContext({ | ||
| locale: defaultLocale, | ||
| localizeLink: link => link, | ||
| }); | ||
|
|
||
| export const useLocale = () => useContext(LocaleContext); | ||
|
|
||
| /** | ||
| * Provides locale and a pre-bound localizeLink fn to the component tree. | ||
| * | ||
| * @param {{ locale?: string, children: import('preact').ComponentChildren }} props | ||
| */ | ||
| export default function LocaleProvider({ locale, children }) { | ||
| const [detectedLocale, setDetectedLocale] = useState(defaultLocale); | ||
|
|
||
| useEffect(() => { | ||
| setDetectedLocale(locale ?? detectLocaleFromCookies()); | ||
| }, [locale]); | ||
|
|
||
| const value = { | ||
| locale: detectedLocale, | ||
| localizeLink: link => localizeLink(link, detectedLocale), | ||
| }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unmemoized context value defeats downstream useMemoMedium Severity The Additional Locations (1)Reviewed by Cursor Bugbot for commit f168ee3. Configure here. |
||
|
|
||
| return ( | ||
| <LocaleContext.Provider value={value}>{children}</LocaleContext.Provider> | ||
| ); | ||
| } | ||


Uh oh!
There was an error while loading. Please reload this page.