cancel
Showing results for 
Search instead for 
Did you mean: 

__ function returns Phrase object. Could this return string?

__ function returns Phrase object. Could this return string?

Feature request from bentideswell, posted on GitHub Jun 22, 2016

The function returns a \Magento\Framework\Phrase object that when echo'd, is converted to a string via the toString method. This breaks several integrations I am working on and also doesn't work properly with json_encode (its saved as an object rather than a string).

Is it possible to have have this method return a string? The majority of time this method is used, it will be echo'd straight away, meaning the object isn't required anyway. A second function could be created that returns the object and the __ function could call this second function and cast the result to a string.

Here is a rough example:

function __O()
{
    $argc = func_get_args();

    $text = array_shift($argc);
    if (!empty($argc) && is_array($argc[0])) {
        $argc = $argc[0];
    }

    return new \Magento\Framework\Phrase($text, $argc);
}

function __()
{
    return (string)__(func_get_args())
}

This is needed as the __ function cannot be overwritten or extended like all other publicly available methods in Magento.

14 Comments
rainbowtree
Occasional Visitor

I am also looking for an update! Would love to be able use the FishPig wordpress add-ons on Magento 2

I also am looking for an update. These extensions are critical to the workflow of all sites built by my Agency.

 

I am starting to move forward into Magento2 - but with serious hesitation without these extensions. I need to be able to offer a quality end product and if the Phrase object is truly not being used in the core then it is a simple matter to change things around -> force __() to return a string in all instances and include a separate function for access to the Phrase if a developer needs to do something with it.

 

For the record -> changing the developer-expected flow for standard PHP functionality is typically a big no-no. I would seriously reconsider going back to doing this the proper way. Especially as it is not just WordPress integration that it affects -> ANY integration with a third-party CMS or Framework will have problems because of this.

Kamil Szewczyk
M2 Certified

We're also looking at the problem presented in this thread, and we're also interested in any updates. Do you have any?

masoud_doroodza
Frequent Visitor

use getText() after translation like 

__('Item number')->getText()

vendor/magento/framework/Phrase.php