improved language support

This commit is contained in:
Sol Irvine 2023-11-13 14:50:14 +09:00
parent 7fa6ffbe2a
commit f19f7e65d8
2 changed files with 7 additions and 5 deletions

View File

@ -71,10 +71,10 @@ const noto = Noto_Serif_JP({
variable: '--font-noto' variable: '--font-noto'
}); });
export const SupportedLocales: SupportedLocale[] = ['ja', 'en']; const locales = ['en', 'ja'] as const;
export function generateStaticParams() { export function generateStaticParams() {
return SupportedLocales.map((locale) => ({ locale })); return locales.map((locale) => ({ locale }));
} }
export default async function RootLayout({ export default async function RootLayout({
@ -85,7 +85,7 @@ export default async function RootLayout({
params: { locale?: SupportedLocale }; params: { locale?: SupportedLocale };
}) { }) {
// Validate that the incoming `locale` parameter is valid // Validate that the incoming `locale` parameter is valid
const isValidLocale = SupportedLocales.some((cur: string) => cur === params?.locale); const isValidLocale = locales.some((cur: string) => cur === params?.locale);
if (!isValidLocale) notFound(); if (!isValidLocale) notFound();
if (params?.locale) { if (params?.locale) {

View File

@ -1,11 +1,12 @@
'use client'; 'use client';
import clsx from 'clsx'; import clsx from 'clsx';
import Link from 'next/link'; import { createSharedPathnamesNavigation } from 'next-intl/navigation';
import { usePathname } from 'next/navigation';
export type SupportedLocale = 'en' | 'ja'; export type SupportedLocale = 'en' | 'ja';
const locales = ['en', 'ja'] as const;
function removeItem<T>(arr: Array<T>, value: T): Array<T> { function removeItem<T>(arr: Array<T>, value: T): Array<T> {
const index = arr.indexOf(value); const index = arr.indexOf(value);
if (index > -1) { if (index > -1) {
@ -15,6 +16,7 @@ function removeItem<T>(arr: Array<T>, value: T): Array<T> {
} }
export const LanguageControl = ({ lang }: { lang?: SupportedLocale }) => { export const LanguageControl = ({ lang }: { lang?: SupportedLocale }) => {
const { Link, usePathname } = createSharedPathnamesNavigation({ locales });
const pathName = usePathname(); const pathName = usePathname();
const basePathName = () => { const basePathName = () => {