symfony/translation · error · InvalidResourceException
0
0
Error message
The file "%s" does not contain valid YAML:
What it means
The YAML parser raised a ParseException while parsing the translation file; the loader wraps it in an InvalidResourceException with this prefix and appends the parser's message. Additionally, if the parsed document is a non-array scalar, the loader rejects it as unloadable.
Solutions
- Read the chained ParseException message for the exact line and fix the YAML syntax (indentation, colons, quotes).
- Replace tab characters with spaces; YAML forbids tabs for indentation.
- Ensure the top-level structure is a mapping of keys to messages (an array when parsed).
- Validate the file with a YAML linter or yaml-lint before deploying.
Example fix
// before
messages:
hello: Hello # tab indentation
// after
messages:
hello: Hello # spaces Defensive patterns
Strategy: try-catch
Validate before calling
try {
\Symfony\Component\Yaml\Yaml::parseFile($resource);
} catch (\Symfony\Component\Yaml\Exception\ParseException $e) {
throw new \RuntimeException('Bad YAML in '.$resource.': '.$e->getMessage());
} Try / catch
try {
$catalogue = $loader->load($resource, $locale);
} catch (InvalidResourceException $e) {
if (str_contains($e->getMessage(), 'does not contain valid YAML')) {
$this->logger->error('YAML translation parse failure', ['file' => $resource, 'detail' => $e->getPrevious()?->getMessage()]);
}
throw $e;
} Prevention
- Use spaces, never tabs, for YAML indentation.
- Lint all translation YAML files in CI (yaml-lint or symfony console lint:yaml).
- Ensure top-level YAML is a mapping (key: value messages).
- Watch for duplicate keys and unquoted special characters like @, %, and colons in values.
When it happens
Trigger: Calling YamlFileLoader::loadResource() (via load()) on a .yaml file with syntax errors — bad indentation, tabs, duplicate keys, unquoted special characters — or a file whose top level parses to a scalar instead of an array.
Common situations: Tabs used for indentation; missing space after a colon; multi-document files; editors mangling encoding; a translation file containing just a string/number at top level.
Related errors
- Unable to load
- Loading translations from the YAML format requires the…
- Dumping translations in the YAML format requires the…
- Error parsing JSON:
- Unable to load " ".
AI-assisted analysis of symfony/translation@ae9e8a51bc (2026-09-15).
Data as JSON: /api/errors/d43f10a66c883718.
Report an issue: GitHub.
Appendix: source
Thrown at Loader/YamlFileLoader.php:42
*/
class YamlFileLoader extends FileLoader
{
private YamlParser $yamlParser;
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)