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(config('localizer.allowed_langs')); |
18
|
|
|
} else { |
19
|
|
|
return self::addNames([config('localizer.default_lang')]); |
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 the url to set up language and return back. |
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 url to set up language and return to url('/'). |
68
|
|
|
* |
69
|
|
|
* @param string $code |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public static function setRouteHome($code) |
74
|
|
|
{ |
75
|
|
|
return route('localizer::setLocaleHome', ['locale' => $code]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns the current language name. |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public static function getLanguage($code = App::getLocale()) |
84
|
|
|
{ |
85
|
|
|
return self::addNames([$code])[$code]; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|