使用可翻译字符串中的占位符遍历对象/访问对象属性

A convenient way to translate dynamic text in applications is to use placeholders, like so:

$message = 'Hello I am {firstname}';
$translation = strtr($message, array('{firstname}', 'John'));

But I want it to be some more dynamicly and just pass a complete person object and the user of the application is free to use whatever he feels comfortable of the person object.

So, given the following object:

$customer = new tstCustomer();
$customer->firstname = 'testfirst';
$customer->lastname = 'testlast';
$customer->email = 'testemail';
class tstCustomer
{
    public $firstname;
    public $lastname;
    public $email;

    public function __construct(){}
}

I want my user to be able to create a translatable string like this:

$message = '{greetings} I am {customer->firstname} {customer->lastname} and my email address is {customer->email} {customer->notexisting}.';

I only have to pass the $customer object now, instead of all kinds of separate properties (and their possible placeholders). That also gives the possibility of object traversing.

echo translation($message, array('{greetings}' => 'Hello World', '{customer}' => $customer));
// Output:
// Hello World I am testfirst testlast and my email address is testemail . 

I've written the following function to accomplish above:

function translation($message, $placeholders = array())
{
    // catch all notices/warnings/errors due to non existent attributes
    $backupHandler = set_error_handler('strtrErrorHandler');

    foreach($placeholders as $placeholder => $value)
    {
        if(gettype($value) === 'string')
        {
            $message = strtr($message, array($placeholder => $value));
            continue;
        }

        if(gettype($value) !== 'object')
            continue;

        $trimmed_placeholder = trim($placeholder, '{}');

        $regex = '/\{'.$trimmed_placeholder.'[\-\>[\w]*]*\}/';
        $matches = array();
        preg_match_all($regex, $message, $matches);
        // $matches should look like:
        // array(1) { [0]=> array(3) { [0]=> string(21) "{customer->firstname}" [1]=> string(20) "{customer->lastname}" [2]=> string(17) "{customer->email}" } } 
        if(!isset($matches[0]))
            continue;

        foreach($matches[0] as $match)
        {
            $stringpath = trim($match, '{}');
            $traversal = substr($stringpath, strlen($trimmed_placeholder)+2); 
            try
            {
                $message = strtr($message, array($match => $value->{$traversal}));
            }
            catch(Exception $e)
            {
                // replace the placeholder with empty string
                $message = strtr($message, array($match => ''));
            }

        }
    } // foreach $placeholders

    set_error_handler($backupHandler);

    return $message;
}
// catch all and convert to a catchable Exception 
function strtrErrorHandler($severity, $message, $filename, $lineno) {
    Yii::log($message, 'error', 'backend.test.index');
    throw new Exception('');
}

Now, my questions:

  1. How safe will this be? (I hope object traversing is limited like this)
  2. Can this be accomplished better in terms of safety?
  3. Can this be accomplished better in terms of performance?
  4. Any other thoughts?
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