first
This commit is contained in:
commit
8538ab969a
434 changed files with 44051 additions and 0 deletions
14
apps/web/modules/i18n/lib/locale-cookie.ts
Normal file
14
apps/web/modules/i18n/lib/locale-cookie.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import "server-only";
|
||||
|
||||
import { config } from "@repo/config";
|
||||
import type { Locale } from "@repo/i18n";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
export async function getUserLocale() {
|
||||
const cookie = (await cookies()).get(config.i18n.localeCookieName);
|
||||
return cookie?.value ?? config.i18n.defaultLocale;
|
||||
}
|
||||
|
||||
export async function setLocaleCookie(locale: Locale) {
|
||||
(await cookies()).set(config.i18n.localeCookieName, locale);
|
||||
}
|
||||
10
apps/web/modules/i18n/lib/update-locale.ts
Normal file
10
apps/web/modules/i18n/lib/update-locale.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
"use server";
|
||||
|
||||
import { setLocaleCookie } from "@i18n/lib/locale-cookie";
|
||||
import type { Locale } from "@repo/i18n";
|
||||
import { revalidatePath } from "next/cache";
|
||||
|
||||
export async function updateLocale(locale: Locale) {
|
||||
await setLocaleCookie(locale);
|
||||
revalidatePath("/");
|
||||
}
|
||||
22
apps/web/modules/i18n/request.ts
Normal file
22
apps/web/modules/i18n/request.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import { getUserLocale } from "@i18n/lib/locale-cookie";
|
||||
import { routing } from "@i18n/routing";
|
||||
import { config } from "@repo/config";
|
||||
import { getMessagesForLocale } from "@repo/i18n";
|
||||
import { getRequestConfig } from "next-intl/server";
|
||||
|
||||
export default getRequestConfig(async ({ requestLocale }) => {
|
||||
let locale = await requestLocale;
|
||||
|
||||
if (!locale) {
|
||||
locale = await getUserLocale();
|
||||
}
|
||||
|
||||
if (!(routing.locales.includes(locale) && config.i18n.enabled)) {
|
||||
locale = routing.defaultLocale;
|
||||
}
|
||||
|
||||
return {
|
||||
locale,
|
||||
messages: await getMessagesForLocale(locale),
|
||||
};
|
||||
});
|
||||
20
apps/web/modules/i18n/routing.ts
Normal file
20
apps/web/modules/i18n/routing.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import { config } from "@repo/config";
|
||||
import { createNavigation } from "next-intl/navigation";
|
||||
import { defineRouting } from "next-intl/routing";
|
||||
|
||||
export const routing = defineRouting({
|
||||
locales: Object.keys(config.i18n.locales),
|
||||
defaultLocale: config.i18n.defaultLocale,
|
||||
localeCookie: {
|
||||
name: config.i18n.localeCookieName,
|
||||
},
|
||||
localePrefix: config.i18n.enabled ? "always" : "never",
|
||||
localeDetection: config.i18n.enabled,
|
||||
});
|
||||
|
||||
export const {
|
||||
Link: LocaleLink,
|
||||
redirect: localeRedirect,
|
||||
usePathname: useLocalePathname,
|
||||
useRouter: useLocaleRouter,
|
||||
} = createNavigation(routing);
|
||||
Loading…
Add table
Add a link
Reference in a new issue