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
- Fix the loadResource() implementation in the concrete loader so it always returns an array (coerce null/false to []).
- Inspect the resource file for corruption or an unsupported format that makes the parser bail.
- If using a custom loader, unit-test loadResource() against empty and malformed files.
- 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
- Return [] instead of null from custom loadResource() on parse failure.
- Unit-test custom loaders against empty and malformed files.
- Keep the parser and PHP/intl versions consistent across environments.
- Validate the file format before adding it to the translation resources.
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
- This is not a local file
- Error opening file " ".
- File " " not found.
- The Translator does not support the following options
- The file dumper needs a path option.
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)