1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Translation\Repositories; |
4
|
|
|
|
5
|
|
|
use Translation\Models\Language; |
6
|
|
|
use Translation\Models\Locale; |
7
|
|
|
use Translation\Models\Country; |
8
|
|
|
use Translation; |
9
|
|
|
|
10
|
|
|
class LangRepository |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
const COOKIENAME = 'language'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
|
|
public static function getDefaultLocale() |
22
|
|
|
{ |
23
|
|
|
return Translation::getAppLocale(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
public static function getLocale() |
30
|
|
|
{ |
31
|
|
|
return config('translation.active_languages', [ |
32
|
|
|
'en-GB', |
33
|
|
|
'fr-FR', |
34
|
|
|
'es-CO', |
35
|
|
|
'pt-BR', |
36
|
|
|
]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param mixed $locale (optional) |
41
|
|
|
* @param string $column (optional) |
42
|
|
|
* @param string $inLocale (optional) |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public static function get($locale = null, $column = null, $inLocale = false) |
46
|
|
|
{ |
47
|
|
|
if (!$inLocale) { |
48
|
|
|
$inLocale = self::getDefaultLocale(); |
49
|
|
|
} |
50
|
|
|
$allLocale = self::getLocale(); |
51
|
|
|
|
52
|
|
|
if ($locale) { |
53
|
|
|
$locale = (array) $locale; |
54
|
|
|
foreach ($locale as $value) { |
55
|
|
|
$bestLocale[] = \Locale::lookup($allLocale, $value, false, $inLocale); |
56
|
|
|
} |
57
|
|
|
$allLocale = $bestLocale; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return self::configure($allLocale, $column, $inLocale); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param array $locale |
65
|
|
|
* @param string $column |
66
|
|
|
* @param string $inLocale |
67
|
|
|
*/ |
68
|
|
|
protected static function configure($locale, $column, $inLocale) |
69
|
|
|
{ |
70
|
|
|
foreach ($locale as $key) { |
71
|
|
|
$configured[$key] = [ |
72
|
|
|
'locale' => $key, |
73
|
|
|
'name' => utf8_decode(\Locale::getDisplayName($key, $inLocale)), |
74
|
|
|
'region' => utf8_decode(\Locale::getDisplayRegion($key, $inLocale)), |
75
|
|
|
'language' => utf8_decode(\Locale::getDisplayLanguage($key, $inLocale)), |
76
|
|
|
'class' => 'flag-icon flag-icon-' . strtolower(\Locale::getRegion($key)) |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($column) { |
81
|
|
|
return array_column($configured, $column, 'locale'); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $configured; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public static function getCurrent() |
91
|
|
|
{ |
92
|
|
|
// if ($cookieLocale = self::getCookie()) { |
93
|
|
|
// return current(self::get($cookieLocale)); |
94
|
|
|
// } |
95
|
|
|
// |
96
|
|
|
// if (!empty(Yii::app()->user->model()->language)) { |
97
|
|
|
// $userLocale = Yii::app()->user->model()->language; |
98
|
|
|
// return current(self::get($userLocale)); |
99
|
|
|
// } |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
if (session('language')!==null) { |
|
|
|
|
103
|
|
|
//Config::set('app.locale', session('language')); |
104
|
|
|
return current(self::get(session('language'))); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
108
|
|
|
$browserLocale = \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']); |
109
|
|
|
return current(self::get($browserLocale)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return current(self::get(self::getDefaultLocale())); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $language |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
|
|
public static function updateCookie($language) |
120
|
|
|
{ |
121
|
|
|
return (!self::getCookie()) ?: self::setCookie($language); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
public static function getCookie() |
128
|
|
|
{ |
129
|
|
|
if (empty($_COOKIE[self::COOKIENAME])) { |
130
|
|
|
return false; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $_COOKIE[self::COOKIENAME]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
} |
137
|
|
|
|