Skip to content

Commit

Permalink
i18n integration
Browse files Browse the repository at this point in the history
  • Loading branch information
guga-grigolia committed Jan 17, 2017
1 parent 625ce8f commit a4c2489
Show file tree
Hide file tree
Showing 7 changed files with 354 additions and 1 deletion.
57 changes: 57 additions & 0 deletions controllers/I18nController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@
namespace centigen\i18ncontent\controllers;


use centigen\i18ncontent\helpers\BaseHelper;
use centigen\i18ncontent\models\I18nMessage;
use centigen\i18ncontent\models\I18nSourceMessage;
use centigen\i18ncontent\models\search\I18nSearch;
use centigen\i18ncontent\web\Controller;
use Yii;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;

class I18nController extends Controller
{
Expand All @@ -27,4 +34,54 @@ public function behaviors()
];
}


/**
* Lists all I18nMessage models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new I18nSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
Url::remember(Yii::$app->request->getUrl(), 'i18n-messages-filter');

$languages = ArrayHelper::map(
I18nMessage::find()->select('language')->distinct()->all(),
'language',
'language'
);
$categories = ArrayHelper::map(
I18nSourceMessage::find()->select('category')->distinct()->all(),
'category',
'category'
);

return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'languages' => $languages,
'categories' => $categories
]);
}

/**
* Creates a new Article model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new I18nSourceMessage();

$locales = BaseHelper::getAvailableLocales();

if ($model->load(Yii::$app->request->post(), null) && $model->save()) {
return $this->redirect(['index']);
}
return $this->render('create', [
'model' => $model,
'locales' => $locales
]);
}

}
59 changes: 58 additions & 1 deletion models/I18nSourceMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace centigen\i18ncontent\models;

use Yii;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use yii\helpers\StringHelper;

/**
* This is the model class for table "{{%i18n_source_message}}".
Expand All @@ -12,9 +15,11 @@
* @property string $message
*
* @property I18nMessage[] $i18nMessages
* @property ActiveRecord[]|array $translations
*/
class I18nSourceMessage extends \yii\db\ActiveRecord
class I18nSourceMessage extends ActiveRecord
{
public $newTranslations = [];
/**
* @inheritdoc
*/
Expand Down Expand Up @@ -53,4 +58,56 @@ public function getI18nMessages()
{
return $this->hasMany(I18nMessage::className(), ['id' => 'id']);
}


/**
* @author Guga Grigolia <[email protected]>
* @inheritdoc
*/
public function load($postData, $formName = null)
{
if (!parent::load($postData, $formName)) {
return false;
}

$className = StringHelper::basename(I18nMessage::className());
$translations = ArrayHelper::getValue($postData, $className);
$this->newTranslations = [];

$allValid = true;
if(!empty($translations)){
foreach ($translations as $loc => $modelData) {
$modelData['language'] = $loc;


$translation = $this->findTranslationByLocale($loc);

$this->newTranslations[] = $translation;
if (!$translation->load($modelData, '')) {
$allValid = false;
}
}
}

return $allValid;
}

/**
* Find PageTranslation object from `translations` array by locale
*
* @author Guga Grigolia <[email protected]>
* @param $locale
* @return ActiveRecord
*/
public function findTranslationByLocale($locale)
{
$translations = array_merge($this->newTranslations, $this->i18nMessages);
foreach ($translations as $translation) {
if ($translation->locale === $locale) {
return $translation;
}
}

return new I18nMessage();
}
}
68 changes: 68 additions & 0 deletions models/search/I18nSearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace centigen\i18ncontent\models\search;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\modules\i18n\models\I18nMessage;

/**
* I18nMessageSearch represents the model behind the search form about `backend\modules\i18n\models\I18nMessage`.
*/
class I18nSearch extends I18nMessage
{
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id'], 'integer'],
[['language', 'translation', 'sourceMessage', 'category'], 'safe'],
];
}

/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}

/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = I18nMessage::find()->with('sourceMessageModel')->joinWith('sourceMessageModel');

$dataProvider = new ActiveDataProvider([
'query' => $query
]);

if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}



$query->andFilterWhere([
'{{%i18n_source_message}}.id' => $this->id
]);

$query->andFilterWhere(['like', '{{%i18n_message}}.language', $this->language])
->andFilterWhere(['like', '{{%i18n_message}}.translation', $this->translation])
->andFilterWhere(['like', '{{%i18n_source_message}}.message', $this->sourceMessage])
->andFilterWhere(['like', '{{%i18n_source_message}}.category', $this->category]);


