-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make language switcher work with translatable slugs.
- Loading branch information
Showing
11 changed files
with
89 additions
and
20 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './language-data-provider'; |
14 changes: 14 additions & 0 deletions
14
components/language-data-provider/language-data-provider.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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use client'; | ||
|
||
import { ReactNode, useState } from 'react'; | ||
|
||
import { LanguageDataContext } from 'ui/context'; | ||
|
||
interface LanguageDataProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
export const LanguageDataProvider = ({ children }: LanguageDataProviderProps) => { | ||
const slugsState = useState<Record<string, string>>({}); | ||
return <LanguageDataContext.Provider value={{ slugsState }}>{children}</LanguageDataContext.Provider>; | ||
}; |
15 changes: 15 additions & 0 deletions
15
components/language-data-provider/language-data-setter.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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use client'; | ||
|
||
import { useEffect } from 'react'; | ||
|
||
import { useLanguageDataContext } from '#/ui/hooks'; | ||
|
||
export const LanguageDataSetter = ({ data }: { data?: Record<string, string> }) => { | ||
const [, setSlugs] = useLanguageDataContext()!.slugsState; | ||
useEffect(() => { | ||
if (data) { | ||
setSlugs(data); | ||
} | ||
}, [data]); | ||
return null; | ||
}; |
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,19 +1,35 @@ | ||
'use client'; | ||
|
||
import { useChangeLocale, useCurrentLocale } from '#/locales/client'; | ||
import { ChangeEvent, useTransition } from 'react'; | ||
import { useRouter } from 'next/navigation'; | ||
|
||
import { useCurrentLocale } from '#/locales/client'; | ||
import { useLanguageDataContext } from '#/ui/hooks'; | ||
|
||
export const LanguageSelector = () => { | ||
const locale = useCurrentLocale(); | ||
const changeLocale = useChangeLocale({ preserveSearchParams: true }); | ||
const router = useRouter(); | ||
const [isPending, startTransition] = useTransition(); | ||
const languageData = useLanguageDataContext(); | ||
|
||
const handleLanguageChange = (e: React.ChangeEvent<HTMLSelectElement>) => { | ||
changeLocale(e.target.value as 'en' | 'de'); | ||
const handleLanguageChange = (e: ChangeEvent<HTMLSelectElement>) => { | ||
startTransition(() => { | ||
const selectedLocale = e.target.value; | ||
const [slugs] = languageData?.slugsState ?? []; | ||
if (slugs?.[selectedLocale]) { | ||
router.push(`/${selectedLocale}/${slugs[selectedLocale]}`); | ||
} else { | ||
router.push(`/${selectedLocale}`); | ||
} | ||
}); | ||
}; | ||
|
||
return ( | ||
<select value={locale} onChange={handleLanguageChange}> | ||
<option value="en">English</option> | ||
<option value="de">Deutsche</option> | ||
</select> | ||
<> | ||
<select value={locale} onChange={handleLanguageChange} disabled={isPending}> | ||
<option value="en">English</option> | ||
<option value="de">Deutsche</option> | ||
</select> | ||
</> | ||
); | ||
}; |
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 @@ | ||
export * from './language-data.context'; |
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,4 @@ | ||
import { createContext, Dispatch, SetStateAction } from 'react'; | ||
|
||
type LanguageData = { slugsState: [Record<string, string>, Dispatch<SetStateAction<Record<string, string>>>] }; | ||
export const LanguageDataContext = createContext<LanguageData | undefined>(undefined); |
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 @@ | ||
export * from './use-language-data-context.hook'; |
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,8 @@ | ||
import { useContext } from 'react'; | ||
|
||
import { LanguageDataContext } from '#/ui/context'; | ||
|
||
export function useLanguageDataContext() { | ||
const context = useContext(LanguageDataContext); | ||
return context ?? null; | ||
} |