Skip to content

Commit

Permalink
Merge pull request #14 from Ymox/master
Browse files Browse the repository at this point in the history
Handles Yii's i18n forceTranslation parameter globally
  • Loading branch information
uran1980 committed Nov 23, 2017
2 parents 3173e70 + 3731d6d commit 40b0f22
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 15 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ return [
'languages' => ['en', 'de', 'fr', 'it', 'es', 'pt', 'ru'],
// Or, if you manage languages in database
//'languages' => function() {
// /* /!\ Make sure the result is a mere list of language codes */
// return \namespace\of\your\LanguageClass::find()->select('code')->column();
// /* /!\ Make sure the result is a mere list of language codes, and the
// * one used in views is the first one */
// return \namespace\of\your\LanguageClass::find()->where(['active' => true'])->orderBy('default' => SORT_DESC])->select('code')->column();
//},
'format' => 'db',
'sourcePath' => [
Expand All @@ -63,11 +64,20 @@ return [
__DIR__ . '/../../common',
],
'messagePath' => __DIR__ . '/../../messages',
// Whether database messages are to be used instead of view ones.
// Enables editing messages in locale specified by
// Yii::$app->sourceLanguage
// Can be set per translation category too
//'forceTranslation' => true,
'translations' => [
'*' => [
'class' => yii\i18n\DbMessageSource::className(),
'enableCaching' => true,
'cachingDuration' => 60 * 60 * 2, // cache on 2 hours
// Whether database messages are to be used instead of view
// ones. Enables editing messages in view code locale.
// Can be set globally too.
//'forceTranslation' => true,
],
],
],
Expand Down
36 changes: 36 additions & 0 deletions components/I18N.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace uran1980\yii\modules\i18n\components;

use \yii\i18n\DbMessageSource;

class I18N extends \Zelenin\yii\modules\I18n\components\I18N
{
/**
Expand All @@ -22,6 +24,17 @@ class I18N extends \Zelenin\yii\modules\I18n\components\I18N
*/
public $translator = 'Yii::t';

/**
* boolean, whether to force translation or not.
* Defaults to false.In that case, the source message will be displayed as in
* the code instead of what you could have set in database. When set to true,
* the version in database will be used instead of the code one, and you will
* be able to edit the messages for the sourceLanguage locale
*
* @var boolean
*/
public $forceTranslation = false;

/**
* boolean, whether to sort messages by keys when merging new messages
* with the existing ones. Defaults to false, which means the new (untranslated)
Expand Down Expand Up @@ -131,6 +144,29 @@ public function init()
if (!is_array($this->languages)) {
throw new InvalidConfigException('i18n component [language] must be an array or a callable returning an array');
}
if (!is_bool($this->forceTranslation)) {
throw new InvalidConfigException('i18n component [forceTranslation] must be a boolean');
}
if (!empty($this->sourceLanguage) && !is_string($this->sourceLanguage)) {
throw new InvalidConfigException('i18n component [sourceLanguage] must be a non-empty string');
}

$translationDefaultParams = [
'class' => DbMessageSource::className(),
'sourceMessageTable' => $this->sourceMessageTable,
'messageTable' => $this->messageTable,
'on missingTranslation' => $this->missingTranslationHandler
];
if ($this->forceTranslation) {
$translationDefaultParams['forceTranslation'] = true;
}

if (!isset($this->translations['*'])) {
$this->translations['*'] = $translationDefaultParams;
}
if (!isset($this->translations['app']) && !isset($this->translations['app*'])) {
$this->translations['app'] = $translationDefaultParams;
}

parent::init();
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name" : "uran1980/yii2-translate-panel",
"description" : "Yii2 Translate Panel makes the translation of your application awesome!",
"version" : "0.1.29",
"version" : "0.1.30",
"type" : "yii2-extension",
"keywords" : [
"yii2",
Expand Down
6 changes: 6 additions & 0 deletions config/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
// array, required, list of language codes that the extracted messages
// should be translated to. For example, ['zh-CN', 'de'].
'languages' => ['en', 'de', 'fr', 'it', 'es', 'pt', 'ru'],
// boolean, whether to force translation or not.
// Defaults to false. In that case, the source message will be displayed as in
// the code instead of what you could have set in database. When set to true,
// the version in database will be used instead of the code one, and you will
// be able to edit the messages for the sourceLanguage locale
'forceTranslation' => false,
// string, the name of the function for translating messages.
// Defaults to 'Yii::t'. This is used as a mark to find the messages to be
// translated. You may use a string for single function name or an array for
Expand Down
23 changes: 14 additions & 9 deletions views/default/_message-tabs.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@
$languages = Yii::$app->i18n->languages;
foreach ( $languages as $lang ) {
$message = Yii::t($model->category, $model->message, [], $lang);
$message = ($model->message == $message && $lang != $languages[0])
? '' : $message;
$message = ($model->message == $message && Yii::$app->sourceLanguage == $lang)
? $model->message : $message;
$parameters = [
'id' => 'message-' . $lang . '-translation',
'class' => 'translation-textarea form-control',
'rel' => $lang,
'dir' => (in_array($lang, ['ar', 'fa']) ? 'rtl' : 'ltr'),
'rows' => 3,
];
if (!Yii::$app->i18n->translations[$model->category]->forceTranslation && Yii::$app->sourceLanguage == $lang) {
$parameters['disabled'] = 'disabled';
$parameters['title'] = Module::t('Please set [forceTranslation] to true to be able to edit this field');
}
$items[] = [
'label' => '<b>' . strtoupper($lang) . '</b>',
'content' => Html::textarea('Message[' . $lang . '][translation]', $message, [
'id' => 'message-' . $lang . '-translation',
'class' => 'translation-textarea form-control',
'rel' => $lang,
'dir' => (in_array($lang, ['ar', 'fa']) ? 'rtl' : 'ltr'),
'rows' => 3,
]) . Html::hiddenInput('categories[' . $lang . ']', $model->category),
'content' => Html::textarea('Message[' . $lang . '][translation]', $message, $parameters) . Html::hiddenInput('categories[' . $lang . ']', $model->category),
'active' => ($lang == Yii::$app->language) ? true : false,
];
}
Expand Down
13 changes: 10 additions & 3 deletions views/default/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@
</div>
<?php $form = ActiveForm::begin(); ?>
<div class="row">
<?php foreach ($model->messages as $language => $message) : ?>
<?php echo $form->field($model->messages[$language], '[' . $language . ']translation', ['options' => ['class' => 'form-group col-sm-6']])->textInput()->label(strtoupper($language)); ?>
<?php endforeach; ?>
<?php foreach ($model->messages as $language => $message):
$message->translation = ($model->message == $message && Yii::$app->sourceLanguage == $lang
? $model->message : $message->translation);
echo $form->field($message, '[' . $language . ']translation', ['options' => ['class' => 'form-group col-sm-6']])
->textInput(
(!Yii::$app->i18n->translations[$model->category]['forceTranslation'] && Yii::$app->sourceLanguage == $language)
? ['disabled' => 'disabled', 'title' => Module::t('Please set [forceTranslation] to true to be able to edit this field')]
: []
)->label(strtoupper($language));
endforeach; ?>
</div>
<div class="form-group">
<?php echo
Expand Down

0 comments on commit 40b0f22

Please sign in to comment.