|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* PWWEB\Localisation. |
|
5
|
|
|
* |
|
6
|
|
|
* Localisation Master Class. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Frank Pillukeit <[email protected]> |
|
9
|
|
|
* @copyright 2020 pw-websolutions.com |
|
10
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace PWWEB\Localisation; |
|
14
|
|
|
|
|
15
|
|
|
use Illuminate\Contracts\Support\Renderable; |
|
16
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
17
|
|
|
use Illuminate\Support\Str; |
|
18
|
|
|
use PWWEB\Localisation\Contracts\Language as LanguageContract; |
|
19
|
|
|
use PWWEB\Localisation\Models\Language; |
|
20
|
|
|
use PWWEB\Localisation\Repositories\LanguageRepository; |
|
21
|
|
|
|
|
22
|
|
|
class Localisation |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* The Laravel application instance. |
|
26
|
|
|
* |
|
27
|
|
|
* @var \Illuminate\Foundation\Application |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $app; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Normalized Laravel Version. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $version; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* True when this is a Lumen application. |
|
40
|
|
|
* |
|
41
|
|
|
* @var bool |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $lumen = false; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Repository of languages to be used throughout the controller. |
|
47
|
|
|
* |
|
48
|
|
|
* @var \PWWEB\Localisation\Repositories\LanguageRepository |
|
49
|
|
|
*/ |
|
50
|
|
|
private $languageRepository; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Constructor. |
|
54
|
|
|
* |
|
55
|
|
|
* @param \PWWEB\Localisation\Repositories\LanguageRepository $languageRepo Repository of Languages |
|
56
|
|
|
* @param \Illuminate\Foundation\Application $app laravel application for further use |
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct(LanguageRepository $languageRepo, $app = null) |
|
59
|
|
|
{ |
|
60
|
|
|
if (null === $app) { |
|
61
|
|
|
$app = app(); |
|
62
|
|
|
//Fallback when $app is not given |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$this->app = $app; |
|
66
|
|
|
$this->version = $app->version(); |
|
|
|
|
|
|
67
|
|
|
$this->lumen = Str::contains($this->version, 'Lumen'); |
|
68
|
|
|
|
|
69
|
|
|
$this->languageRepository = $languageRepo; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Retrieves all active languages. |
|
74
|
|
|
* |
|
75
|
|
|
* @return \Illuminate\Database\Eloquent\Collection a collection of active languages |
|
76
|
|
|
*/ |
|
77
|
|
|
public function languages(): Collection |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->languageRepository->getAllActive(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Determines the currently selected locale. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $locale (Optional) Locale to be used for language retrieval |
|
86
|
|
|
* |
|
87
|
|
|
* @return \PWWEB\Localisation\Contracts\Language A language object |
|
88
|
|
|
*/ |
|
89
|
|
|
public function currentLanguage(string $locale = ''): LanguageContract |
|
90
|
|
|
{ |
|
91
|
|
|
$fallbackLocale = config('app.fallback_locale'); |
|
92
|
|
|
|
|
93
|
|
|
if ('' === $locale) { |
|
94
|
|
|
$locale = app()->getLocale(); |
|
|
|
|
|
|
95
|
|
|
} elseif ($locale === $fallbackLocale) { |
|
96
|
|
|
$locale = 'en-GB'; |
|
97
|
|
|
} else { |
|
98
|
|
|
$locale = $fallbackLocale.'-'.strtoupper($fallbackLocale); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
try { |
|
102
|
|
|
$current = $this->languageRepository->findByLocale($locale); |
|
103
|
|
|
} catch (\InvalidArgumentException $e) { |
|
104
|
|
|
$current = $this->currentLanguage($fallbackLocale); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $current; |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Renders a language selector / switcher according to view file. |
|
112
|
|
|
* |
|
113
|
|
|
* @return \Illuminate\Contracts\Support\Renderable language selector / switcher markup |
|
114
|
|
|
*/ |
|
115
|
|
|
public function languageSelector(): Renderable |
|
116
|
|
|
{ |
|
117
|
|
|
$languages = $this->languages(); |
|
118
|
|
|
$current = $this->currentLanguage(); |
|
119
|
|
|
|
|
120
|
|
|
return view('localisation::atoms.languageselector', compact('languages', 'current')); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|