-
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
Open
canerakdas
wants to merge
3
commits into
nodejs:main
Choose a base branch
from
canerakdas:feat/preserve-locale-with-cookie
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -2,23 +2,39 @@ import ThemeToggle from '@node-core/ui-components/Common/ThemeToggle'; | |
| import NavBar from '@node-core/ui-components/Containers/NavBar'; | ||
| import styles from '@node-core/ui-components/Containers/NavBar/index.module.css'; | ||
| import GitHubIcon from '@node-core/ui-components/Icons/Social/GitHub'; | ||
|
|
||
| import SearchBox from '@node-core/doc-kit/src/generators/web/ui/components/SearchBox'; | ||
| import { useTheme } from '@node-core/doc-kit/src/generators/web/ui/hooks/useTheme.mjs'; | ||
|
|
||
| import { useEffect, useState } from 'preact/hooks'; | ||
| import { useIntl } from 'react-intl'; | ||
|
|
||
| import localizeLink from '../../util/link'; | ||
| import { navigation } from '../../site.json' with { type: 'json' }; | ||
|
canerakdas marked this conversation as resolved.
|
||
|
|
||
| import Logo from '#theme/Logo'; | ||
|
|
||
| /** | ||
| * NavBar component that displays the headings, search, etc. | ||
| */ | ||
| export default ({ metadata }) => { | ||
| const [themePreference, setThemePreference] = useTheme(); | ||
| const [navigationItems, setNavigationItems] = useState(navigation); | ||
| const { locale } = useIntl(); | ||
|
|
||
| useEffect(() => { | ||
|
Member
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. Do we need to use an useEffect here? Can't this be an useMemo? |
||
| const items = navigation.map(item => ({ | ||
| ...item, | ||
| link: localizeLink(item.link, locale), | ||
| })); | ||
|
|
||
| setNavigationItems(items); | ||
| }, [locale]); | ||
|
|
||
| return ( | ||
| <NavBar | ||
| Logo={Logo} | ||
| sidebarItemTogglerAriaLabel="Toggle navigation menu" | ||
| navItems={navigation} | ||
| navItems={navigationItems} | ||
| > | ||
| <SearchBox pathname={metadata.path} /> | ||
| <ThemeToggle | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,43 @@ | ||
| import { IntlProvider } from 'react-intl'; | ||
|
|
||
| import { defaultLocale } from '../site.json' with { type: 'json' }; | ||
|
|
||
| const LOCALE_COOKIE = 'NEXT_LOCALE'; | ||
|
|
||
| /** | ||
| * Detects the locale from the cookie. | ||
| * Falls back to the default locale when the cookie is missing. | ||
| * | ||
| * @returns {string} | ||
| */ | ||
| export const detectLocaleFromCookies = () => { | ||
| if (typeof document === 'undefined') { | ||
| return defaultLocale; | ||
| } | ||
|
|
||
| const localeCookie = document.cookie | ||
| .split(';') | ||
| .map(cookie => cookie.trim()) | ||
| .find(cookie => cookie.startsWith(`${LOCALE_COOKIE}=`)); | ||
|
|
||
| if (!localeCookie) { | ||
| return defaultLocale; | ||
| } | ||
|
|
||
| return decodeURIComponent(localeCookie.slice(LOCALE_COOKIE.length + 1)); | ||
|
canerakdas marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| /** | ||
| * LocaleProvider component that provides the locale context to its children. | ||
| * | ||
| * @param {{ locale?: string, children: import('preact').ComponentChildren }} props | ||
| */ | ||
| export default function LocaleProvider({ locale, children }) { | ||
| const detectedLocale = locale ?? detectLocaleFromCookies(); | ||
|
|
||
| return ( | ||
| <IntlProvider locale={detectedLocale} messages={{}}> | ||
| {children} | ||
| </IntlProvider> | ||
| ); | ||
| } | ||
This file contains hidden or 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 hidden or 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,28 @@ | ||
| import { defaultLocale } from '../site.json' with { type: 'json' }; | ||
|
|
||
| /** | ||
| * Replaces the default locale in a link with the provided locale. | ||
| * | ||
| * @param {string} link - The link to be localized. | ||
| * @param {string|null} locale - The locale to apply to the link. | ||
| * @returns {string} - The localized link. | ||
| */ | ||
| const localizeLink = (link, locale) => { | ||
| if ( | ||
| typeof document === 'undefined' || | ||
| !link.startsWith('/') || | ||
| !link.startsWith(`/${defaultLocale}`) | ||
| ) { | ||
| return link; | ||
|
canerakdas marked this conversation as resolved.
|
||
| } | ||
|
|
||
| const localizedPrefix = locale ? `/${locale}` : ''; | ||
| const localizedLink = link.replace( | ||
| `/${defaultLocale}/`, | ||
| `${localizedPrefix}/` | ||
| ); | ||
|
|
||
|
canerakdas marked this conversation as resolved.
|
||
| return localizedLink; | ||
| }; | ||
|
|
||
| export default { localizeLink }; | ||
|
canerakdas marked this conversation as resolved.
Outdated
canerakdas marked this conversation as resolved.
Outdated
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: providers folder inside components folder.