silverstripe / silverstripe-framework

Silverstripe Framework, the MVC framework that powers Silverstripe CMS
https://www.silverstripe.org
BSD 3-Clause "New" or "Revised" License
720 stars 820 forks source link

text collector doesn't collect when parameters are passed as a variable #11268

Open lekoala opened 3 weeks ago

lekoala commented 3 weeks ago

Module version(s) affected

5.x

Description

the method collectFromCode doesn't collect if the third argument is a variable instead of a plain array. This makes using _t not very practical if you reuse injection arguments in multiple strings.

eg from my project

Here is some code

$metaDescription = _t(
    'Controller.Someentity',
    "Meet {name} {address}",
    $args
);

is never collected

    $metaDescription = _t(
      'Controller.Someentity',
      "Meet {name} {address}",
      [
          'name' => $office->Name,
          'city' => $city,
          'address' => $address->StreetAddress . " " . $address->PostalCode . ". " . $address->City
      ];
);

works fine

How to reproduce

In a controller, have

        $var = ['some' => 'var'];
        $ImNOTCollected = _t('PageController.ImNOTCollected', 'test {some}', $var);
        $ImCollected = _t('PageController.ImCollected', 'test {some}', ['some' => 'var']);

Run text collector

=> ImNOTCollected is not there

Possible Solution

No response

Additional Context

No response

Validations

lekoala commented 3 weeks ago

current fix if you have the issue, if you can live with a silly syntax ;-)

$ImCollectedAgain = _t('PageController.ImNOTCollected', 'test {some}', [...$var]);

GuySartorelli commented 3 weeks ago

I don't know that this is a bug so much as a desired enhancement. The text collector relies on what is currently a pretty crude hand-made static analysis. The capability to figure out what's in a variable would be a new capability, I think.

lekoala commented 3 weeks ago

well, at least it could complain or make some kind of error. Or at least be documented here https://docs.silverstripe.org/en/5/developer_guides/i18n/#usage-in-php-files at the moment it fails silently even though i have a _t function even a regex would do a better job :)