Skip to content

Commit 8ef2b79

Browse files
authored
Add translator (#257)
1 parent cf15dc4 commit 8ef2b79

File tree

14 files changed

+119
-15
lines changed

14 files changed

+119
-15
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
name: PHP ${{ matrix.php }}-${{ matrix.os }}
3232

3333
env:
34-
extensions: fileinfo
34+
extensions: fileinfo, intl
3535
key: cache-v1
3636
YII_C3: true
3737

composer.json

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
},
2323
"require": {
2424
"php": "^8.0",
25+
"ext-intl": "*",
2526
"httpsoft/http-message": "^1.0.5",
2627
"psr/container": "^2.0",
2728
"psr/http-message": "^1.0",
@@ -47,11 +48,15 @@
4748
"yiisoft/log-target-file": "^2.0",
4849
"yiisoft/router": "^1.2",
4950
"yiisoft/router-fastroute": "^1.1",
51+
"yiisoft/translator": "^1.1",
52+
"yiisoft/translator-formatter-intl": "^1.0",
53+
"yiisoft/translator-message-php": "^1.0",
5054
"yiisoft/view": "^6.0",
5155
"yiisoft/yii-console": "^1.3",
5256
"yiisoft/yii-debug": "^3.0@dev",
5357
"yiisoft/yii-event": "^1.0",
5458
"yiisoft/yii-http": "^1.0",
59+
"yiisoft/yii-middleware": "dev-master",
5560
"yiisoft/yii-runner-console": "^1.0",
5661
"yiisoft/yii-runner-http": "^1.0",
5762
"yiisoft/yii-view": "^5.0"

config/common/i18n.php

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Yiisoft\I18n\Locale;
66

7+
/** @var $params array */
8+
79
return [
810
Locale::class => [
911
'class' => Locale::class,

config/common/router.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
->middleware(CsrfMiddleware::class)
1919
->middleware(FormatDataResponse::class)
2020
->addGroup(
21-
Group::create(null)
21+
Group::create('/{_language}')
2222
->routes(...$config->get('routes'))
2323
);
2424

config/common/translator.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Yiisoft\Aliases\Aliases;
6+
use Yiisoft\Translator\CategorySource;
7+
use Yiisoft\Translator\Formatter\Intl\IntlMessageFormatter;
8+
use Yiisoft\Translator\Message\Php\MessageSource;
9+
10+
/** @var array $params */
11+
12+
return [
13+
// Configure application CategorySource
14+
'translation.app' => static function (Aliases $aliases) use ($params) {
15+
return new CategorySource(
16+
$params['yiisoft/translator']['defaultCategory'],
17+
new MessageSource($aliases->get('@messages')),
18+
new IntlMessageFormatter(),
19+
);
20+
},
21+
];

config/params.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
use App\Command\Hello;
66
use App\ViewInjection\CommonViewInjection;
77
use App\ViewInjection\LayoutViewInjection;
8+
use App\ViewInjection\TranslatorViewInjection;
89
use Yiisoft\Definitions\Reference;
910
use Yiisoft\ErrorHandler\Middleware\ErrorCatcher;
1011
use Yiisoft\Router\Middleware\Router;
1112
use Yiisoft\Session\SessionMiddleware;
13+
use Yiisoft\Yii\Middleware\Locale;
1214
use Yiisoft\Yii\View\CsrfViewInjection;
1315

1416
return [
@@ -17,9 +19,16 @@
1719
'locale' => 'en',
1820
'name' => 'My Project',
1921
],
22+
'locale' => [
23+
'locales' => ['en' => 'en-US', 'ru' => 'ru-RU'],
24+
'ignoredRequests' => [
25+
'/debug**',
26+
],
27+
],
2028
'middlewares' => [
2129
ErrorCatcher::class,
2230
SessionMiddleware::class,
31+
Locale::class,
2332
Router::class,
2433
],
2534

@@ -29,7 +38,7 @@
2938
'@assets' => '@root/public/assets',
3039
'@assetsUrl' => '@baseUrl/assets',
3140
'@baseUrl' => '/',
32-
'@message' => '@root/resources/message',
41+
'@messages' => '@resources/messages',
3342
'@npm' => '@root/node_modules',
3443
'@public' => '@root/public',
3544
'@resources' => '@root/resources',
@@ -40,11 +49,23 @@
4049
],
4150
],
4251

52+
'yiisoft/translator' => [
53+
'locale' => 'en',
54+
'fallbackLocale' => 'en',
55+
'defaultCategory' => 'app',
56+
'categorySources' => [
57+
// You can add categories from your application and additional modules using `Reference::to` below
58+
// Reference::to(ApplicationCategorySource::class),
59+
Reference::to('translation.app'),
60+
],
61+
],
62+
4363
'yiisoft/yii-view' => [
4464
'injections' => [
4565
Reference::to(CommonViewInjection::class),
4666
Reference::to(CsrfViewInjection::class),
4767
Reference::to(LayoutViewInjection::class),
68+
Reference::to(TranslatorViewInjection::class),
4869
],
4970
],
5071

config/web/application.php

+7
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,11 @@
2020
'fallbackHandler' => Reference::to(NotFoundHandler::class),
2121
],
2222
],
23+
\Yiisoft\Yii\Middleware\Locale::class => [
24+
'__construct()' => [
25+
'locales' => $params['locale']['locales'],
26+
'ignoredRequests' => $params['locale']['ignoredRequests'],
27+
],
28+
'withEnableSaveLocale()' => [false],
29+
],
2330
];

