1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* BEdita, API-first content management framework |
6
|
|
|
* Copyright 2018 ChannelWeb Srl, Chialab Srl |
7
|
|
|
* |
8
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
10
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
14
|
|
|
*/ |
15
|
|
|
namespace BEdita\I18n\Core; |
16
|
|
|
|
17
|
|
|
use Cake\Core\Configure; |
18
|
|
|
use Cake\I18n\I18n; |
19
|
|
|
use Cake\Utility\Hash; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Trait that helps with I18n stuff as locale and language configuration. |
23
|
|
|
*/ |
24
|
|
|
trait I18nTrait |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Proxy to `\Cake\I18n\I18n::getLocale()`. |
28
|
|
|
* Return the currently configure locale as stored in the `intl.default_locale` PHP setting. |
29
|
|
|
* |
30
|
|
|
* @return string The name of the default locale. |
31
|
|
|
* @codeCoverageIgnore |
32
|
|
|
*/ |
33
|
|
|
public function getLocale(): string |
34
|
|
|
{ |
35
|
|
|
return I18n::getLocale(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Return the configured supported locales |
40
|
|
|
* |
41
|
|
|
* @return array |
42
|
|
|
* @codeCoverageIgnore |
43
|
|
|
*/ |
44
|
|
|
public function getLocales(): array |
45
|
|
|
{ |
46
|
|
|
return (array)Configure::read('I18n.locales', []); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Return an array of available languages. |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
* @codeCoverageIgnore |
54
|
|
|
*/ |
55
|
|
|
public function getLanguages(): array |
56
|
|
|
{ |
57
|
|
|
return (array)Configure::read('I18n.languages', []); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Return the current lang usually set by `\BEdita\I18n\Middleware\I18nMiddleware` |
62
|
|
|
* |
63
|
|
|
* @return string|null |
64
|
|
|
* @codeCoverageIgnore |
65
|
|
|
*/ |
66
|
|
|
public function getLang(): ?string |
67
|
|
|
{ |
68
|
|
|
return Configure::read('I18n.lang'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Return the language name as configured. |
73
|
|
|
* |
74
|
|
|
* @param string $lang The abbreviated lang |
75
|
|
|
* @return string|null |
76
|
|
|
*/ |
77
|
|
|
public function getLangName(?string $lang = null): ?string |
78
|
|
|
{ |
79
|
|
|
if (empty($lang)) { |
80
|
|
|
$lang = Configure::read('I18n.default'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return Hash::get($this->getLanguages(), $lang); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Return the default lang as configured. |
88
|
|
|
* |
89
|
|
|
* @return string|null |
90
|
|
|
* @codeCoverageIgnore |
91
|
|
|
*/ |
92
|
|
|
public function getDefaultLang(): ?string |
93
|
|
|
{ |
94
|
|
|
return Configure::read('I18n.default'); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|