move language config to i18n/request

This commit is contained in:
peintnermax
2024-10-11 11:16:34 +02:00
parent f87c2311a0
commit 76bb714e3a
3 changed files with 30 additions and 36 deletions

View File

@@ -1,5 +1,6 @@
"use client";
import { Lang, LANGS } from "@/i18n/request";
import { setLanguageCookie } from "@/lib/cookies";
import {
Listbox,
@@ -13,40 +14,6 @@ import { useLocale } from "next-intl";
import { useRouter } from "next/navigation";
import { useState } from "react";
interface Lang {
id: number;
name: string;
img: string;
code: string;
}
const LANGS: Lang[] = [
{
id: 1,
name: "English",
code: "en",
img: "/images/flags/us.png",
},
{
id: 2,
name: "Deutsch",
code: "de",
img: "/images/flags/de.png",
},
{
id: 3,
name: "Italiano",
code: "it",
img: "/images/flags/it.png",
},
{
id: 4,
name: "Español",
code: "es",
img: "/images/flags/es.png",
},
];
export function LanguageSwitcher() {
const currentLocale = useLocale();

View File

@@ -2,10 +2,36 @@ import deepmerge from "deepmerge";
import { getRequestConfig } from "next-intl/server";
import { cookies } from "next/headers";
export interface Lang {
name: string;
code: string;
}
export const LANGS: Lang[] = [
{
name: "English",
code: "en",
},
{
name: "Deutsch",
code: "de",
},
{
name: "Italiano",
code: "it",
},
{
name: "Español",
code: "es",
},
];
export const LANGUAGE_COOKIE_NAME = "NEXT_LOCALE";
export default getRequestConfig(async () => {
const fallback = "en";
const cookiesList = cookies();
const locale: string = cookiesList.get("NEXT_LOCALE")?.value ?? "en";
const locale: string = cookiesList.get(LANGUAGE_COOKIE_NAME)?.value ?? "en";
const userMessages = (await import(`../../locales/${locale}.json`)).default;
const fallbackMessages = (await import(`../../locales/${fallback}.json`))

View File

@@ -1,5 +1,6 @@
"use server";
import { LANGUAGE_COOKIE_NAME } from "@/i18n/request";
import { cookies } from "next/headers";
export type Cookie = {
@@ -30,7 +31,7 @@ export async function setLanguageCookie(language: string) {
const cookiesList = cookies();
await cookiesList.set({
name: "NEXT_LOCALE",
name: LANGUAGE_COOKIE_NAME,
value: language,
httpOnly: true,
path: "/",