1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mcamara\LaravelLocalization\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Log; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* LoadsTranslatedCachedRoutes |
9
|
|
|
* |
10
|
|
|
* Add this trait to your App\RouteServiceProvider to load |
11
|
|
|
* translated cached routes for the active locale, instead |
12
|
|
|
* of the default locale's routes (irrespective of active). |
13
|
|
|
*/ |
14
|
|
|
trait LoadsTranslatedCachedRoutes |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Load the cached routes for the application. |
18
|
|
|
* |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
protected function loadCachedRoutes() |
22
|
|
|
{ |
23
|
|
|
$localization = $this->getLaravelLocalization(); |
24
|
|
|
|
25
|
|
|
$localization->setLocale(); |
26
|
|
|
|
27
|
|
|
$locale = $localization->getCurrentLocale(); |
28
|
|
|
|
29
|
|
|
$localeKeys = $localization->getSupportedLanguagesKeys(); |
30
|
|
|
|
31
|
|
|
// First, try to load the routes specifically cached for this locale |
32
|
|
|
// if they do not exist, write a warning to the log and load the default |
33
|
|
|
// routes instead. Note that this is guaranteed to exist, because the |
34
|
|
|
// 'cached routes' check in the Application checks its existence. |
35
|
|
|
|
36
|
|
|
$path = $this->makeLocaleRoutesPath($locale, $localeKeys); |
37
|
|
|
|
38
|
|
|
if ( ! file_exists($path)) { |
39
|
|
|
|
40
|
|
|
Log::warning("Routes cached, but no cached routes found for locale '{$locale}'!"); |
41
|
|
|
|
42
|
|
|
$path = $this->getDefaultCachedRoutePath(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->app->booted(function () use ($path) { |
|
|
|
|
46
|
|
|
require $path; |
47
|
|
|
}); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Returns the path to the cached routes file for a given locale. |
52
|
|
|
* |
53
|
|
|
* @param string $locale |
54
|
|
|
* @param string[] $localeKeys |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
protected function makeLocaleRoutesPath($locale, $localeKeys) |
58
|
|
|
{ |
59
|
|
|
$path = $this->getDefaultCachedRoutePath(); |
60
|
|
|
|
61
|
|
|
$localeSegment = request()->segment(1); |
62
|
|
|
if ( ! $localeSegment || ! in_array($localeSegment, $localeKeys)) { |
63
|
|
|
return $path; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return substr($path, 0, -4) . '_' . $locale . '.php'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns the path to the standard cached routes file. |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
protected function getDefaultCachedRoutePath() |
75
|
|
|
{ |
76
|
|
|
return $this->app->getCachedRoutesPath(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return \Mcamara\LaravelLocalization\LaravelLocalization |
81
|
|
|
*/ |
82
|
|
|
protected function getLaravelLocalization() |
83
|
|
|
{ |
84
|
|
|
return app('laravellocalization'); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: