# Language switcher and hreflang

Every page and entry knows its own address in every language. A language switcher links from that map, and the site's hreflang tags come from the same one.

## The language map

A page type's view gets `page.locales`, and an entry page gets `locales`: one key per supported language. The value is the page's `slug`, `path` and `url` in that language, or `null` where the page does not exist or is not published there. In the editor's canvas an unpublished translation still counts; on the public site it never links.

A switcher links to the same page where it exists, and to that language's home otherwise. The blank starter does it in one component:

**Astro**

File: `src/components/LocaleNav.astro`

```astro
---
import { href } from "smoodly/astro/runtime";
// …
const codes = Object.keys(locales);
---
{codes.length > 1 && (
  <nav class="container locales" aria-label="Language">
    {codes.map((code) => <a href={href(locales[code]?.path ?? "/", code)} aria-current={code === locale ? "page" : undefined}>{code}</a>)}
  </nav>
)}
```

`href` from `smoodly/astro/runtime` adds the language prefix the site uses. With one language the map has one key and the component renders nothing, which is why the single-language scaffold shares the file.

**Next.js**

File: `components/LocaleNav.tsx`

```tsx
export function LocaleNav({ locale, locales }: { locale: string; locales: Record<string, LocaleAlternate | null> }) {
  const codes = Object.keys(locales);
  if (codes.length < 2) return null;
  return (
    <nav className="container locales" aria-label="Language">
      {codes.map((code) => (
        <a key={code} href={href(locales[code]?.path ?? "/", code)} aria-current={code === locale ? "page" : undefined}>{code}</a>
      ))}
    </nav>
  );
}
```

`href` from `smoodly/server.ts` adds the language prefix the site uses. With one language the map has one key and the component renders nothing, which is why the single-language scaffold shares the file.

## Why the page decides it

The links must land on the same page in the other language, so they come from the page on screen.

**Astro**

Every page view passes its own `locales` map to the layout, and the layout renders the header with the links. Astro renders the page before its layout, so the layout knows which page is on screen. [Header and footer](https://smoodly.io/docs/globals/header-and-footer.md) has the file.

**Next.js**

The switcher lives with the page, not in the layout, because a layout cannot know which page is on screen and the links must land on the same page in the other language. The Lorem Co. starter renders its header, links included, from every page type through `PageFrame`:

File: `components/PageFrame.tsx`

```tsx
  const header = await q().getGlobal("header", { locale });
  const codes = Object.keys(locales);
  const links: LocaleLink[] | undefined = codes.length > 1
    ? codes.map((code) => ({ code, url: href(locales[code]?.path ?? "/", code), current: code === locale }))
    : undefined;
```

[Header and footer](https://smoodly.io/docs/globals/header-and-footer.md) has the whole file.

## hreflang

**Astro**

`Head` from `smoodly/astro/components` writes one hreflang link per language where the page is published, from the same map the switcher reads. A single-language site gets none, only the page's canonical link. The links are absolute on `site` in `astro.config.ts`, which the starter reads from `SITE_URL`: set it to the site's real address, as [SEO](https://smoodly.io/docs/pages/seo.md) explains.

File: `src/layouts/Layout.astro`

```astro
    <Head of={head} fallback="Lorem Co." />
```

The visitor picks a language through the switcher; nothing redirects by browser language.

**Next.js**

`smoodlyMetadata` in the route files emits `alternates.languages` for every page and entry, from the same map the switcher reads: only the languages where the page has a URL, each through `href`. A single-language site emits none, only the page's canonical link. The links are absolute on the host in `SITE_URL`, which the installer sets to `http://localhost:3000`: set it to the site's real address, as [SEO](https://smoodly.io/docs/pages/seo.md) explains.

next-intl could derive hreflang and redirect visitors by browser language on its own, and the scaffold turns both off:

File: `i18n/routing.ts`

```ts
  alternateLinks: false,
  localeDetection: false,
```

Both assume a page has the same slug in every language. Once slugs translate, a derived hreflang points at a URL that does not exist, and a detection redirect keeps the English segments under `/fi/` and lands on a 404. So the visitor picks a language through the switcher, and the tags come from the content.

Next, [Frameworks](https://smoodly.io/docs/frameworks/astro.md).
