1
|
|
|
<?php namespace App\Services\Routing; |
2
|
|
|
|
3
|
|
|
use Illuminate\Container\Container; |
4
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
5
|
|
|
use Illuminate\Routing\Router; |
6
|
|
|
|
7
|
|
|
class LocalizedRouter extends Router |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* {@inheritdoc} |
11
|
|
|
*/ |
12
|
19 |
|
public function __construct(Dispatcher $events, Container $container = null) |
13
|
|
|
{ |
14
|
19 |
|
parent::__construct($events, $container); |
15
|
|
|
|
16
|
19 |
|
$this->routes = new LocalizedRouteCollection; |
17
|
19 |
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
19 |
|
protected function addRoute($methods, $uri, $action) |
23
|
|
|
{ |
24
|
19 |
|
if (isset($action['locale'])) { |
25
|
|
|
// Now we apply our Localization modifications. |
26
|
19 |
|
$uri = $this->localizeUris($uri, $action['locale']); |
27
|
|
|
} |
28
|
|
|
|
29
|
19 |
|
return parent::addRoute($methods, $uri, $action); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
19 |
|
public function resource($name, $controller, array $options = []) |
36
|
|
|
{ |
37
|
19 |
|
if ($this->container && $this->container->bound(LocalizedResourceRegistrar::class)) { |
38
|
|
|
$registrar = $this->container->make(LocalizedResourceRegistrar::class); |
39
|
|
|
} else { |
40
|
19 |
|
$registrar = new LocalizedResourceRegistrar($this); |
41
|
|
|
} |
42
|
|
|
|
43
|
19 |
|
$registrar->register($name, $controller, $options); |
44
|
19 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Translates URIs |
48
|
|
|
* |
49
|
|
|
* @param string $uri |
50
|
|
|
* @param string $locale |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
19 |
|
private function localizeUris($uri, $locale) |
55
|
|
|
{ |
56
|
19 |
|
$uriExploded = explode('/', trim($uri, '/')); |
57
|
19 |
|
$localizedUriTranslationBitParts = []; |
58
|
19 |
|
while (list($level, $bitName) = each($uriExploded)) { |
59
|
19 |
|
if ($level == 0) { |
60
|
19 |
|
$localizedUriTranslationBitParts[$level] = 'routes.' . $bitName . '.'; |
61
|
|
|
} else { |
62
|
19 |
|
$localizedUriTranslationBitParts[$level] = trim($localizedUriTranslationBitParts[$level - 1], '.') . '.' . $bitName; |
63
|
|
|
} |
64
|
|
|
} |
65
|
19 |
|
foreach ($localizedUriTranslationBitParts as $level => &$translationBitPart) { |
66
|
19 |
|
$phraseToGetTranslationFor = $translationBitPart; |
67
|
19 |
|
if (preg_match('#(?<!routes)\.\{[^\}]+\}\.#', $translationBitPart)) { // For lower-level paths, in order not to hit 'routes.' index. |
68
|
|
|
$phraseToGetTranslationFor = preg_replace('#\{[^\}]+\}\.?#', '', $translationBitPart); |
69
|
|
|
} |
70
|
19 |
|
app('translator')->setLocale($locale); |
71
|
19 |
|
$translatedPhrase = app('translator')->get($phraseToGetTranslationFor); |
72
|
19 |
|
if (false !== strpos($translatedPhrase, '.')) { |
73
|
19 |
|
$translationBitPart = $uriExploded[$level]; |
74
|
|
|
} else { |
75
|
19 |
|
$translationBitPart = $translatedPhrase; |
76
|
|
|
} |
77
|
19 |
|
unset($translationBitPart); // Delete the reference (won't delete the original). |
78
|
|
|
} |
79
|
|
|
|
80
|
19 |
|
return implode('/', $localizedUriTranslationBitParts); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|