Astro throws “The provided locale does not exist” when you pass a locale to an astro:i18n helper such as getRelativeLocaleUrl() or getAbsoluteLocaleUrl() that is not in your i18n.locales config. The check is exact: 'en-US' does not match 'en', and for custom locale objects you must pass one of its codes, not its path. Use a configured locale, or add the missing one to astro.config.mjs.
Key Takeaways
- The locale you pass must exactly match a string in
i18n.localesor a code in a locale object.- Matching is case-sensitive:
'pt-BR'and'pt-br'are different.- For
{ path: 'spanish', codes: ['es', 'es-AR'] }, pass'es', not'spanish'.- Values from URLs, cookies or
Accept-Languageneed validating before use.Astro.currentLocaleis always a valid value to pass back in.
The error
The provided locale does not exist.
The locale/path `de` does not exist in the configured `i18n.locales`.
Why it happens
The URL helpers look up the locale you pass in i18n.locales before building a path. Given this config:
// astro.config.mjs
import { defineConfig } from 'astro/config';
export default defineConfig({
i18n: {
defaultLocale: 'en',
locales: ['en', 'fr', { path: 'spanish', codes: ['es', 'es-AR'] }],
},
});
These calls fail:
getRelativeLocaleUrl('de'); // not configured
getRelativeLocaleUrl('en-US'); // only 'en' is configured
getRelativeLocaleUrl('spanish'); // that is the path; pass a code
And these work, returning /fr/about/ and /spanish/about/ (with your trailing slash setting):
getRelativeLocaleUrl('fr', 'about');
getRelativeLocaleUrl('es', 'about');
Fix: pass a configured locale
Use the exact values from your config. In a language switcher, loop over the locales instead of hard-coding them:
---
// src/components/LanguagePicker.astro
import { getRelativeLocaleUrl } from 'astro:i18n';
const languages = [
{ code: 'en', label: 'English' },
{ code: 'fr', label: 'Français' },
{ code: 'es', label: 'Español' },
];
---
<ul>
{languages.map(({ code, label }) => (
<li>
<a href={getRelativeLocaleUrl(code)} aria-current={Astro.currentLocale === code ? 'page' : undefined} lang={code}>
{label}
</a>
</li>
))}
</ul>
If a site really needs the new language, add it to i18n.locales and create its pages.
Fix: validate dynamic values
Locales read from a cookie, query string or the browser’s languages can be anything. Check them first:
// src/lib/locale.ts
const SUPPORTED = ['en', 'fr', 'es', 'es-AR'] as const;
type Locale = (typeof SUPPORTED)[number];
export function toLocale(value: string | undefined, fallback: Locale = 'en'): Locale {
return SUPPORTED.includes(value as Locale) ? (value as Locale) : fallback;
}
Then call getRelativeLocaleUrl(toLocale(cookieValue), 'about'). Inside a page, Astro.currentLocale and Astro.preferredLocale (on demand) already return configured values.
Our Astro i18n routing guide covers locale objects and fallbacks, the Astro config docs explain the config file, and the official astro:i18n reference lists every helper. For related i18n setup errors, see manual i18n without a middleware. Themes in our Astro themes catalog are single-language by default, so these helpers come in when you add locales.
FAQ
What locales can I pass to getRelativeLocaleUrl?
Any string in i18n.locales, or any entry in the codes array of a locale object. The match is exact and case-sensitive.
Why does en-US fail when en is configured?
Astro does not fall back from a region code to its language. Configure en-US as its own locale or as a code in a locale object, or pass en.
How do I map several codes to one URL in Astro?
Use a locale object: { path: 'spanish', codes: ['es', 'es-AR'] }. Both codes produce URLs under /spanish/.