symfony/translation · error · InvalidResourceException

Unable to load file " ".

Error message

Unable to load file "%s".

What it means

FileLoader::load() calls the subclass's loadResource() and requires the result to be an array; if loadResource() returns a non-array it throws InvalidResourceException('Unable to load file...'). This indicates the concrete loader produced malformed output for the resource.

Solutions

  1. Fix the loadResource() implementation in the concrete loader so it always returns an array (coerce null/false to []).
  2. Inspect the resource file for corruption or an unsupported format that makes the parser bail.
  3. If using a custom loader, unit-test loadResource() against empty and malformed files.
  4. Catch InvalidResourceException around load() to degrade to an empty catalogue.

Example fix

// before
protected function loadResource(string $resource): array
{
    return yaml_parse_file($resource); // null on failure
}
// after
protected function loadResource(string $resource): array
{
    return yaml_parse_file($resource) ?? [];
}
Defensive patterns

Strategy: try-catch

Validate before calling

// For custom loaders: ensure loadResource() always returns array
$result = $this->parse($resource);
if (!is_array($result)) { $result = []; }

Type guard

function isStringKeyedArray(mixed $v): bool { return is_array($v); }

Try / catch

try {
    $catalogue = $loader->load($path, $locale);
} catch (Symfony\Component\Translation\Exception\InvalidResourceException $e) {
    // fall back to empty catalogue and alert
    error_log('Translation load failed: '.$e->getMessage());
    $catalogue = new MessageCatalogue($locale);
}

Prevention

When it happens

Trigger: A concrete loader's loadResource() returns null, false, an object, or otherwise non-array data for the given file, then FileLoader::load() detects !is_array($messages) and throws.

Common situations: Custom loader subclass whose loadResource() returns null on parse failure; corrupted/unreadable file parsed into a non-array; PHP version behavior change where a parser returns null on empty content.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of symfony/translation@ae9e8a51bc (2026-09-15). Data as JSON: /api/errors/b1d854c0f03a8cca. Report an issue: GitHub.

Appendix: source

Thrown at Loader/FileLoader.php:41

{
    public function load(mixed $resource, string $locale, string $domain = 'messages'): MessageCatalogue
    {
        if (!stream_is_local($resource)) {
            throw new InvalidResourceException(\sprintf('This is not a local file "%s".', $resource));
        }

        if (!file_exists($resource)) {
            throw new NotFoundResourceException(\sprintf('File "%s" not found.', $resource));
        }

        $messages = $this->loadResource($resource);

        // empty resource
        $messages ??= [];

        // not an array
        if (!\is_array($messages)) {
            throw new InvalidResourceException(\sprintf('Unable to load file "%s".', $resource));
        }

        $catalogue = parent::load($messages, $locale, $domain);

        if (class_exists(FileResource::class)) {
            $catalogue->addResource(new FileResource($resource));
        }

        return $catalogue;
    }

    /**
     * @throws InvalidResourceException if stream content has an invalid format
     */
    abstract protected function loadResource(string $resource): array;
}

View on GitHub (pinned to ae9e8a51bc)