-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add locale switcher to Header (#1907)
* feat(core): add locale switcher to Header * fix: add translation for error * chore: add context comment
- Loading branch information
Showing
3 changed files
with
54 additions
and
4 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,40 @@ | ||
'use server'; | ||
|
||
import { SubmissionResult } from '@conform-to/react'; | ||
import { parseWithZod } from '@conform-to/zod'; | ||
import { revalidatePath } from 'next/cache'; | ||
import { getTranslations } from 'next-intl/server'; | ||
|
||
import { localeSchema } from '@/vibes/soul/primitives/navigation/schema'; | ||
import { defaultLocale, redirect } from '~/i18n/routing'; | ||
|
||
export const switchLocale = async (_prevState: SubmissionResult | null, payload: FormData) => { | ||
const t = await getTranslations('Components.Header.Locale'); | ||
|
||
const submission = parseWithZod(payload, { schema: localeSchema }); | ||
|
||
if (submission.status !== 'success') { | ||
return submission.reply({ formErrors: [t('invalidLocale')] }); | ||
} | ||
|
||
await Promise.resolve(); | ||
|
||
revalidatePath('/'); | ||
|
||
// Since `redirect` doesn't prepend the local to the redirect url | ||
// when navigating the a default locale link, we need to prepend | ||
// it ourselves to ensure the redirect happens. | ||
if (submission.value.id === defaultLocale) { | ||
redirect({ | ||
href: `/${submission.value.id}`, | ||
locale: submission.value.id, | ||
}); | ||
} else { | ||
redirect({ | ||
href: `/`, | ||
locale: submission.value.id, | ||
}); | ||
} | ||
|
||
return submission.reply({ resetForm: true }); | ||
}; |
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