resources/message/en-US/app.php

-5
This file was deleted.

resources/messages/en/app.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'site.hello' => 'Hello',
7+
'site.start_with' => 'Let\'s start something great with <strong>Yii3</strong>',
8+
'site.guide_remind' => 'Don\'t forget to check the guide',
9+
];

resources/messages/ru/app.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'site.hello' => 'Привет',
7+
'site.start_with' => 'Давайте создадим что-нибудь при помощи <strong>Yii3</strong>',
8+
'site.guide_remind' => 'Не забудьте просмотреть инструкцию',
9+
];

resources/views/site/index.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,26 @@
33
declare(strict_types=1);
44

55
/**
6-
* @var \Yiisoft\View\WebView $this
7-
* @var \App\ApplicationParameters $applicationParameters
6+
* @var WebView $this
7+
* @var TranslatorInterface $translator
8+
* @var ApplicationParameters $applicationParameters
89
*/
910

11+
use App\ApplicationParameters;
12+
use Yiisoft\Translator\TranslatorInterface;
13+
use Yiisoft\View\WebView;
14+
1015
$this->setTitle($applicationParameters->getName());
1116
?>
1217

1318
<div class="text-center">
14-
<h1>Hello!</h1>
19+
<h1><?= $translator->translate('site.hello')?>!</h1>
1520

16-
<p>Let's start something great with <strong>Yii3</strong>!</p>
21+
<p><?= $translator->translate('site.start_with')?>!</p>
1722

1823
<p>
1924
<a href="https://github.com/yiisoft/docs/tree/master/guide/en" target="_blank" rel="noopener">
20-
<i>Don't forget to check the guide.</i>
25+
<i><?= $translator->translate('site.guide_remind')?>.</i>
2126
</a>
2227
</p>
2328
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\ViewInjection;
6+
7+
use Yiisoft\Translator\TranslatorInterface;
8+
use Yiisoft\Yii\View\CommonParametersInjectionInterface;
9+
10+
final class TranslatorViewInjection implements CommonParametersInjectionInterface
11+
{
12+
public function __construct(private TranslatorInterface $translator)
13+
{
14+
}
15+
16+
public function getCommonParameters(): array
17+
{
18+
return [
19+
'translator' => $this->translator,
20+
];
21+
}
22+
}

tests/Acceptance/HomeCest.php

+8
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,12 @@ public function testIndexPage(AcceptanceTester $I): void
1515
$I->expectTo('see page home.');
1616
$I->see('Hello!');
1717
}
18+
19+
public function testIndexPageRu(AcceptanceTester $I): void
20+
{
21+
$I->wantTo('home page works.');
22+
$I->amOnPage('/ru/');
23+
$I->expectTo('see page home.');
24+
$I->see('Привет!');
25+
}
1826
}

tests/Acceptance/NotFoundHandlerCest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function about(AcceptanceTester $I): void
1313
$I->amOnPage('/about');
1414
$I->wantTo('see about page.');
1515
$I->see('404');
16-
$I->see('The page /about not found.');
16+
$I->see('The page /en/about not found.');
1717
$I->see('The above error occurred while the Web server was processing your request.');
1818
$I->see('Please contact us if you think this is a server error. Thank you.');
1919
}
@@ -23,7 +23,7 @@ public function aboutReturnHome(AcceptanceTester $I): void
2323
$I->amOnPage('/about');
2424
$I->wantTo('see about page.');
2525
$I->see('404');
26-
$I->see('The page /about not found.');
26+
$I->see('The page /en/about not found.');
2727
$I->see('The above error occurred while the Web server was processing your request.');
2828
$I->see('Please contact us if you think this is a server error. Thank you.');
2929
$I->click('Go Back Home');

0 commit comments

Comments
 (0)