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 self::addNames(array_merge(config('localizer.allowed_langs'), [config('localizer.default_lang')])); |
18
|
|
|
} else { |
19
|
|
|
return self::addNames([config('localizer.default_lang')]); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Return true if $code is an allowed lang. |
25
|
|
|
* |
26
|
|
|
* @return bool |
27
|
|
|
**/ |
28
|
|
|
public static function isAllowedLanguage($code) |
29
|
|
|
{ |
30
|
|
|
return in_array($code, array_keys(self::allowedLanguages()) ); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Add names to an array of language codes as [$code => $language]. |
35
|
|
|
* |
36
|
|
|
* @param array $codes |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
**/ |
40
|
|
View Code Duplication |
public static function addNames($codes) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
// Read and decode JSON |
43
|
|
|
$json_data = json_decode(file_get_contents(__DIR__.'/languages.json'), true); |
44
|
|
|
|
45
|
|
|
$array = []; |
46
|
|
|
|
47
|
|
|
// Generate an array with $code as key and $code language as value |
48
|
|
|
foreach ($codes as $code) { |
49
|
|
|
$lang_name = 'Unknoun'; |
50
|
|
|
foreach ($json_data as $lang_data) { |
51
|
|
|
if ($lang_data['code'] == $code) { |
52
|
|
|
$lang_name = $lang_data['name']; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
$array[$code] = $lang_name; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $array; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Add names to an array of language codes as [$language => $code]. |
63
|
|
|
* |
64
|
|
|
* @param array $langs |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
**/ |
68
|
|
View Code Duplication |
public static function addCodes($langs) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
// Read and decode JSON |
71
|
|
|
$json_data = json_decode(file_get_contents(__DIR__.'/languages.json'), true); |
72
|
|
|
|
73
|
|
|
$array = []; |
74
|
|
|
|
75
|
|
|
// Generate an array with $lang as key and $lang code as value |
76
|
|
|
foreach ($langs as $lang) { |
77
|
|
|
$code = 'unk'; |
78
|
|
|
foreach ($json_data as $lang_data) { |
79
|
|
|
if ($lang_data['name'] == $lang) { |
80
|
|
|
$code = $lang_data['code']; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
$array[$lang] = $code; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $array; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns the url to set up language and return back. |
91
|
|
|
* |
92
|
|
|
* @param string $code |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
**/ |
96
|
|
|
public static function setRoute($code) |
97
|
|
|
{ |
98
|
|
|
return route('localizer::setLocale', ['locale' => $code]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns the url to set up language and return to url('/'). |
103
|
|
|
* |
104
|
|
|
* @param string $code |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
**/ |
108
|
|
|
public static function setRouteHome($code) |
109
|
|
|
{ |
110
|
|
|
return route('localizer::setLocaleHome', ['locale' => $code]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns the current language code. |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
**/ |
118
|
|
|
public static function getCode($name = 'default') |
119
|
|
|
{ |
120
|
|
|
if ($name == 'default') $name = self::getLanguage(); |
121
|
|
|
|
122
|
|
|
return self::addCodes([$name])[$name]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns the language name. |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
**/ |
130
|
|
|
public static function getLanguage($code = 'default') |
131
|
|
|
{ |
132
|
|
|
if ($code == 'default') $code = App::getLocale(); |
133
|
|
|
return self::addNames([$code])[$code]; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.