symfony/translation · error · InvalidResourceException
Unable to load file " ".
Error message
Unable to load file "%s".
What it means
Thrown by the YamlFileLoader (translation resource loader) as an InvalidResourceException when a YAML translation file parses to valid YAML but the top-level value is not an array (it is a non-null scalar, e.g. a string or number). Translation catalogues must be mappings of message keys to translations, so any other top-level shape cannot be loaded.
Solutions
- Open the YAML file and make its top level a mapping (key: value lines) instead of a scalar.
- Check for accidental edits/merges that flattened the file structure.
- Verify the file registered as a translation resource is actually a translation catalogue file.
- Run `php -r "var_dump((new Symfony\Component\Yaml\Parser())->parseFile('messages.yaml', Yaml::PARSE_CONSTANT));"` to confirm the parsed root is an array.
Example fix
// before (messages.yaml) translations // after (messages.yaml) hello: Hello bye: Goodbye
Defensive patterns
Strategy: validation
Validate before calling
$parsed = (new \Symfony\Component\Yaml\Parser())->parseFile($file, Yaml::PARSE_CONSTANT);
if (null !== $parsed && !is_array($parsed)) {
throw new \InvalidArgumentException("$file must be a YAML mapping of translations");
} Type guard
if (!is_array($parsed)) { /* reject or coerce */ } Try / catch
try {
$translator->addResource('yaml', $file, 'en');
} catch (InvalidResourceException $e) {
// inspect $e->getMessage(); fix the YAML root node
} Prevention
- Keep translation YAML files as top-level key: value mappings.
- Lint translation files in CI with the YAML parser before deployment.
- Never register non-translation YAML files as translator resources.
When it happens
Trigger: Loading a translation resource (e.g. via Translatable / Translator->addResource or loadResource) where the YAML file parses successfully with Yaml::PARSE_CONSTANT but yields a non-null, non-array root node such as `foo` or `123` instead of a mapping.
Common situations: A translator edited a messages.yaml file and accidentally replaced the mapping with a single scalar line; an empty-ish file containing just a quoted string; a file that lost its structure after a bad merge; wrong file (e.g. a README or config YAML) registered as a translation resource.
Related errors
- Dumping translations in the YAML format requires the…
- The Translator does not support the following options
- The file dumper needs a path option.
- Unable to create directory
- No support implemented for dumping XLIFF version
AI-assisted analysis of symfony/translation@ae9e8a51bc (2026-09-15).
Data as JSON: /api/errors/275596fac95e9b88.
Report an issue: GitHub.
Appendix: source
Thrown at Loader/YamlFileLoader.php:46
protected function loadResource(string $resource): array
{
if (!isset($this->yamlParser)) {
if (!class_exists(YamlParser::class)) {
throw new LogicException('Loading translations from the YAML format requires the Symfony Yaml component.');
}
$this->yamlParser = new YamlParser();
}
try {
$messages = $this->yamlParser->parseFile($resource, Yaml::PARSE_CONSTANT);
} catch (ParseException $e) {
throw new InvalidResourceException(\sprintf('The file "%s" does not contain valid YAML: ', $resource).$e->getMessage(), 0, $e);
}
if (null !== $messages && !\is_array($messages)) {
throw new InvalidResourceException(\sprintf('Unable to load file "%s".', $resource));
}
return $messages ?: [];
}
}
View on GitHub (pinned to ae9e8a51bc)