symfony/translation · error · InvalidArgumentException
Invalid " " locale.
Error message
Invalid "%s" locale.
What it means
Static guard in LocaleFallbackProvider (Translation provider layer) that rejects locale strings containing characters outside [a-zA-Z0-9@_.-]. It is called when constructing the provider and when computing fallback locales, because an invalid locale would silently produce broken catalogue lookups.
Solutions
- Fix the locale string so it matches /^[a-z0-9@_\.\-]*$/i (e.g. 'en', 'en_US', 'fr@posix').
- If passing multiple locales, pass an array or split on a valid separator instead of embedding them in one string.
- Trim whitespace and strip query fragments from user-supplied locales before use.
- Call LocaleFallbackProvider::validateLocale($locale) yourself in config bootstrapping to fail early with a clearer message.
Example fix
// before new LocaleFallbackProvider($loader, 'en_US de_DE'); // after new LocaleFallbackProvider($loader, 'en_US');
Defensive patterns
Strategy: validation
Validate before calling
if (!preg_match('/^[a-z0-9@_\.\-]*$/i', $locale)) {
throw new \InvalidArgumentException("Locale '$locale' contains invalid characters");
} Type guard
function isValidLocale(string $locale): bool { return (bool) preg_match('/^[a-z0-9@_\.\-]*$/i', $locale); } Try / catch
try {
$provider = new LocaleFallbackProvider($loader, $locale);
} catch (\InvalidArgumentException $e) {
// sanitize/fix $locale before retrying
} Prevention
- Normalize and trim locale input from env vars and HTTP requests.
- Never pack multiple locales into one string; use arrays.
- Reuse LocaleFallbackProvider::validateLocale in your own config validation.
When it happens
Trigger: Passing a locale containing spaces, slashes, commas, uppercase-extended symbols or other invalid characters to new LocaleFallbackProvider(...), to fallback locale configuration (computeFallbackLocales), or via helpers like assertValidLocale.
Common situations: Environment variables or config containing 'en_US;de_DE' lists with semicolons, URLs accidentally used as locales, locales with spaces ('en US'), or user-supplied locale input injected into fallback configuration.
Understand the failure class
Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.
Related errors
AI-assisted analysis of symfony/translation@ae9e8a51bc (2026-09-15).
Data as JSON: /api/errors/2d43dceae823e13e.
Report an issue: GitHub.
Appendix: source
Thrown at LocaleFallbackProvider.php:92
if ($fallback === $originLocale) {
continue;
}
$locales[$fallback] = $fallback;
}
return array_keys($locales);
}
/**
* Asserts that the locale is valid, throws an Exception if not.
*
* @throws InvalidArgumentException If the locale contains invalid characters
*/
public static function validateLocale(string $locale): void
{
if (!preg_match('/^[a-z0-9@_\.\-]*$/i', $locale)) {
throw new InvalidArgumentException(\sprintf('Invalid "%s" locale.', $locale));
}
}
}
View on GitHub (pinned to ae9e8a51bc)