Yii Framework Forum: Yii::t() - reorder parameters - Yii Framework Forum

Jump to content

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

Yii::t() - reorder parameters Second argument $message should be on first place

#1 User is offline   ManInTheBox 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 75
  • Joined: 17-June 11

Posted 29 November 2011 - 06:10 AM

Hello everybody,

Recently I decided to define some shortcut global functions inspired by qiang's tutorial.

And I have something like this for Yii::t() shortcut:
function t($message, $category = 'myapp', $params = array(), $source = null, $language = null)
{
    return Yii::t($category, $message, $params, $source, $language);
}

And call it in code is so clean and beautiful:
echo t('Some message...');


But there is one issue if you use yiic MessageCommand to extract messages for translation.
Obviously you need to set translator varible in config file to be your custom translating function (in my case it is t) and that's all. But you won't get extracted messages.
That's because regex for matching your translator function expects $category to be first argument:
/\b'.$translator.'\s*\(\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*,\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*[,\)]/s


My suggestion for Yii 2.0 is to reorder parameters in Yii::t() so $message become first, and $category second. And of course to edit regex in MessageCommand.

Cheers
0

#2 User is offline   lubosdz 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 115
  • Joined: 25-July 10
  • Location:Slovakia, Bratislava

Posted 09 December 2011 - 06:34 PM

View PostManInTheBox, on 29 November 2011 - 06:10 AM, said:

My suggestion for Yii 2.0 is to reorder parameters in Yii::t() so $message become first, and $category second.


But params must also be bound to the message - so you also would need to set params prior to category, because they may change more frequently than category. So your proposal should be:

Yii::t($message, $params = array(), $category = 'main', $source = null, $language = null)


But I am not sure if this is good idea, sorry:-)
What if somebody does not like category="main"?
Yii extension: Captcha Extended

Greatest discoveries in 22nd century will be about the gravitation. | http://www.synet.sk | http://ipdf.sk
0

#3 User is offline   ManInTheBox 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 75
  • Joined: 17-June 11

Posted 11 December 2011 - 10:24 AM

Yes, think you're right about params. If someone doesn't like default category he always have chance to change it, or can make wrapper as explained in my previous post :)

View Postlubosdz, on 09 December 2011 - 06:34 PM, said:

But params must also be bound to the message - so you also would need to set params prior to category, because they may change more frequently than category. So your proposal should be:

Yii::t($message, $params = array(), $category = 'main', $source = null, $language = null)


But I am not sure if this is good idea, sorry:-)
What if somebody does not like category="main"?

0

#4 User is offline   Psih 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 115
  • Joined: 30-June 10

Posted 12 December 2011 - 04:32 AM

Moving the params is a bad idea. I have 3 multilingual projects with 15-20 translation categories containing a lot of text (one project has ~250KB of data PER language). I estimate my usage of the params at about 5-7%, but the usage of the category param is 100% - I specify it for EACH translation message we have.

So I would support switching the $message and $category params, but do not touch other params. This will make the development of small-scale applications witch do not need many categories for translations. But moving the params will be a major pain for everyone.
0

#5 User is offline   Jaggi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 90
  • Joined: 05-September 11

Posted 12 December 2011 - 05:33 PM

I don't see the logic behind this, its very easy and quick to overload this method with your own and put it any order you want. Params shouldn't be changed in methods as it'll cause backwards compatibility issues breaking existing applications and causing inconsistencies in documentation.
See my development site @ www.CodeTheInter.net (BETA)

Posted Image Posted Image

Quote

If you make it idiot proof, they'll build a better idiot
1

#6 User is offline   ekerazha 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 526
  • Joined: 10-October 08
  • Location:European Union

Posted 14 December 2011 - 06:32 PM

View PostJaggi, on 12 December 2011 - 05:33 PM, said:

I don't see the logic behind this, its very easy and quick to overload this method with your own and put it any order you want. Params shouldn't be changed in methods as it'll cause backwards compatibility issues breaking existing applications and causing inconsistencies in documentation.


I've read that backward compatibility isn't a priority for the new major version. The important thing is to offer an upgrade path (i.e. "please note that you have to change the params order for this and that").
Yii user #37
0

#7 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,788
  • Joined: 17-January 09
  • Location:Russia

Posted 15 December 2011 - 07:34 AM

Yes, that's true.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
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