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\Models\Language; |
19
|
|
|
|
20
|
|
|
class Localisation |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The Laravel application instance. |
24
|
|
|
* |
25
|
|
|
* @var \Illuminate\Foundation\Application |
26
|
|
|
*/ |
27
|
|
|
protected $app; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Normalized Laravel Version. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $version; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* True when this is a Lumen application. |
38
|
|
|
* |
39
|
|
|
* @var boolean |
40
|
|
|
*/ |
41
|
|
|
protected $lumen = false; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor. |
45
|
|
|
* |
46
|
|
|
* @param Application $app laravel application for further use |
|
|
|
|
47
|
|
|
*/ |
48
|
|
|
public function __construct($app = null) |
49
|
|
|
{ |
50
|
|
|
if (null === $app) { |
51
|
|
|
$app = app(); |
52
|
|
|
//Fallback when $app is not given |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->app = $app; |
56
|
|
|
$this->version = $app->version(); |
|
|
|
|
57
|
|
|
$this->lumen = Str::contains($this->version, 'Lumen'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Retrieves all active languages. |
62
|
|
|
* |
63
|
|
|
* @return Collection a collection of active languages |
64
|
|
|
*/ |
65
|
|
|
public static function languages(): Collection |
66
|
|
|
{ |
67
|
|
|
return Language::where('active', 1)->get(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Determines the currently selected locale. |
72
|
|
|
* |
73
|
|
|
* @param string $locale (Optional) Locale to be used for language retrieval |
74
|
|
|
* |
75
|
|
|
* @return Language A language object |
76
|
|
|
*/ |
77
|
|
|
public static function currentLanguage(string $locale = ''): Language |
78
|
|
|
{ |
79
|
|
|
$fallbackLocale = config('app.fallback_locale'); |
80
|
|
|
|
81
|
|
|
if ('' === $locale) { |
82
|
|
|
$locale = app()->getLocale(); |
|
|
|
|
83
|
|
|
} else if ($locale === $fallbackLocale) { |
84
|
|
|
$locale = 'en-GB'; |
85
|
|
|
} else { |
86
|
|
|
$locale = $fallbackLocale . '-' . strtoupper($fallbackLocale); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
try { |
90
|
|
|
$current = Language::findByLocale($locale); |
91
|
|
|
} catch (\InvalidArgumentException $e) { |
92
|
|
|
$current = self::currentLanguage($fallbackLocale); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $current; |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Renders a language selector / switcher according to view file. |
100
|
|
|
* |
101
|
|
|
* @return Renderable language selector / switcher markup |
102
|
|
|
*/ |
103
|
|
|
public static function languageSelector(): Renderable |
104
|
|
|
{ |
105
|
|
|
$languages = self::languages(); |
106
|
|
|
$current = self::currentLanguage(); |
107
|
|
|
|
108
|
|
|
return view('localisation::atoms.languageselector', compact('languages', 'current')); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths