Skip to content

Commit 2b1a526

Browse files
author
Andrey Helldar
committed
Added the ability to translate the interface
1 parent 0c71c0c commit 2b1a526

File tree

10 files changed

+235
-29
lines changed

10 files changed

+235
-29
lines changed

config/pretty-routes.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,14 @@
7272
*/
7373

7474
'domain_force' => false,
75+
76+
/*
77+
* In the case when you need to use a specific localization, set its name to the value.
78+
*
79+
* For example "de".
80+
*
81+
* Otherwise, leave the value "false".
82+
*/
83+
84+
'locale_force' => false,
7585
];

resources/lang/en/info.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
return [
4+
'action' => 'Action',
5+
'deprecated' => 'Deprecated',
6+
'domain' => 'Domain',
7+
'itemsPerPageAllText' => 'All',
8+
'itemsPerPageText' => 'Routes per page',
9+
'loading' => 'Loading... Please wait...',
10+
'methods' => 'Methods',
11+
'middlewares' => 'Middlewares',
12+
'module' => 'Module',
13+
'name' => 'Name',
14+
'noDataText' => 'No data available',
15+
'noResultsText' => 'No matching records found',
16+
'of' => 'of',
17+
'only' => 'Only',
18+
'openGtHiub' => 'Open the project page on GitHub',
19+
'pageText' => '{0}-{1} of {2}',
20+
'path' => 'Path',
21+
'priority' => 'Priority',
22+
'refreshRoutes' => 'Refresh the list of routes',
23+
'search' => 'Search',
24+
'show' => 'Show',
25+
'title' => 'Routes',
26+
'without' => 'Without',
27+
];

resources/lang/ru/info.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
return [
4+
'action' => 'Экшены',
5+
'deprecated' => 'Устаревшие',
6+
'domain' => 'Домен',
7+
'itemsPerPageAllText' => 'Все',
8+
'itemsPerPageText' => 'Записей на странице',
9+
'loading' => 'Загрузка... Пожалуйста, подождите...',
10+
'methods' => 'Методы',
11+
'middlewares' => 'Мидлвари',
12+
'module' => 'Модуль',
13+
'name' => 'Название',
14+
'noDataText' => 'Данные отсутствуют',
15+
'noResultsText' => 'Элементы не найдены',
16+
'of' => 'из',
17+
'only' => 'Только',
18+
'openGtHiub' => 'Открыть страницу проекта в GitHub',
19+
'pageText' => '{0}-{1} из {2}',
20+
'path' => 'URI',
21+
'priority' => 'Приоритет',
22+
'refreshRoutes' => 'Обновить список маршрутов',
23+
'search' => 'Поиск',
24+
'show' => 'Показать',
25+
'title' => 'Маршруты',
26+
'without' => 'Не указано',
27+
];

resources/views/layout.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
55

6-
<title>{{ trans('Routes list') }} | {{ config('app.name') }}</title>
6+
<title>{{ \PrettyRoutes\Facades\Trans::get('title') }} | {{ config('app.name') }}</title>
77

88
<link rel="dns-prefetch" href="https://fonts.googleapis.com">
99
<link rel="dns-prefetch" href="https://cdn.jsdelivr.net">

resources/views/scripts.blade.php

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,7 @@
44
<script src="https://cdn.jsdelivr.net/npm/lodash"></script>
55

