Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/[locale]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Matomo from "@/components/Matomo"

import { getLastDeployDate } from "@/lib/utils/getLastDeployDate"
import { getLocaleTimestamp } from "@/lib/utils/time"
import { toLanguageTag } from "@/lib/utils/url"

import Providers from "./providers"

Expand Down Expand Up @@ -66,7 +67,7 @@ export default async function LocaleLayout(props: {

return (
<html
lang={locale}
lang={toLanguageTag(locale)}
className={`${inter.variable} ${ibmPlexMono.variable}`}
suppressHydrationWarning
>
Expand Down
4 changes: 2 additions & 2 deletions app/sitemap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { MetadataRoute } from "next"

import { getFullUrl } from "@/lib/utils/url"
import { getFullUrl, toLanguageTag } from "@/lib/utils/url"

import { DEFAULT_LOCALE } from "@/lib/constants"

Expand All @@ -21,7 +21,7 @@ export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
"x-default": getFullUrl(DEFAULT_LOCALE, normalizedSlug),
...Object.fromEntries(
translatedLocales.map((locale) => [
locale,
toLanguageTag(locale),
getFullUrl(locale, normalizedSlug),
])
),
Expand Down
7 changes: 5 additions & 2 deletions src/lib/utils/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {

import { getTranslatedLocales } from "../i18n/translationRegistry"

import { getFullUrl } from "./url"
import { getFullUrl, toLanguageTag } from "./url"

import { routing } from "@/i18n/routing"

Expand Down Expand Up @@ -103,7 +103,10 @@ export const getMetadata = async ({
languages: {
"x-default": xDefault,
...Object.fromEntries(
localesForHreflang.map((loc) => [loc, getFullUrl(loc, slugString)])
localesForHreflang.map((loc) => [
toLanguageTag(loc),
getFullUrl(loc, slugString),
])
),
},
}),
Expand Down
12 changes: 12 additions & 0 deletions src/lib/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ export const slugify = (text: string): string => {
)
}

/**
* Converts an internal locale code to a valid BCP 47 language tag.
* Internal codes use lowercase region subtags (e.g. "pt-br", "zh-tw")
* for use as URL path segments, but HTML lang and hreflang attributes
* require uppercase region subtags per BCP 47 (e.g. "pt-BR", "zh-TW").
*/
export const toLanguageTag = (locale: string): string => {
const parts = locale.split("-")
if (parts.length === 2) return `${parts[0]}-${parts[1].toUpperCase()}`
return locale
}

export const normalizeUrlForJsonLd = (
locale: string | Lang | undefined,
pathWithoutLocale: string
Expand Down
Loading