Yii Framework Forum: Weird and inconsistent params substitutions in Yii:t - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Weird and inconsistent params substitutions in Yii:t Rate Topic: -----

#1 User is offline   MetaYii 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 393
  • Joined: 07-October 08
  • Location:The Matrix

Posted 24 October 2008 - 12:30 PM

Hi, I posted about this on the General Discussion, but I guess this is a bug, or it could cause bugs if one doesn't pay attention. From the helloworld demo:

<?php

class SiteController extends CController
{
public function actionIndex()
{
  $world = 'World Series';
echo Yii::t('demo', 'Hello world', array('world'=>$world));
}
}


Output:

[tt]Hello World Series[/tt]

This could be solved using this modified [tt]YiiBase::t[/tt] method:

<?php
public static function t($category,$message,$params=array())
{
if(self::$_app!==null)
{
$source=$category==='yii'?self::$_app->getCoreMessages():self::$_app->getMessages();
if($source!==null)
$message=$source->translate($category,$message);
}
if (is_array($params) && $params !== array()) {
  foreach ($params as $key => $val) {
      $message = str_replace('{' . $key . '}', $val, $message);
  }
}
    return $message;
}


with the benefit that you dont need to surround the key with [tt]{ }[/tt] in the params array, making it nicer. I don't think the performance impact would be so high.

0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,901
  • Joined: 04-October 08
  • Location:DC, USA

Posted 24 October 2008 - 12:34 PM

This is by design. Yes, it requires you to type in the whole placeholder (including the curly brackets if you use them). This makes it possible to use arbitrary placeholders instead of just the ones enclosed in brackets.
0

#3 User is offline   MetaYii 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 393
  • Joined: 07-October 08
  • Location:The Matrix

Posted 24 October 2008 - 04:05 PM

Quote

This is by design. Yes, it requires you to type in the whole placeholder (including the curly brackets if you use them). This makes it possible to use arbitrary placeholders instead of just the ones enclosed in brackets.


Hmm, oki. Maybe putting this clarification on the guide should be useful for pradoers :)

Thanks Qiang.
0

#4 User is offline   wei 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 147
  • Joined: 04-October 08

Posted 24 October 2008 - 06:06 PM

Agree, complete replacement is more consistent with PDO bind parameters as well.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users