Skip to content
This repository has been archived by the owner on Aug 12, 2023. It is now read-only.

Alexdev #70

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions ImperaviRedactorAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
class ImperaviRedactorAsset extends AssetBundle
{
public $language;
public $sourcePath = '@yii/imperavi/assets';
public $js = [
'redactor.js'
Expand All @@ -26,12 +27,17 @@ class ImperaviRedactorAsset extends AssetBundle
'yii\web\JqueryAsset'
];

public function init() {
$appLanguage = strtolower(substr(Yii::$app->language , 0, 2)); // First 2 letters
if($appLanguage != 'en' && file_exists(Yii::getAlias('@yii/imperavi/assets') . 'lang/' . $appLanguage . '.js')){
$this->js[] = 'lang/' . $appLanguage . '.js';
public function registerAssetFiles($view) {
$appLanguage = strtolower(substr(Yii::$app->language , 0, 2)); //First 2 letters

//check if the language configured or use appLanguage instead
$language = $this->language ? $this->language : $appLanguage;
$languageJs = 'lang/' . $language . '.js';
if($language != 'en'){
$this->js[] = $languageJs;
}
parent::init();

parent::registerAssetFiles($view);
}
}

36 changes: 4 additions & 32 deletions Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
use Yii;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\AssetBundle;

/**
* Imperavi Redactor Widget For Yii2 class file.
*
* @property array $plugins
* @property \yii\web\AssetBundle $assetBundle Imperavi Redactor asset bundle
*
* @author Veaceslav Medvedev <[email protected]>
* @author Alexander Makarov <[email protected]>
Expand Down Expand Up @@ -66,11 +64,6 @@ class Widget extends \yii\base\Widget
*/
public $value = '';

/**
* @var \yii\web\AssetBundle|null Imperavi Redactor Asset bundle
*/
protected $_assetBundle = null;

/**
* Initializes the widget.
* If you override this method, make sure you call the parent implementation first.
Expand All @@ -96,38 +89,20 @@ public function run()
echo Html::textarea($this->attribute, $this->value, $this->htmlOptions);
}

$this->registerRedactorAsset();
$appLanguage = strtolower(substr(Yii::$app->language , 0, 2)); //First 2 letters
//register asset and configure language from options
ImperaviRedactorAsset::register($this->getView())->language = $this->options['lang'] ?? $appLanguage;
$this->registerClientScript();
}

/**
* Registers Imperavi Redactor asset bundle
*/
protected function registerRedactorAsset()
{
$this->_assetBundle = ImperaviRedactorAsset::register($this->getView());
}

/**
* Returns current asset bundle
* @return \yii\web\AssetBundle current asset bundle for Redactor
*/
protected function getAssetBundle()
{
if (!($this->_assetBundle instanceof AssetBundle)) {
$this->registerRedactorAsset();
}

return $this->_assetBundle;
}

/**
* Registers Imperavi Redactor JS
*/
protected function registerClientScript()
{
$view = $this->getView();


/*
* Language fix
* @author <https://github.com/sim2github>
Expand All @@ -136,9 +111,6 @@ protected function registerClientScript()
$this->options['lang'] = strtolower(substr(Yii::$app->language, 0, 2));
}

// Kudos to yiidoc/yii2-redactor for this solution
$this->assetBundle->js[] = 'lang/' . $this->options['lang'] . '.js';

// Insert plugins in options
if (!empty($this->plugins)) {
$this->options['plugins'] = $this->plugins;
Expand Down