Skip to content

Commit

Permalink
#2: Fixes multi-language support
Browse files Browse the repository at this point in the history
  • Loading branch information
greeflas committed Aug 15, 2017
1 parent 6ca68cc commit e848001
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 12 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
Email Templates Module Change Log
---------------------------------

1.1.1 August 15, 2017
---------------------
* Fix: multi-language support

1.1.0 August 1, 2017
------------------
--------------------
* Enh: adds Gii generator for email templates

1.0.2 July 27, 2017
----------------
-------------------
* Fix: bug with updating

1.0.1 June 22, 2017
Expand Down
5 changes: 3 additions & 2 deletions src/controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ public function actionIndex()
/**
* Create email template model
*
* @param null|string $lang
* @return string|\yii\web\Response
*/
public function actionCreate()
public function actionCreate($lang = null)
{
$request = Yii::$app->getRequest();
if ($request->getIsPost()) {
Expand All @@ -82,7 +83,7 @@ public function actionCreate()
}
}
$template = $this->_service->getModel();
$translation = $this->_service->getTranslationModel();
$translation = $this->_service->getTranslationModel(null, $lang);

return $this->render('create', compact([
'errros',
Expand Down
5 changes: 3 additions & 2 deletions src/services/DbService.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ public function getTranslationModel($modelId = null, $language = null)
return $model ?: new EmailTemplateTranslation($params);
}

return new EmailTemplateTranslation();
return new EmailTemplateTranslation([
'language' => $language ?: Yii::$app->language,
]);
}

/**
Expand All @@ -99,7 +101,6 @@ protected function processData(array $data)
if ($this->_template->load($data) && $this->_translation->load($data)) {
if ($this->_template->getIsNewRecord()) {
$this->_translation->templateId = 0;
$this->_translation->language = Yii::$app->language;
}
$validationErrors = [];
if (!$this->_template->validate()) {
Expand Down
7 changes: 7 additions & 0 deletions src/views/default/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use yii\widgets\ActiveForm;
use ymaker\email\templates\Module as TemplatesModule;
use vova07\imperavi\Widget as ImperaviRedactor;
use ymaker\email\templates\widgets\LanguagesList;

/**
* View file for CRUD backend controller
Expand Down Expand Up @@ -40,12 +41,18 @@
<?php endforeach; ?>
<?php endif; ?>
</div>
<div class="col-md-12">
<div class="pull-right">
<?= LanguagesList::widget(['currentLanguage' => $translation->language]) ?>
</div>
</div>
<div class="col-md-12">
<?php $form = ActiveForm::begin() ?>
<?= $form->field($template, 'key') ?>
<?= $form->field($translation, 'subject') ?>
<?= $form->field($translation, 'body')->widget(ImperaviRedactor::class) ?>
<?= $form->field($translation, 'hint') ?>
<?= $form->field($translation, 'language')->hiddenInput()->label(false) ?>
<?= Html::submitButton(
TemplatesModule::t('Create'),
['class' => 'btn btn-success']
Expand Down
6 changes: 6 additions & 0 deletions src/views/default/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use ymaker\email\templates\Module as TemplatesModule;
use ymaker\email\templates\widgets\LanguagesList;
use vova07\imperavi\Widget as ImperaviRedactor;

/**
Expand Down Expand Up @@ -40,6 +41,11 @@
<?php endforeach; ?>
<?php endif; ?>
</div>
<div class="col-md-12">
<div class="pull-right">
<?= LanguagesList::widget(['currentLanguage' => $translation->language]) ?>
</div>
</div>
<div class="col-md-12">
<?php $form = ActiveForm::begin() ?>
<?= $form->field($template, 'key')
Expand Down
4 changes: 4 additions & 0 deletions src/views/default/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use yii\helpers\Html;
use yii\helpers\Url;
use ymaker\email\templates\Module as TemplatesModule;
use ymaker\email\templates\widgets\LanguagesList;

/**
* View file for CRUD backend controller
Expand Down Expand Up @@ -36,6 +37,9 @@
Url::toRoute(['delete', 'id' => $template->id]),
['class' => 'btn btn-danger']
) ?>
<div class="pull-right">
<?= LanguagesList::widget(['currentLanguage' => $translation->language]) ?>
</div>
</div>
</div>
</div>
Expand Down
19 changes: 17 additions & 2 deletions src/widgets/LanguagesList.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @license BSD 3-Clause License
*/

namespace yii2deman\shop\common\widgets;
namespace ymaker\email\templates\widgets;

use Yii;
use yii\base\Widget;
Expand All @@ -19,6 +19,11 @@
*/
class LanguagesList extends Widget
{
/**
* @var string
*/
public $currentLanguage;

/**
* @var array
*/
Expand All @@ -33,6 +38,16 @@ public function init()
/** @var LanguageProviderInterface $provider */
$provider = Yii::$container->get(LanguageProviderInterface::class);
$this->_languages = $provider->getLanguages();

if (!empty($this->currentLanguage)) {
foreach ($this->_languages as $key => $language) {
if ($language['locale'] == $this->currentLanguage) {
$this->currentLanguage = $language;
unset($this->_languages[$key]);
break;
}
}
}
}

/**
Expand All @@ -41,7 +56,7 @@ public function init()
public function run()
{
return $this->render('languages', [
'languages' => $this->_languages
'languages' => $this->_languages,
]);
}
}
14 changes: 10 additions & 4 deletions src/widgets/views/languages.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@
\yii\bootstrap\BootstrapPluginAsset::register($this);
?>
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button"
id="languages" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<?= Yii::t('email-templates', 'Language') ?>
<button class="btn btn-default dropdown-toggle" type="button" id="email-template-languages"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="true">
<?php if (!empty($this->context->currentLanguage)): ?>
<?= $this->context->currentLanguage['label'] ?>
<?php else: ?>
<?= Yii::t('email-templates', 'Language') ?>
<?php endif; ?>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="languages">
<ul class="dropdown-menu" aria-labelledby="email-template-languages">
<?php foreach ($languages as $language): ?>
<li>
<?= Html::a($language['label'], Url::current(['lang' => $language['locale']])) ?>
Expand Down

0 comments on commit e848001

Please sign in to comment.