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

summaryrefslogtreecommitdiffhomepage
path: root/apps/web/lib/locale.server.ts
blob: 977e16b805f5b2c6ba3ad15f44270834d6bbe616 (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 WITH AdditionRef-Progmasoft-Patent-Grant-1.1

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"));
}