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

  1. Load the data first, then pass the array: ['content' => require $path] or the already-decoded array.
  2. Use json_decode($json, true) so objects become arrays.
  3. 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

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


AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21). Data as JSON: /api/errors/cc80d7f4da64a66e. Report an issue: GitHub.