66
<script>
7-
const trans = {
8-
action: '{{ trans("Action") }}',
9-
deprecated: '{{ trans("Deprecated") }}',
10-
domain: '{{ trans("Domain") }}',
11-
itemsPerPageAllText: '{{ trans("All") }}',
12-
itemsPerPageText: '{{ trans("Routes per page:") }}',
13-
loading: '{{ trans("Loading... Please wait...") }}',
14-
methods: '{{ trans("Methods") }}',
15-
middlewares: '{{ trans("Middlewares") }}',
16-
module: '{{ trans("Module") }}',
17-
name: '{{ trans("Name") }}',
18-
noDataText: '{{ trans("No data available") }}',
19-
noResultsText: '{{ trans("No matching records found") }}',
20-
of: '{{ trans("of") }}',
21-
only: '{{ trans("Only") }}',
22-
openGitHub: '{{ trans('Open the project page on GitHub') }}',
23-
pageText: '{0}-{1} {{ trans("of") }} {2}',
24-
path: '{{ trans("Path") }}',
25-
priority: '{{ trans("Priority") }}',
26-
refreshRoutes: '{{ trans("Refresh the list of routes") }}',
27-
search: '{{ trans("Search") }}',
28-
show: '{{ trans("Show") }}',
29-
title: '{{ trans("Routes") }}',
30-
without: '{{ trans("Without") }}'
31-
};
7+
const trans = {!! json_encode(\PrettyRoutes\Facades\Trans::all(), JSON_UNESCAPED_UNICODE) !!};
328
339
const colorScheme = () => {
3410
switch ('{{ config('pretty-routes.color_scheme', 'auto') }}') {

src/Facades/Trans.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace PrettyRoutes\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
use PrettyRoutes\Support\Trans as Support;
7+
8+
/**
9+
* @method static array all()
10+
* @method static string get(string $key)
11+
*/
12+
final class Trans extends Facade
13+
{
14+
protected static function getFacadeAccessor()
15+
{
16+
return Support::class;
17+
}
18+
}

src/ServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ protected function isDisabled(): bool
4141

4242
protected function fullPath(string $path): string
4343
{
44-
return realpath(__DIR__ . '/../' . $path);
44+
return realpath(__DIR__ . '/../' . ltrim($path, '/'));
4545
}
4646
}

src/Support/Trans.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace PrettyRoutes\Support;
4+
5+
use Helldar\Support\Facades\Arr;
6+
use Illuminate\Support\Facades\App;
7+
8+
final class Trans
9+
{
10+
public const DEFAULT_LOCALE = 'en';
11+
12+
protected static $translations = [];
13+
14+
public function get(string $key): string
15+
{
16+
return Arr::get($this->all(), $key);
17+
}
18+
19+
public function all(): array
20+
{
21+
$locale = $this->getLocale();
22+
23+
if (! isset(static::$translations[$locale])) {
24+
static::$translations[$locale] = require $this->getCorrectedPath($locale);
25+
}
26+
27+
return static::$translations[$locale];
28+
}
29+
30+
protected function isForce(): bool
31+
{
32+
return ! empty($this->getForceLocale());
33+
}
34+
35+
protected function appLocale(): string
36+
{
37+
return App::getLocale();
38+
}
39+
40+
protected function getLocale(): string
41+
{
42+
$locale = $this->isForce() ? $this->getForceLocale() : $this->appLocale();
43+
44+
$exploded = explode(',', $locale);
45+
46+
return reset($exploded);
47+
}
48+
49+
protected function getForceLocale()
50+
{
51+
return config('pretty-routes.locale_force');
52+
}
53+
54+
protected function exist(string $locale): bool
55+
{
56+
return ! empty($this->path($locale));
57+
}
58+
59+
protected function path(string $locale): string
60+
{
61+
return realpath(__DIR__ . '/../../resources/lang/' . $locale . '/info.php');
62+
}
63+
64+
protected function getCorrectedPath(string $locale): string
65+
{
66+
return $this->exist($locale)
67+
? $this->path($locale)
68+
: $this->path(static::DEFAULT_LOCALE);
69+
}
70+
}

tests/TranslationsTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Illuminate\Support\Facades\Config;
6+
7+
final class TranslationsTest extends TestCase
8+
{
9+
public function testDefaultTranslation()
10+
{
11+
$this->setConfigLocale('en');
12+
13+
$response = $this->get('/routes');
14+
15+
$response->assertStatus(200);
16+
$response->assertSee('Routes');
17+
$response->assertSee('Middlewares');
18+
$response->assertSee('Loading... Please wait...');
19+
$response->assertSee('Routes per page');
20+
21+
$response->assertDontSee('Foo Bar');
22+
$response->assertDontSee('Записей на странице');
23+
}
24+
25+
public function testChangedAppLocaleTranslation()
26+
{
27+
$this->setConfigLocale('ru');
28+
29+
$response = $this->get('/routes');
30+
31+
$response->assertStatus(200);
32+
$response->assertSee('Маршруты');
33+
$response->assertSee('Мидлвари');
34+
$response->assertSee('Загрузка... Пожалуйста, подождите...');
35+
$response->assertSee('Записей на странице');
36+
37+
$response->assertDontSee('Foo Bar');
38+
$response->assertDontSee('Routes per page');
39+
}
40+
41+
public function testChangedPackageLocaleTranslation()
42+
{
43+
$this->setConfigLocale('en', 'ru');
44+
45+
$response = $this->get('/routes');
46+
47+
$response->assertStatus(200);
48+
$response->assertSee('Маршруты');
49+
$response->assertSee('Мидлвари');
50+
$response->assertSee('Загрузка... Пожалуйста, подождите...');
51+
$response->assertSee('Записей на странице');
52+
53+
$response->assertDontSee('Foo Bar');
54+
$response->assertDontSee('Routes per page');
55+
}
56+
57+
public function testIncorrectLocaleTranslation()
58+
{
59+
$this->setConfigLocale('en', 'foo');
60+
61+
$response = $this->get('/routes');
62+
63+
$response->assertStatus(200);
64+
$response->assertSee('Routes');
65+
$response->assertSee('Middlewares');
66+
$response->assertSee('Loading... Please wait...');
67+
$response->assertSee('Routes per page');
68+
69+
$response->assertDontSee('Foo Bar');
70+
$response->assertDontSee('Записей на странице');
71+
}
72+
73+
protected function setConfigLocale(string $app, string $package = null): void
74+
{
75+
Config::set('app.locale', $app);
76+
Config::set('pretty-routes.locale_force', $package ?: false);
77+
}
78+
}

tests/ViewTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public function testTexts()
99
$response = $this->get('/routes');
1010

1111
$response->assertStatus(200);
12-
$response->assertSee('Routes list');
13-
$response->assertSee('Laravel');
12+
$response->assertSee('<!DOCTYPE html>', false);
13+
$response->assertSee('<div id="app">', false);
1414

1515
$response->assertDontSee('Foo Bar');
1616
}

0 commit comments

Comments
 (0)