silverstripe / silverstripe-framework

Silverstripe Framework, the MVC framework that powers Silverstripe CMS

Home Page:https://www.silverstripe.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

lekoala opened this issue · comments

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

  • Check that there isn't already an issue that reports the same bug
  • Double check that your reproduction steps work in a fresh installation of silverstripe/installer (with any code examples you've provided)

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

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

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.

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 :)