return $dataProvider;
}
}
85 changes: 85 additions & 0 deletions views/i18n/_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

use yii\bootstrap\Tabs;
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;

/* @var $this yii\web\View */
/* @var $model centigen\i18ncontent\models\I18nSourceMessage */
/* @var $categories array */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $locales array */
?>

<div class="article-form">

<?php $form = ActiveForm::begin(); ?>

<?php echo $form->field($model, 'category')->textInput(['maxlength' => 32]) ?>

<?php echo $form->field($model, 'message')->textarea(['rows' => 6]) ?>


<?php

if (isset($locales)) {
$items = [];
$ind = 0;
foreach ($locales as $key => $locale) {
$title = $locale;
$translationModel = $model->findTranslationByLocale($key);

$content = $this->render('_tab_content', [
'form' => $form,
'model' => $translationModel,
'language' => $key,
]);

$items[] = [
'label' => $title,
'content' => $content,
'headerOptions' => [
'title' => $translationModel->hasErrors() ? Yii::t('i18ncontent', 'You have validation errors') : "",
'class' => $translationModel->hasErrors() ? 'has-error' : '',
'data-toggle' => 'tooltip'
],
'options' => [
'class' => 'fade' . ($ind++ === 0 ? ' in' : '')
]
];
}
echo '<div class="tab-wrapper">';
echo Tabs::widget([
'items' => $items
]);
echo '</div>';
} else {
echo $this->render('_tab_content', [
'form' => $form,
'model' => $model
]);
}

?>


<div class="form-group pull-left margin-right-5">
<?php echo Html::submitButton(
$model->isNewRecord ? Yii::t('i18ncontent', 'Create') : Yii::t('i18ncontent', 'Update'),
['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>

<?php if(!$model->isNewRecord): ?>
<div class="form-group">
<?php echo Html::a(Yii::t('i18ncontent', 'Delete', []), ['delete', 'id' => $model->id],
[
'class' => 'btn btn-danger',
'data-confirm' => "Are you sure you want to delete this item?",
'data-method'=>"post",
'data-pjax' => "0"
]) ?>
</div>
<?php endif?>
</div>
17 changes: 17 additions & 0 deletions views/i18n/_tab_content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $language string */
/* @var $model centigen\i18ncontent\models\I18nMessage */
/* @var $form yii\bootstrap\ActiveForm */

$className = \yii\helpers\StringHelper::basename(\centigen\i18ncontent\models\I18nMessage::className());

?>
<?php echo $form->field($model, 'translation', [
'inputOptions' => [
'name' => "{$className}[$language][translation]"
]
])->textarea(['maxlength' => 512]) ?>
19 changes: 19 additions & 0 deletions views/i18n/create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/* @var $this yii\web\View */
/* @var $model centigen\i18ncontent\models\I18nSourceMessage */
/* @var $locales array */

$this->title = Yii::t('i18ncontent', 'Create {modelClass}', [
'modelClass' => 'i18n',
]);
$this->params['breadcrumbs'][] = ['label' => Yii::t('i18ncontent', 'i18n'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="article-create">

<?php echo $this->render('_form', [
'model' => $model,
'locales' => $locales
]) ?>

</div>
50 changes: 50 additions & 0 deletions views/i18n/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

use backend\modules\i18n\models\search\I18nMessageSearch;
use yii\helpers\Html;
use yii\grid\GridView;
/**
* @var $this yii\web\View
* @var $searchModel I18nMessageSearch
* @var $languages array
* @var $categories array
* @var $dataProvider yii\data\ActiveDataProvider
*/

$this->title = Yii::t('backend', 'I18n Messages');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="i18n-message-index">

<?php //echo $this->render('_search', ['model' => $searchModel]); ?>

<p>
<?php echo Html::a(Yii::t('backend', 'Create {modelClass}', [
'modelClass' => 'I18n Message',
]), ['create'], ['class' => 'btn btn-success']) ?>
</p>

<?php echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'options' => [
'class' => 'grid-view table-responsive'
],
'columns' => [

'id',
[
'attribute'=>'language',
'filter'=> $languages
],
[
'attribute'=>'category',
'filter'=> $categories
],
'sourceMessage',
'translation:ntext',
['class' => 'yii\grid\ActionColumn', 'template'=>'{update} {delete}'],
],
]); ?>

</div>

0 comments on commit a4c2489

Please sign in to comment.