1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JeroenNoten\LaravelAdminLte\Menu\Filters; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Lang; |
6
|
|
|
|
7
|
|
|
class LangFilter implements FilterInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The array of menu item's properties that can be translated. |
11
|
|
|
* |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $itemProperties; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Constructor. |
18
|
|
|
*/ |
19
|
61 |
|
public function __construct() |
20
|
|
|
{ |
21
|
61 |
|
$this->itemProperties = ['header', 'text', 'label']; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Transforms a menu item. Makes the translations on the expected item |
26
|
|
|
* properties. |
27
|
|
|
* |
28
|
|
|
* @param array $item A menu item |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
54 |
|
public function transform($item) |
32
|
|
|
{ |
33
|
|
|
// Translate the expected menu item properties. |
34
|
|
|
|
35
|
54 |
|
foreach ($this->itemProperties as $prop) { |
36
|
|
|
// Check if the property exists in the item. |
37
|
|
|
|
38
|
54 |
|
if (empty($item[$prop])) { |
39
|
54 |
|
continue; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// Check if the property value is valid to be translated. |
43
|
|
|
|
44
|
54 |
|
if (is_array($item[$prop])) { |
45
|
1 |
|
$params = $item[$prop][1] ?? []; |
46
|
1 |
|
$params = is_array($params) ? $params : []; |
47
|
1 |
|
$item[$prop] = $this->getTranslation($item[$prop][0], $params); |
48
|
53 |
|
} elseif (is_string($item[$prop])) { |
49
|
53 |
|
$item[$prop] = $this->getTranslation($item[$prop]); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
54 |
|
return $item; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Gets the translation for a given key. |
58
|
|
|
* |
59
|
|
|
* @param string $key The key to translate |
60
|
|
|
* @param array $params The additional translation parameters |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
54 |
|
protected function getTranslation($key, $params = []) |
64
|
|
|
{ |
65
|
|
|
// Check for a translation. Note we first check if translations are |
66
|
|
|
// available in a "menu.php" file, then we check for translations in |
67
|
|
|
// the published language resources (under the adminlte namespace). |
68
|
|
|
|
69
|
54 |
|
if (Lang::has("menu.{$key}")) { |
70
|
2 |
|
return Lang::get("menu.{$key}", $params); |
71
|
53 |
|
} elseif (Lang::has("adminlte::menu.{$key}")) { |
72
|
4 |
|
return Lang::get("adminlte::menu.{$key}", $params); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// When there is no translation available, return the original key. |
76
|
|
|
|
77
|
52 |
|
return $key; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|