withastro/astro · error · AstroError

i18nNoLocaleFoundInPath

i18nNoLocaleFoundInPath

Error message

You tried to use an i18n utility on a path that doesn't contain any locale. You can use `pathHasLocale` first to determine if the path has a locale.

What it means

In the internal locale-matching loop (used to find a configured locale whose path matches a given input), if the loop completes without any locale string or locale object code matching the requested `locale`, Astro throws `i18nNoLocaleFoundInPath`. The message advises checking the path with `pathHasLocale` first.

Source

Thrown at packages/astro/src/i18n/index.ts:180

 * Given a locale (code), it returns its corresponding path
 * @param locale
 * @param locales
 */
export function getPathByLocale(locale: string, locales: Locales): string {
	for (const loopLocale of locales) {
		if (typeof loopLocale === 'string') {
			if (loopLocale === locale) {
				return loopLocale;
			}
		} else {
			for (const code of loopLocale.codes) {
				if (code === locale) {
					return loopLocale.path;
				}
			}
		}
	}
	throw new AstroError(i18nNoLocaleFoundInPath);
}

/**
 * A utility function that retrieves the preferred locale that correspond to a path.
 *
 * @param path
 * @param locales
 */
export function getLocaleByPath(path: string, locales: Locales): string {
	for (const locale of locales) {
		if (typeof locale !== 'string') {
			if (locale.path === path) {
				// the first code is the one that user usually wants
				const code = locale.codes.at(0);
				if (code === undefined) throw new AstroError(i18nNoLocaleFoundInPath);
				return code;
			}
		} else if (locale === path) {

View on GitHub (pinned to d081033d5f)

Solutions

  1. Call `pathHasLocale(path, locales)` (or equivalent membership check) before invoking the locale utility, and branch when it returns false.
  2. Ensure the path includes a locale segment that exactly matches a configured locale code or path.
  3. Normalize casing/spacing of the locale segment before lookup.
  4. Add or correct the locale in `i18n.locales` if it should be supported.

Example fix

// before
const locale = extractLocale(path, locales) // throws if path has no locale

// after
if (pathHasLocale(path, locales)) {
  const locale = extractLocale(path, locales)
} else {
  // handle locale-less path (e.g. default locale)
}
Defensive patterns

Strategy: type-guard

Validate before calling

import { pathHasLocale } from 'astro:i18n';
if (!pathHasLocale(path, locales)) {
  // skip locale utility, handle locale-less path
}

Type guard

function pathHasLocaleFast(path, locales) {
  return locales.some(l => {
    const seg = typeof l === 'string' ? l : l.path;
    return path === seg || path.startsWith('/' + seg);
  });
}

Try / catch

null

Prevention

When it happens

Trigger: Calling an i18n utility that resolves a locale against `locales` with a `locale`/path argument that matches none of the configured locales' codes or paths.

Common situations: Passing a URL path that has no locale segment to a utility that assumes one; a locale code casing or formatting mismatch (e.g. 'EN' vs 'en'); a path produced by an external source (CMS, redirect) that doesn't include the expected locale prefix.

Related errors


AI-assisted analysis of withastro/astro@d081033d5f (2026-08-12). Data as JSON: /api/errors/1d41d2d199756346. Report an issue: GitHub.