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

summaryrefslogtreecommitdiffhomepage
path: root/app/composables/useSiteLocale.ts
blob: b89fc6a418a1c84bdd10cfe13b86987ac973a186 (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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1

import {
  defaultLocale,
  localeOfPrefix,
  localePath,
  locales,
  pageOfPath,
  textDirection,
  type Locale,
} from "~/content/locales";
import { siteUrl } from "~/content/site";

/** The language of the current route; a path whose prefix names no language shows English. */
export function useSiteLocale() {
  const route = useRoute();
  const locale = computed<Locale>(() => {
    const segment = route.params.locale;
    return localeOfPrefix(typeof segment === "string" ? segment : undefined) ?? defaultLocale;
  });
  const page = computed(() => pageOfPath(route.path));
  const to = (path: string) => localePath(locale.value, path);
  return { locale, page, to };
}

/**
 * The head of a page: its title and description, the language and direction of the document,
 * and the address of the page in every language.
 */
export function usePageHead(text: () => { title: string; description: string }) {
  const { locale, page } = useSiteLocale();
  const url = (target: Locale) => `${siteUrl}${localePath(target, page.value)}`;
  useHead(() => ({
    htmlAttrs: { lang: locale.value, dir: textDirection(locale.value) },
    link: [
      { rel: "canonical", href: url(locale.value) },
      ...locales.map((target) => ({
        rel: "alternate" as const,
        hreflang: target,
        href: url(target),
      })),
      { rel: "alternate", hreflang: "x-default", href: url(defaultLocale) },
    ],
  }));
  useSeoMeta({
    title: () => text().title,
    description: () => text().description,
    ogTitle: () => text().title,
    ogDescription: () => text().description,
    ogUrl: () => url(locale.value),
    ogType: "website",
    ogSiteName: "Tazel",
    ogImage: `${siteUrl}/social.png`,
    twitterCard: "summary_large_image",
  });
}