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

  1. Read the chained ParseException message for the exact line and fix the YAML syntax (indentation, colons, quotes).
  2. Replace tab characters with spaces; YAML forbids tabs for indentation.
  3. Ensure the top-level structure is a mapping of keys to messages (an array when parsed).
  4. 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

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


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)