Skip to content

Commit 02b7c67

Browse files
committed
init commit
1 parent b672411 commit 02b7c67

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

LanguageAsset.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace pceuropa\language-selection;
3+
4+
class LanguageAsset extends \yii\web\AssetBundle{
5+
public $sourcePath = '@vendor/pceuropa/yii2-language-selection-widget/assets';
6+
public $baseUrl = '@web';
7+
public $depends = [
8+
'yii\bootstrap\BootstrapAsset',
9+
];
10+
}

LanguageSelection.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
namespace pceuropa\language-selection\LanguageSelection;
3+
4+
use Yii;
5+
use yii\helpers\Html;
6+
use pceuropa\language-selection\LanguageAsset;
7+
8+
class LanguageSelection extends \yii\base\Widget {
9+
10+
public $language;
11+
public $url;
12+
public $languageParam;
13+
14+
public function init(){
15+
parent::init();
16+
if ($this->language === null) {
17+
$this->language = ['en'];
18+
}
19+
20+
if ($this->languageParam === null) {
21+
$this->languageParam = ['language'];
22+
}
23+
24+
LanguageAsset::register($this->view);
25+
$this->url = $this->view->assetBundles[LanguageAsset::className()]->baseUrl;
26+
}
27+
28+
public function run(){
29+
return $this->widgetRender();
30+
}
31+
32+
public function patchFlag($lang){
33+
return $this->url .'/images/flags/'.$lang.'.png';
34+
}
35+
36+
public function allFlag(){
37+
$items = '';
38+
39+
foreach ($this->language as $key => $value) {
40+
if (Yii::$app->language != $value){
41+
$img = Html::img( $this->patchFlag($value));
42+
$a = Html::a ($img , ['', $this->languageParam => $value] );
43+
$items .= Html::tag('li',$a );
44+
}
45+
}
46+
47+
return $items;
48+
}
49+
50+
public function widgetRender(){
51+
52+
return $this->render('language-selection', [
53+
'flags' => [
54+
'now' => $this->patchFlag(Yii::$app->language),
55+
'all' => $this->allFlag(),
56+
]
57+
]);
58+
}
59+
}
60+
61+
?>

composer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "pceuropa/yii2-language-selection-widget",
3+
"description": "Menu dropdown to select languages",
4+
"keywords": [
5+
"yii2",
6+
"extension",
7+
"dropmenu",
8+
"flag icons",
9+
"bootstrap menu"
10+
],
11+
"version": "1.0.0",
12+
"homepage": "https://github.com/pceuropa/yii2-language-selection-widget.git",
13+
"type": "yii2-extension",
14+
"license": "MIT",
15+
"minimum-stability": "dev",
16+
"authors": [
17+
{
18+
"name": "Rafal Marguzewicz",
19+
"email": "info@pceuropa.net",
20+
"homepage": "https://pceuropa.net/"
21+
}
22+
],
23+
"autoload": {
24+
"psr-4": {
25+
"pceuropa\\language-selection\\": ""
26+
}
27+
}
28+
}

views/language-selection.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
use yii\helpers\Html;
3+
$img = Html::img($flags['now']) . ' <span class="caret"></span>';
4+
?>
5+
<li class="dropdown">
6+
<?= Html::a ($img, ['#'], [ 'class' => 'dropdown-toggle', 'data-toggle' => 'dropdown', 'role' => 'button', 'aria-haspopup' => 'true', 'aria-expanded' => 'false', ] ); ?>
7+
<ul class="dropdown-menu court">
8+
<?= $flags['all']?>
9+
</ul>
10+
</li>
11+
12+
<?php $this->registerCss(".court { width: 50px; min-width:50px}"); ?>

0 commit comments

Comments
 (0)