Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/apps/web/lib/locale.server.ts
blob: 029d361a650b2dff5a27396ff1d0fdf7369905c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// SPDX-License-Identifier: AGPL-3.0-or-later

import "server-only";
import { cookies, headers } from "next/headers";
import {
  isLocale,
  localeCookieName,
  negotiateLocale,
  type Locale,
} from "./localization";

export { localeCookieName };

/**
 * The language of the current request.
 *
 * A language the visitor chose is stored in a cookie and always wins. Without
 * that choice the page follows the language preference of the browser.
 */
export async function getLocale(): Promise<Locale> {
  const value = (await cookies()).get(localeCookieName)?.value;
  if (isLocale(value)) {
    return value;
  }
  return negotiateLocale((await headers()).get("accept-language"));
}