Skip to content

Commit

Permalink
Интернационализация
Browse files Browse the repository at this point in the history
  • Loading branch information
chiliec committed Jan 3, 2015
1 parent 2c0d1f4 commit f8e47c7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
9 changes: 8 additions & 1 deletion Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace chiliec\vote;

use yii\base\InvalidConfigException;
use Yii;

class Module extends \yii\base\Module
{
Expand All @@ -26,6 +27,12 @@ public function init()
if(!isset($this->matchingModels)) {
throw new InvalidConfigException('matchingModels not configurated');
}

if(empty(Yii::$app->i18n->translations['vote'])) {
Yii::$app->i18n->translations['vote'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en-US',
'basePath' => __DIR__ . '/messages',
];
}
}
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ In the above `pathMap` means that every view in `@chiliec/vote/views` will be fi
<div id="vote-<?=$model_name.$target_id;?>" style="text-align: center;">
<span id="vote-up-<?=$model_name.$target_id;?>" class="glyphicon glyphicon-thumbs-up" onclick="vote('<?=$model_name;?>',<?=$target_id;?>,'like'); return false;" style="cursor:pointer;"><?=$rating['likes'];?></span>&nbsp;
<span id="vote-down-<?=$model_name.$target_id;?>" class="glyphicon glyphicon-thumbs-down" onclick="vote('<?=$model_name;?>',<?=$target_id;?>,'dislike'); return false;" style="cursor:pointer;"><?=$rating['dislikes'];?></span>
<div id="vote-response-<?=$model_name.$target_id;?>">Aggregate rating: <?=$rating['aggregate_rating'];?></div>
<div id="vote-response-<?=$model_name.$target_id;?>"><?=\Yii::t('vote', 'Aggregate rating');?>: <?=$rating['aggregate_rating'];?></div>
</div>
```

Expand All @@ -104,7 +104,7 @@ For example, you can markup with [schema.org](http://schema.org/AggregateRating)
<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating" id="vote-response-<?=$model_name.$target_id;?>">
<meta itemprop="bestRating" content="10" />
<meta itemprop="worstRating" content="0" />
Aggregate rating: <span itemprop="ratingValue"><?=$rating['aggregate_rating'];?> based on <span itemprop="ratingCount"><?=$rating['likes']+$rating['dislikes'];?></span> reviews
<?=\Yii::t('vote', 'Aggregate rating');?>: <span itemprop="ratingValue"><?=$rating['aggregate_rating'];?> based on <span itemprop="ratingCount"><?=$rating['likes']+$rating['dislikes'];?></span> reviews
</div>
</div>
````
Expand Down
18 changes: 9 additions & 9 deletions controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,24 @@ public function actionIndex()
}

if($user_id==null) {
return ['content' => 'Пользователь не распознан', 'successfully' => false];
return ['content' => Yii::t('vote','The user is not recognized'), 'successfully' => false];
}

$model_id = Rating::getModelIdByName($model_name);
if(!is_int($model_id)) {
return ['content' => 'Модель не зарегистрирована!', 'successfully' => false];
return ['content' => Yii::t('vote','The model is not registered'), 'successfully' => false];
}

if($target_id==null) {
return ['content' => 'Цель не определена', 'successfully' => false];
return ['content' => Yii::t('vote', 'The purpose is not defined'), 'successfully' => false];
}

if($act=='like'){
$act = 1;
} elseif($act=='dislike') {
$act = 0;
} else {
return ['content' => 'Неправильное действие!', 'successfully' => false];
return ['content' => Yii::t('vote', 'Wrong action'), 'successfully' => false];
}

$isVoted = Rating::findOne(["model_id"=>$model_id, "target_id"=>$target_id, "user_id"=>$user_id]);
Expand All @@ -58,19 +58,19 @@ public function actionIndex()
if($newVote->save()) {
if($act===1) {
Yii::$app->cache->delete('likes'.$model_name.$target_id);
return ['content' => 'Голос принят. Не забудьте поделиться с друзьями!', 'successfully' => true];
return ['content' => Yii::t('vote', 'Your vote is accepted. Thanks!'), 'successfully' => true];
} else {
Yii::$app->cache->delete('dislikes'.$model_name.$target_id);
return ['content' => 'Вы правы, действительно паршивая история...', 'successfully' => true];
return ['content' => Yii::t('vote', 'Thanks for your opinion'), 'successfully' => true];
}
} else {
return ['content' => 'Ошибка валидации!', 'successfully' => false];
return ['content' => Yii::t('vote', 'Validation error'), 'successfully' => false];
}
} else {
return ['content' => 'Вы уже голосовали!', 'successfully' => false];
return ['content' => Yii::t('vote', 'You have already voted!'), 'successfully' => false];
}
} else {
throw new MethodNotAllowedHttpException('Попытка обмана', 405);
throw new MethodNotAllowedHttpException(Yii::t('vote', 'Forbidden method'), 405);
}
}
}
14 changes: 14 additions & 0 deletions messages/ru/vote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

return [
'The user is not recognized' => 'Пользователь не распознан',
'The model is not registered' => 'Модель не зарегистрирована!',
'The purpose is not defined' => 'Цель не определена',
'Wrong action' => 'Неправильное действие!',
'Your vote is accepted. Thanks!' => 'Голос принят. Спасибо!',
'Thanks for your opinion' => 'Спасибо за ваше мнение',
'Validation error' => 'Ошибка валидации',
'You have already voted!' => 'Вы уже голосовали!',
'Forbidden method' => 'Запрещенный метод',
'Aggregate rating' => 'Совокупный рейтинг'
];
2 changes: 1 addition & 1 deletion views/display.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div id="vote-<?=$model_name.$target_id;?>" style="text-align: center;">
<span id="vote-up-<?=$model_name.$target_id;?>" class="glyphicon glyphicon-thumbs-up" onclick="vote('<?=$model_name;?>',<?=$target_id;?>,'like'); return false;" style="cursor:pointer;"><?=$rating['likes'];?></span>&nbsp;
<span id="vote-down-<?=$model_name.$target_id;?>" class="glyphicon glyphicon-thumbs-down" onclick="vote('<?=$model_name;?>',<?=$target_id;?>,'dislike'); return false;" style="cursor:pointer;"><?=$rating['dislikes'];?></span>
<div id="vote-response-<?=$model_name.$target_id;?>">Aggregate rating: <?=$rating['aggregate_rating'];?></div>
<div id="vote-response-<?=$model_name.$target_id;?>"><?=\Yii::t('vote', 'Aggregate rating');?>: <?=$rating['aggregate_rating'];?></div>
</div>

0 comments on commit f8e47c7

Please sign in to comment.