简体   繁体   中英

PHP T_OBJECT_OPERATOR | Variable Set

Working on a project of translating website and I had chose this solution . I'm trying to accomplish something like :

$VAR1 = $translate->__('Word_To_Translate');

This, not works for me since, the result is directly shown in stdout of the webpage. Even so when trying to call $VAR1 no result is returned.

This is not easily possible with the class you've mentioned.

If you wish to edit the class so it'll return the value instead of echo ing it, you can edit class.translation.php , replace the two occurances of echo $str; with return $str; , and replace echo $this->lang[$this->language][$str]; with return $this->lang[$this->language][$str] (simply changing echo to return on both instances).

//$VAR1 delegating $VAR1 = $translate->__('Word_To_Translate');

//class.translation.php `class Translator {

private $language = 'en'; private $lang = array();

public function __construct($language){
    $this->language = $language;
}

private function findString($str) {
    if (array_key_exists($str, $this->lang[$this->language])) {
        return $this->lang[$this->language][$str];
        return;
    }
    return $str;
}

private function splitStrings($str) {
    return explode('=',trim($str));
}

public function __($str) {  
    if (!array_key_exists($this->language, $this->lang)) {
        if (file_exists($this->language.'.txt')) {
            $strings = array_map(array($this,'splitStrings'),file($this->language.'.txt'));
            foreach ($strings as $k => $v) {
                $this->lang[$this->language][$v[0]] = $v[1];
            }
            return $this->findString($str);
        }
        else {
            return $str;
        }
    }
    else {
        return $this->findString($str);
    }
}

}`

Switched the echo for a return

Thank you very much uri2x && Rizier123 .

For the moment looks that it is working solution.

Best wishes !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM