yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.23k stars 6.91k forks source link

After first call of yii::t() changing the sourceLanguage has not effect #8125

Closed gpoehl closed 8 years ago

gpoehl commented 9 years ago

Once the yii:t('app','something') has been called changing the sourceLanguage has no effect for following translations.

To verify this you might use this example: There is an app.php file in each of the folders de, en and it under frontend\messages with a content like this:

return [
    'Test' => 'English translation',
    ];

That's the view file to play around:

<?php
Yii::$app->sourceLanguage = 'de-DE';   // Try using other value.
echo "<br>Source language: " . Yii::$app->sourceLanguage;
echo "<br>Language: " . Yii::$app->language;
/**
 * Call yii::t() with any sourceLanguage
 * Uncomment this call to see the difference
 */
echo '<br>First Call of Yii::t(): ' . Yii::t('app', 'Test') . '<br>';
/**
 * Change the sourceLanguage to anything else.
 * Note that translation for the language which equals the sourceLanguage before
 * the first call of yii::t() doesn't take place. 
 */
Yii::$app->sourceLanguage = 'xx-XX';
echo '<h2>Translation after change of sourceLanguage to ' . Yii::$app->sourceLanguage . '</h2>';

$languages = [
    'it-IT',
    'it',
    'de-DE',
    'de',
    'en-US',
    'en-En'
];

foreach ($languages as $lang) {
    Yii::$app->language = $lang;
    echo '<br>' . Yii::$app->language . ' = ' . Yii::t('app', 'Test');
}
?>
gpoehl commented 9 years ago

As a consequence of the situation described above the following error occurs as well when rbac\DBManager::checkAccessRecursive is checking permission: rbac_admin

yii\i18n\PhpMessageSource::loadMessages

The message file for category 'yii' does not exist: D:\WebRoot\yii-advanced\vendor\yiisoft\yii2/messages/en-En/yii.php Fallback file does not exist as well: D:\WebRoot\yii-advanced\vendor\yiisoft\yii2/messages/en/yii.php

klimov-paul commented 9 years ago

This is expected. Setup of Application::sourceLanguage does not affect any already loaded MessageSource instances as they have thier own sourceLanguage, which is filled up as copy of the one from application during message source creation. If you wish sourceLanguage to be changed for any message source you should change it manually there.

Note: there is no sense of changing Application::sourceLanguage during aplication running, as it is "the language that the application is written in", which is unlikely to vary on any conditions.

gpoehl commented 9 years ago

@klimov-paul, thank's for your explanation. But I don't follow your arguments.

1) There might be many reasons to develop an application using more than one language. Even if you use only one language there might be external sources written in another language. Yii itself has options for modules and widgets to set the source language. So different languages between modules and widgets are already supported.

2) Documentation says clearly that the current application language is used for translations : Yii tries to load an appropriate translation according to the current application language from one of the message sources defined in the i18n application component. If you don't want to change the current behavior docs need to be adjusted.

3) MessageSource is used in many classes so that I don't know right out of the box what needs to be done to get the expected translations. I expected. So some more help would be greatly appreciated.

4) Breadcrumbs don't find category 'yii' when sourceLanguage is changed. For details see issue https://github.com/yiisoft/yii2/issues/8131.

cebe commented 9 years ago

if your source language varies among different sources you can configure it per message source: http://www.yiiframework.com/doc-2.0/yii-i18n-messagesource.html#$sourceLanguage-detail

gpoehl commented 9 years ago

@cebe, that's a very clever solution. Thank's a lot for pointing in the right direction. Without your help I wouldn't got it. Might be helpful for others to adjust the documentation. See my above comment (number 2) which suggests to change the application language on the fly. Instead you'd better work with different categories related to different sorce languages.