Skip to content

Commit 84c9316

Browse files
committed
Add selector multilanguage.
1 parent 40f46ae commit 84c9316

File tree

13 files changed

+347
-2
lines changed

13 files changed

+347
-2
lines changed

config/messages.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111

1212
return [
1313
// string, required, root directory of all source files
14-
'sourcePath' => '@root/src',
14+
'sourcePath' => '@app/src',
1515
// array, required, list of language codes that the extracted messages
1616
// should be translated to. For example, ['zh-CN', 'de'].
1717
'languages' => ['es', 'ru', 'fr', 'pt'],
1818
// string, the name of the function for translating messages.
1919
// Defaults to 'Yii::t'. This is used as a mark to find the messages to be
2020
// translated. You may use a string for single function name or an array for
2121
// multiple function names.
22-
'translator' => ['\Yii::t'],
22+
'translator' => ['Yii::t'],
2323
// boolean, whether to sort messages by keys when merging new messages
2424
// with the existing ones. Defaults to false, which means the new (untranslated)
2525
// messages will be separated from the old (translated) ones.

config/params-web.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
'app.language' => 'en-US',
3737
'app.name' => 'Web application basic',
3838
'app.params' => [
39+
'app.languages' => [
40+
'en-US' => 'English',
41+
'es-ES' => 'Español',
42+
'fr-FR' => 'Français',
43+
'ru-RU' => 'Русский',
44+
],
3945
'app.mailer.sender' => 'noreply@example.com',
4046
'app.mailer.sender.name' => 'Web application basic',
4147
'app.menu.isguest' => [

config/web/bootstrap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
declare(strict_types=1);
44

55
use App\Framework\EventHandler\ContactEventHandler;
6+
use App\Framework\EventHandler\LanguageEventHandler;
67

78
return [
89
'bootstrap' => [
910
ContactEventHandler::class,
11+
LanguageEventHandler::class,
1012
],
1113
];

config/web/components.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use yii\i18n\PhpMessageSource;
6+
use App\Framework\Widget\languageSwitcher;
67

78
/**
89
* @var array $params
@@ -19,9 +20,13 @@
1920
'translations' => [
2021
'app.basic' => [
2122
'class' => PhpMessageSource::class,
23+
'basePath' => '@resource/message',
2224
],
2325
],
2426
],
27+
'languageSwitcher' => [
28+
'class' => languageSwitcher::class,
29+
],
2530
'request' => [
2631
'cookieValidationKey' => $params['app.request.cookieValidationKey'],
2732
'enableCsrfValidation' => $params['app.request.enableCsrfValidation'],
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Framework\Action;
6+
7+
use Yii;
8+
use yii\base\Action;
9+
use yii\web\Cookie;
10+
use yii\web\Request;
11+
12+
class LanguageAction extends Action
13+
{
14+
public function __construct(
15+
$id,
16+
$controller,
17+
private readonly Request $request,
18+
$config = []
19+
) {
20+
parent::__construct($id, $controller, $config);
21+
}
22+
public function run(): void
23+
{
24+
if ($this->request->isAjax === false) {
25+
return;
26+
}
27+
28+
$language = $this->request->post('language');
29+
Yii::$app->language = $language;
30+
31+
$cookie = new Cookie(
32+
[
33+
'name' => 'language',
34+
'value' => $language,
35+
'expire' => time() + 86400 * 365, // 1 year
36+
'httpOnly' => true,
37+
'sameSite' => 'strict',
38+
]
39+
);
40+
41+
Yii::$app->getResponse()->getCookies()->add($cookie);
42+
}
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Framework\EventHandler;
6+
7+
use Yii;
8+
use yii\base\BootstrapInterface;
9+
use yii\base\Event;
10+
use yii\web\Application;
11+
use yii\web\Cookie;
12+
13+
final class LanguageEventHandler implements BootstrapInterface
14+
{
15+
/**
16+
* @param Application $app
17+
*/
18+
public function bootstrap($app): void
19+
{
20+
Event::on(
21+
Application::class,
22+
Application::EVENT_BEFORE_REQUEST,
23+
static function (Event $event): void {
24+
if(php_sapi_name() === 'cli') {
25+
return;
26+
}
27+
28+
$languageNew = Yii::$app->request->get('language');
29+
30+
if($languageNew) {
31+
$event->sender->language = $languageNew;
32+
33+
$cookie = new Cookie(['name' => 'language', 'value' => $languageNew, 'httpOnly' => true, 'sameSite' => 'strict']);
34+
35+
Yii::$app->getResponse()->getCookies()->add($cookie);
36+
}
37+
else {
38+
$event->sender->language = Yii::$app->getRequest()->getCookies()->getValue('language', $_COOKIE['language'] ?? 'EN');
39+
}
40+
}
41+
);
42+
}
43+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Framework\Widget;
6+
7+
use Yii;
8+
use yii\base\Widget;
9+
use yii\bootstrap5\ButtonDropdown;
10+
use yii\helpers\Url;
11+
12+
final class LanguageSwitcher extends Widget
13+
{
14+
public function run(){
15+
$languages = Yii::$app->params['app.languages'];
16+
17+
$current = $languages[Yii::$app->language];
18+
unset($languages[Yii::$app->language]);
19+
20+
$items = [];
21+
foreach($languages as $code => $language)
22+
{
23+
$temp = [];
24+
$temp['label'] = $language;
25+
$temp['url'] = 'javascript:void(0);';
26+
$temp['linkOptions'] = [
27+
'class' => 'language-switch',
28+
'data-language' => $code,
29+
];
30+
31+
array_push($items, $temp);
32+
}
33+
34+
echo ButtonDropdown::widget(
35+
[
36+
'label' => $current,
37+
'buttonOptions' => [
38+
'class' => 'btn-secondary',
39+
],
40+
'dropdown' => [
41+
'items' => $items,
42+
],
43+
],
44+
);
45+
46+
$Js = <<<JS
47+
$(document).ready(function(){
48+
$('.language-switch').on('click', function (event) {
49+
event.preventDefault(); // Prevenir el evento click
50+
var selectedLanguage = $(this).data('language');
51+
$.ajax({
52+
url: '/site/language',
53+
type: 'POST',
54+
data: {language: selectedLanguage},
55+
success: function () {
56+
location.reload(); // Reload the page to apply the selected language
57+
}
58+
});
59+
});
60+
});
61+
JS;
62+
63+
$this->view->registerJs($Js);
64+
}
65+
}

src/Framework/resource/layout/footer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare(strict_types=1);
44

5+
use App\Framework\Widget\languageSwitcher;
56
use sjaakp\icon\Icon;
67
use yii\bootstrap5\Html;
78
use yii\web\View;
@@ -80,6 +81,10 @@
8081
['class' => 'ms-3'],
8182
) ?>
8283
<?= $this->render('component/toggle_dark') ?>
84+
<?= Html::tag(
85+
'li',
86+
languageSwitcher::Widget(),
87+
) ?>
8388
<?= Html::endTag('ul') ?>
8489
<?= Html::endTag('footer') ?>
8590
<?= Html::endTag('div') ?>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Message translations.
4+
*
5+
* This file is automatically generated by 'yii message/extract' command.
6+
* It contains the localizable messages extracted from source code.
7+
* You may modify this file by translating the extracted messages.
8+
*
9+
* Each array element represents the translation (value) of a message (key).
10+
* If the value is empty, the message is considered as not translated.
11+
* Messages that no longer need translation will have their translations
12+
* enclosed between a pair of '@@' marks.
13+
*
14+
* Message string can be used with plural forms format. Check i18n section
15+
* of the guide for details.
16+
*
17+
* NOTE: this file must be saved in UTF-8 encoding.
18+
*/
19+
return [
20+
'About' => 'Sobre nosotros',
21+
'Because the application is in development mode, the email is not sent but saved as a file under.' => '',
22+
'Body' => '',
23+
'Captcha Code' => '',
24+
'Contact' => '',
25+
'Contact us' => '',
26+
'Email' => '',
27+
'Enter Body Here' => '',
28+
'Enter Captcha Code Here' => '',
29+
'Enter Email Here' => '',
30+
'Enter Subject Here' => '',
31+
'Enter Username Here' => '',
32+
'If you think this is a problem with us, please tell us.' => '',
33+
'Index' => '',
34+
'Name' => '',
35+
'Note that if you turn on the Yii debugger, you should be able to view the mail message on the mail panel of the debugger.' => '',
36+
'Oops! Looks like you followed a bad link.' => '',
37+
'Please configure the <code>useFileTransport </code>property of the <code>mail </code>application component to be false to enable email sending.' => '',
38+
'Please fill out the following form to contact us.' => '',
39+
'Subject' => '',
40+
'Thank you for contacting us. We will respond to you as soon as possible.' => '',
41+
'This is the About page. You may modify the following file to customize its content.' => '',
42+
'Username' => '',
43+
];
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Message translations.
4+
*
5+
* This file is automatically generated by 'yii message/extract' command.
6+
* It contains the localizable messages extracted from source code.
7+
* You may modify this file by translating the extracted messages.
8+
*
9+
* Each array element represents the translation (value) of a message (key).
10+
* If the value is empty, the message is considered as not translated.
11+
* Messages that no longer need translation will have their translations
12+
* enclosed between a pair of '@@' marks.
13+
*
14+
* Message string can be used with plural forms format. Check i18n section
15+
* of the guide for details.
16+
*
17+
* NOTE: this file must be saved in UTF-8 encoding.
18+
*/
19+
return [
20+
'About' => '',
21+
'Because the application is in development mode, the email is not sent but saved as a file under.' => '',
22+
'Body' => '',
23+
'Captcha Code' => '',
24+
'Contact' => '',
25+
'Contact us' => '',
26+
'Email' => '',
27+
'Enter Body Here' => '',
28+
'Enter Captcha Code Here' => '',
29+
'Enter Email Here' => '',
30+
'Enter Subject Here' => '',
31+
'Enter Username Here' => '',
32+
'If you think this is a problem with us, please tell us.' => '',
33+
'Index' => '',
34+
'Name' => '',
35+
'Note that if you turn on the Yii debugger, you should be able to view the mail message on the mail panel of the debugger.' => '',
36+
'Oops! Looks like you followed a bad link.' => '',
37+
'Please configure the <code>useFileTransport </code>property of the <code>mail </code>application component to be false to enable email sending.' => '',
38+
'Please fill out the following form to contact us.' => '',
39+
'Subject' => '',
40+
'Thank you for contacting us. We will respond to you as soon as possible.' => '',
41+
'This is the About page. You may modify the following file to customize its content.' => '',
42+
'Username' => '',
43+
];
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Message translations.
4+
*
5+
* This file is automatically generated by 'yii message/extract' command.
6+
* It contains the localizable messages extracted from source code.
7+
* You may modify this file by translating the extracted messages.
8+
*
9+
* Each array element represents the translation (value) of a message (key).
10+
* If the value is empty, the message is considered as not translated.
11+
* Messages that no longer need translation will have their translations
12+
* enclosed between a pair of '@@' marks.
13+
*
14+
* Message string can be used with plural forms format. Check i18n section
15+
* of the guide for details.
16+
*
17+
* NOTE: this file must be saved in UTF-8 encoding.
18+
*/
19+
return [
20+
'About' => '',
21+
'Because the application is in development mode, the email is not sent but saved as a file under.' => '',
22+
'Body' => '',
23+
'Captcha Code' => '',
24+
'Contact' => '',
25+
'Contact us' => '',
26+
'Email' => '',
27+
'Enter Body Here' => '',
28+
'Enter Captcha Code Here' => '',
29+
'Enter Email Here' => '',
30+
'Enter Subject Here' => '',
31+
'Enter Username Here' => '',
32+
'If you think this is a problem with us, please tell us.' => '',
33+
'Index' => '',
34+
'Name' => '',
35+
'Note that if you turn on the Yii debugger, you should be able to view the mail message on the mail panel of the debugger.' => '',
36+
'Oops! Looks like you followed a bad link.' => '',
37+
'Please configure the <code>useFileTransport </code>property of the <code>mail </code>application component to be false to enable email sending.' => '',
38+
'Please fill out the following form to contact us.' => '',
39+
'Subject' => '',
40+
'Thank you for contacting us. We will respond to you as soon as possible.' => '',
41+
'This is the About page. You may modify the following file to customize its content.' => '',
42+
'Username' => '',
43+
];

0 commit comments

Comments
 (0)