1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Aitor24\Localizer; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\App; |
6
|
|
|
|
7
|
|
|
class Builder |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Get all allowed languages. |
11
|
|
|
* |
12
|
|
|
* @return array |
13
|
|
|
*/ |
14
|
|
|
public static function allowedLanguages() |
15
|
|
|
{ |
16
|
|
|
if (!config('localizer.allowed_langs')) { |
17
|
|
|
return ['en']; |
18
|
|
|
} else { |
19
|
|
|
return self::addNames(config('localizer.allowed_langs')); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Returns add names for arrays with only codes an return an array as [$code => $language]. |
25
|
|
|
* |
26
|
|
|
* @param array $langs |
27
|
|
|
* |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
public static function addNames($langs) |
31
|
|
|
{ |
32
|
|
|
// Read JSON file |
33
|
|
|
$json = file_get_contents(__DIR__.'/languages.json'); |
34
|
|
|
|
35
|
|
|
//Decode JSON |
36
|
|
|
$json_data = json_decode($json, true); |
37
|
|
|
|
38
|
|
|
$array = []; |
39
|
|
|
|
40
|
|
|
//Generate an array with $lang code as key and name as value |
41
|
|
|
foreach ($langs as $lang) { |
42
|
|
|
$lang_name = 'Unknoun'; |
43
|
|
|
foreach ($json_data as $lang_data) { |
44
|
|
|
if ($lang_data['code'] == $lang) { |
45
|
|
|
$lang_name = $lang_data['name']; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
$array[$lang] = $lang_name; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $array; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns an string url to set up language. |
56
|
|
|
* |
57
|
|
|
* @param string $code |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public static function setRoute($code) |
62
|
|
|
{ |
63
|
|
|
return route('localizer::setLocale', ['locale' => $code]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns the current language code. |
68
|
|
|
* |
69
|
|
|
* @param string $ucfirst |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public static function getCurrentCode($ucfirst = false) |
74
|
|
|
{ |
75
|
|
|
if ($ucfirst) { |
|
|
|
|
76
|
|
|
return ucfirst(App::getLocale()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return App::getLocale(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Returns the current language name. |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public static function getCurrentLanguage() |
88
|
|
|
{ |
89
|
|
|
return self::addNames([self::getCurrentCode()])[self::getCurrentCode()]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns the language name. |
94
|
|
|
* |
95
|
|
|
* @param string $code |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public static function getLanguage($code) |
100
|
|
|
{ |
101
|
|
|
return self::addNames([$code])[$code]; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: