extractor icon indicating copy to clipboard operation
extractor copied to clipboard

Error during translation extraction: "Form choice is not an array" and "Choice label is not a scalar string"

Open ovsep404 opened this issue 11 months ago • 0 comments

Description

When running the translation extractor (php bin/console translation:extract), I encounter the following errors:

  • Form choice is not an array
  • Choice label is not a scalar string

Image

These errors occur in my form types where dynamic choices are generated via function calls. For example, in my ProductType form I use:

->add('vendor', EntityType::class, [
    'class' => Vendor::class,
    'choices' => $this->vendorRepository->findBy($vendorFilters),
    'required' => false,
])

And in another form, I have:

$builder->add('state', ChoiceType::class, [
    'choices' => $this->enumChoicesWithAll(EquipmentState::class),
    'data' => AbstractRepository::FILTER_ALL_ID,
]);

Problem

The issue arises because:

  • findBy() returns an array of entities (T[]), but the translation extractor expects a static array literal (an instance of Node\Expr\Array_).
  • I think the extractor can only analyze static arrays defined directly in the code. It cannot process arrays created dynamically at runtime via method calls like findBy() or helper functions like enumChoicesWithAll().
  • When the extractor encounters these dynamic calls, it sees a Node\Expr\MethodCall or Node\Expr\FuncCall instead of a static array, which triggers the errors:
    • Form choice is not an array: The extractor cannot process the dynamically generated choices.
    • Choice label is not a scalar string: In similar dynamic cases, the extractor is unable to determine the label because it isn’t provided as a static scalar string.

The error is triggered in the following block of code in the translation extractor: FormTypeChoices.php:

} elseif (!$choices instanceof Node\Expr\Array_) {
    $this->addError($choices, 'Form choice is not an array');
    continue;
}

Here, the result of findBy() is treated as a method call (Node\Expr\MethodCall), not as a static array node, which leads to the error.

  • Form choice is not an array: The extractor cannot process the dynamically generated choices.
  • Choice label is not a scalar string: In similar dynamic cases, the extractor is unable to determine the label because it isn’t provided as a static scalar string.

Question

I am wondering if there is an alternative approach that would allow the translation extractor to handle dynamic choices without requiring the use of multiple @Ignore annotations. Has anyone encountered a similar issue or found a solution that would let the extractor process dynamically generated arrays (or provide a configurable fallback during extraction)?

Any guidance, suggestions, or workarounds would be greatly appreciated.

Thank you for your time and support!


ovsep404 avatar Feb 06 '25 18:02 ovsep404