-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Yii:t() variants #251
Comments
I'm for the following variant: // default category
\Yii::t('eat {stuff}', array(
'{stuff}' => 'meat',
));
// default category, 5.4
\Yii::t('eat {stuff}', [
'{stuff}' => 'meat'
]);
// non-default category
\Yii::t(array('category', 'eat {stuff}'), array(
'{stuff}' => 'meat'
));
// non-default category, 5.4
\Yii::t(['category', 'eat {stuff}'], [
'{stuff}' => 'meat'
]); |
I gave it a try implementing a variadic version of the YiiBase::t() method. Can be reviewed in my variadic-yii-t branch. While it allows very flexible invocation of the method, I think it lacks on the 'clean interface' requirement. Those parameter names are weird... |
Moving category and language into the params array might be another approach: \Yii::t('I like {stuff}', array(
'{stuff}' => 'meat',
'category' => 'myExt',
'language' => 'de_DE',
)); But I guess that makes message extraction hard to do (category needs to be extracted as well)... |
@samdark I'm with you, just don't know about the message extraction tool. Do you think it would be feasible? |
Current solution: \Yii::t('message', $params); // default category
\Yii::t('category|message', $params); // non-default category |
Array variant doesn't make it too hard to implement message extraction, I think. |
Moving to params isn't a reliable way when considering extraction tool. |
@bwoester why are they named |
@tonydspaniard lol, typo and c&p. Should have been |
If It also would be cool if Yii could detect a namespace from current app controller or command, so Do you have any plans for date & time formats? Something like this? |
@alanwillms the problem is that dots are common in messages. |
i think idea with separator is really bad. i vote for @samdark first case. Also need to consider modules or extensions translations, so it must be possible to set full path in the category, for example: Yii:t(array(
'category' => 'my/own/path/to/the/cat/MainCategory',
'translation message',
)) i dont like to set basePath every time, so this situation really need to be considered, in Yii1.1 it is made as "by design" so need this one in Yii2 too. |
What do you think about following variant: \Yii::tapp('message', $params); // app category
\Yii::t_app('message', $params); // perhaps this
\Yii::t('message', $params); // default category |
@samdark You're right, I forgot. But that's another problem, if I have the following message:
And then I decide to change it:
I would have to update all translation files or create a new key for the default language. I think people should be encouraged to use message ID's instead of the proper sentence:
|
@resurtm create little-different methods for same purpose, hm, i think not good. |
@Ragazzo, i meant |
ah, ok, got it. But anyway i think that samdark case is much better and explicit) |
@alanwillms if it's different message the key should be different. Else you can end up with outdated translations. |
@samdark or unused translations. Well, in the end this is left to the programmer to decide. I vote for your first usage example 😄 |
I don't quite like the array approach as it requires too much typing (8 more characters). The variadic solution is better, but could easily cause typos. I prefer to using two functions: |
@qiangxue but it is not very useful, it is only useful (1 param less) for the framework with default category |
Would it be possible to override |
What about having the $category as the last (3rd) parameter? The only con is if we want to specify the category but don't have params so the call would be like |
@mdomba what about $lang param? |
@Ragazzo It is useful in your main application code when your message category is "app", which should be the case most of the time (if not, we won't take time discussing about this.) @bwoester You can certainly override it or use your own shortcut function. Anyway, it is just a shortcut function. You can define whatever you want. The only concern is to let the message extraction tool recognize it. @mdomba Yes, this is a con. And because the category is set as the last parameter, it could be easily omitted, especially after you finish writing a complex |
There is one more option: public static function t($message, $category = 'app', $params = array())
{
if (is_array($category)) {
$params = $category;
$category = 'app';
}
// ... do main stuff here
} Usage: Yii::t('test', array('param1' => 'value1'));
Yii::t('test', 'app', array('param1' => 'value1'));
Yii::t('test', 'app'); |
This is the similar variadic approach. The drawback with variadic approaches is that it is difficult to document and can easily cause usage problems. For this particular approach you described, it conflicts with the choice format where the second parameter could be a scalar used to choose which plural form to use. |
Ok. Personally for me leaving everything as it was in 1.1 is well ( |
Mhm. Just another idea... Basically if I do this: namespace MyExtension;
class Yii extends yii\YiiBase
{
static function t( $message, $params=array(), $language=null ) {
return yii\YiiBase::t2_or_tt_or_tr( 'myExtension', $message, $params, $language );
}
}
class MyExtension {
// ...
} I could call // EDIT: |
I have to ask, how many of commenters had written a project with a desent amount of categories and text? I can say for sure - I have. Just one of my projects has 20 categories and 107 KB of text in single language translation file - and I have 7 languages. The message keyword idea is nice, but if you have to deal with the amount of text I have to deal (and I have to say that half the text is translated throught view static files system that Yii has), you understand at first glance, that without some special tools it's a pita. Right now I just give the generated file with 'source text' => '' and they translate the second part to the language required. How should I do that with message keywords or message ID - it's an open question. My 0.02$ |
@psihius Thank you for sharing your experience. It's true in serious projects, message categories are very important and the default I think we are close to the conclusion. Most likely we will keep the 1.1 version of t(). We may consider adding an additional method |
Some additional thoughts. Actually I had to deal with the issue when source message changes and I have to update it in translation. While thinking about that and keeping in mind, that I really don't wana deal with some kind of message id, a wild thought crossed my mind. @qiangxue thank you for considering my thoughts :) |
I think that a translation into any available language is a must, so as an easy category change and params array for translated message. The most flexible option I see is a Yii::t() to return an object: // Category — app, language — russian
Yii::t('app', 'ru')->do('message.in.category', array(
'{stuff}' => 'meat'
));
// Or all goes default
Yii::t()->do('message.in.category');
// The real benefit is felt when translating a set of strings
$t = Yii::t('app', 'ru');
$t->do('message.in.category');
$t->do('second.message.with.params', array(
'{stuff}' => 'meat'
));
// Or even like that
foreach (array('en', 'ru') as &$lang)
{
$t->setLanguage($lang)->do('message.in.category');
} Sure it's not the shortest option available, but I think it's the most flexible and functionally rich one. |
+1 to // is shortcut to Yii::$app->message component
Yii::t($message, array $params, $category = MessageSource::$defaultCategory);
// is shortcut to Yii::$app->coreMessages component to translate core messages
Yii::tt($message, array $params); |
I am also for keeping 'label1' => Yii::t('models', 'My cool label 1'),
'label2' => Yii::t('models', 'Another wild translation'), vs. 'label1' => Yii::t('My cool label 1', 'models'),
'label2' => Yii::t('Another wild translation', 'models'), Moving category between Yii::t($category, $message, $params = array(), $language = null);
Yii::tt($message, $params = array(), $language = null); // category is 'app' by default The name
@psihius sounds like a cool thing but I think this is a bit too much to put in the framework. Might be good to create an extension to MessageCommand that allows that. The tagging might also be done with PHP comments so it does not affect the code: Yii::t('app', 'Translate this!'/*#25*/) @slavcodev I'm afraid you are mixing something up here. discussion is not about 2 different methods for coreMessages and messages. And btw: there is no coreMessages vs. messages component in yii2 |
right, I mixed up with yii1.1, but on the syntax is correct, that's what I like the most. |
or add the feature to use a single file for all categories |
It is not, because you would have to specify
What for? You add categories to keep overview. Putting it all in one file after that you could have used just one category. |
Sorry, disagree on this. Category's primary use case is not keeping messages in separate files for overview purposes. There are tools that can help you with this problem, regardless how or where the data is saved. Category's primary use case is about having one and the same message twice in your source language, which needs to be translated differently, depending on its context. At least that's the way I see it. 😉 |
@cebe That's actually a great though about using comments for that. Yep, maybe an extension is in order - should be pretty straight forward. Such tagging will deal with the only annoying issue the yii 1.1 has with translations :) |
Related to http://www.yiiframework.com/forum/index.php/topic/43147-backward-compatibility-questions/
The thread suggests a couple of different variants about how to avoid the string mangling approach used in the current implementation of YiiBase::t() and L18N::translate().
Basic goals:
The text was updated successfully, but these errors were encountered: