Skip to content

Commit

Permalink
Merge pull request #13 from Ymox/master
Browse files Browse the repository at this point in the history
Simplifies the callable possibility
  • Loading branch information
uran1980 committed Nov 20, 2017
2 parents 1232da0 + cce974a commit 3173e70
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 52 deletions.
18 changes: 18 additions & 0 deletions components/I18N.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,22 @@ class I18N extends \Zelenin\yii\modules\I18n\components\I18N
*/
public $catalog = 'messages';

/**
* @throws InvalidConfigException
*/
public function init()
{
if (!$this->languages) {
throw new InvalidConfigException('You should configure i18n component [language]');
}

if (is_callable($this->languages)) {
$this->languages = call_user_func($this->languages);
}
if (!is_array($this->languages)) {
throw new InvalidConfigException('i18n component [language] must be an array or a callable returning an array');
}

parent::init();
}
}
87 changes: 43 additions & 44 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@
{
"name": "uran1980/yii2-translate-panel",
"description": "Yii2 Translate Panel makes the translation of your application awesome!",
"version": "0.1.28",
"type": "yii2-extension",
"keywords": [
"yii2",
"i18n",
"translation",
"translations",
"translate-panel"
],
"homepage": "https://github.com/uran1980/yii2-translate-panel",
"time": "2015-03-11 17:30",
"license": "MIT",
"authors": [
{
"name": "Ivan Yakovlev",
"email": "uran1980@gmail.com",
"homepage": "https://github.com/uran1980",
"role": "Developer"
}
],
"support": {
"issues": "https://github.com/uran1980/yii2-translate-panel/issues",
"source": "https://github.com/uran1980/yii2-translate-panel"
},
"require": {
"yiisoft/yii2": "~2",
"zelenin/yii2-i18n-module": "0.1.*",
"rmrevin/yii2-fontawesome": "*",
"uran1980/yii2-igrowl-widget": "*",
"uran1980/yii2-chosen-select": "*",
"uran1980/yii2-bootstrap-confirmation": "*",
"uran1980/yii2-jquery-essential": "*"
},
"autoload": {
"psr-4": {
"uran1980\\yii\\modules\\i18n\\": ""
}
},
"extra": {
"bootstrap": "uran1980\\yii\\modules\\i18n\\Bootstrap"
}
}
"name" : "uran1980/yii2-translate-panel",
"description" : "Yii2 Translate Panel makes the translation of your application awesome!",
"version" : "0.1.29",
"type" : "yii2-extension",
"keywords" : [
"yii2",
"i18n",
"translation",
"translations",
"translate-panel"
],
"homepage" : "https://github.com/uran1980/yii2-translate-panel",
"time" : "2015-03-11 17:30",
"license" : "MIT",
"authors" : [{
"name" : "Ivan Yakovlev",
"email" : "uran1980@gmail.com",
"homepage" : "https://github.com/uran1980",
"role" : "Developer"
}
],
"support" : {
"issues" : "https://github.com/uran1980/yii2-translate-panel/issues",
"source" : "https://github.com/uran1980/yii2-translate-panel"
},
"require" : {
"yiisoft/yii2" : "~2",
"zelenin/yii2-i18n-module" : "0.1.*",
"rmrevin/yii2-fontawesome" : "*",
"uran1980/yii2-igrowl-widget" : "*",
"uran1980/yii2-chosen-select" : "*",
"uran1980/yii2-bootstrap-confirmation" : "*",
"uran1980/yii2-jquery-essential" : "*"
},
"autoload" : {
"psr-4" : {
"uran1980\\yii\\modules\\i18n\\" : ""
}
},
"extra" : {
"bootstrap" : "uran1980\\yii\\modules\\i18n\\Bootstrap"
}
}
19 changes: 19 additions & 0 deletions controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Yii;
use yii\base\Model;
use uran1980\yii\modules\i18n\Module;
use uran1980\yii\modules\i18n\models\SourceMessage;
use uran1980\yii\modules\i18n\models\search\SourceMessageSearch;
use uran1980\yii\modules\i18n\helpers\AppHelper;
use yii\helpers\Html;
Expand Down Expand Up @@ -184,4 +185,22 @@ public function actionRestore($id)

return $response;
}

/**
* @param array|integer $id
* @return SourceMessage|SourceMessage[]
* @throws NotFoundHttpException
*/
protected function findModel($id)
{
$query = SourceMessage::find()->where('id = :id', [':id' => $id]);
$models = is_array($id)
? $query->all()
: $query->one();
if (!empty($models)) {
return $models;
} else {
throw new NotFoundHttpException(Module::t('The requested page does not exist'));
}
}
}
4 changes: 2 additions & 2 deletions models/SourceMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function attributeLabels()
return [
'id' => Module::t('ID'),
'category' => Module::t('Category'),
'message' => Module::t('Sourse Messages'),
'message' => Module::t('Source Messages'),
'translation' => Module::t('Message Translations'),
'status' => Module::t('Translation status')
];
Expand Down Expand Up @@ -92,7 +92,7 @@ public static function getCategories()
public function initMessages()
{
$messages = [];
foreach (Yii::$app->getI18n()->languages as $language) {
foreach (Yii::$app->i18n->languages as $language) {
if (!isset($this->messages[$language])) {
$message = new Message;
$message->language = $language;
Expand Down
7 changes: 2 additions & 5 deletions views/default/_message-tabs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
use yii\bootstrap\Tabs;

$items = [];
if (is_callable(Yii::$app->i18n->languages)) {
$languages = call_user_func(Yii::$app->i18n->languages);
} else {
$languages = Yii::$app->i18n->languages;
}

$languages = Yii::$app->i18n->languages;
foreach ( $languages as $lang ) {
$message = Yii::t($model->category, $model->message, [], $lang);
$message = ($model->message == $message && $lang != $languages[0])
Expand Down
2 changes: 1 addition & 1 deletion views/default/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<?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($language); ?>
<?php echo $form->field($model->messages[$language], '[' . $language . ']translation', ['options' => ['class' => 'form-group col-sm-6']])->textInput()->label(strtoupper($language)); ?>
<?php endforeach; ?>
</div>
<div class="form-group">
Expand Down

0 comments on commit 3173e70

Please sign in to comment.