phalcon/cphalcon · error · Phalcon\Translate\Exceptions\InvalidDataType
Translation data must be an array
Error message
Translation data must be an array
What it means
After confirming 'content' exists, NativeArray requires it to be an array; any other type throws Phalcon\Translate\Exceptions\InvalidDataType (phalcon/Translate/Adapter/NativeArray.zep:54). The adapter stores the value directly in its translate map, so scalars, null, or objects cannot be used.
Source
Thrown at phalcon/Translate/Adapter/NativeArray.zep:54
* @phpstan-param translate_array_options $options
*
* @throws InvalidDataType
* @throws MissingContent
*/
public function __construct(
<InterpolatorFactory> interpolator,
array options
) {
var data;
parent::__construct(interpolator, options);
if unlikely !fetch data, options["content"] {
throw new MissingContent();
}
if unlikely typeof data !== "array" {
throw new InvalidDataType();
}
let this->translate = data;
}
/**
* Check whether is defined a translation key in the internal array
*
* @deprecated
*/
public function exists(string index) -> bool
{
return this->has(index);
}
/**
* Check whether is defined a translation key in the internal array
*/View on GitHub (pinned to b7419de9cd)
Solutions
- Load the data first, then pass the array: ['content' => require $path] or the already-decoded array.
- Use json_decode($json, true) so objects become arrays.
- If what you have is a CSV file path, use the Csv adapter instead.
Example fix
// before
$t = new NativeArray($factory, ['content' => app_path('messages/en.php')]); // string path
// after
$t = new NativeArray($factory, ['content' => require app_path('messages/en.php')]); Defensive patterns
Strategy: type-guard
Validate before calling
if (!is_array($options['content'] ?? null)) {
throw new InvalidArgumentException('NativeArray content must be an array of translations');
} Type guard
/** @param mixed $content @return array<string,string> */
function ensureTranslationArray(mixed $content): array
{
return is_array($content) ? $content : [];
} Prevention
- Use require for PHP dictionaries and json_decode(..., true) for JSON — never pass paths or stdClass.
- Static-analyze factories with phpstan so content is known to be array<string,string>.
- If you have a file path, choose the Csv adapter instead.
When it happens
Trigger: new NativeArray($factory, ['content' => 'path/to/file.php']) — passing a file path like the Csv adapter instead of the array; content from json_decode($json) without the true flag (stdClass); include() returning int 1; a config value overwritten by a string.
Common situations: Mixing up adapter conventions (Csv takes a path, NativeArray takes the array); decoder flags lost in a refactor; env-specific config where one environment has a string instead of an array.
Related errors
- Translation content was not provided
- Cannot find translation key: {key}
- Translate is an immutable ArrayAccess object
- Parameter 'content' is required
- Error opening translation file '{file}'
AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21).
Data as JSON: /api/errors/cc80d7f4da64a66e.
Report an issue: GitHub.