-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathLang.php
More file actions
196 lines (167 loc) · 4.73 KB
/
Copy pathLang.php
File metadata and controls
196 lines (167 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
namespace PHPCensor\Helper;
use PHPCensor\Common\Application\ConfigurationInterface;
use PHPCensor\Store\UserStore;
use PHPCensor\StoreRegistry;
/**
* Languages Helper Class - Handles loading strings files and the strings within them.
*
* @package PHP Censor
* @subpackage Application
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class Lang
{
public const DEFAULT_LANGUAGE = 'en';
/**
* @var string
*/
protected static $language = null;
/**
* @var array
*/
protected static $languages = [];
/**
* @var array
*/
protected static $strings = [];
/**
* @var array
*/
protected static $defaultStrings = [];
/**
* Get a specific string from the language file.
*
* @param mixed ...$params
*
* @return string
*/
public static function get(...$params)
{
$string = $params[0];
if (\array_key_exists($string, self::$strings)) {
$params[0] = self::$strings[$string];
return \call_user_func_array('sprintf', $params);
} elseif (self::DEFAULT_LANGUAGE !== self::$language && \array_key_exists($string, self::$defaultStrings)) {
$params[0] = self::$defaultStrings[$string];
return \call_user_func_array('sprintf', $params);
}
return $string;
}
/**
* Get the currently active language.
*
* @return string|null
*/
public static function getLanguage()
{
return self::$language;
}
/**
* Try and load a language, and if successful, set it for use throughout the system.
*
*
* @return bool
*/
public static function setLanguage($language)
{
if (\in_array($language, self::$languages, true)) {
self::$language = $language;
self::$strings = self::loadLanguage();
return true;
}
return false;
}
/**
* Return a list of available languages and their names.
*
* @return array
*/
public static function getLanguageOptions()
{
$languages = [];
foreach (self::$languages as $language) {
$strings = include(SRC_DIR . 'Languages/lang.' . $language . '.php');
$languages[$language] = !empty($strings['language_name'])
? $strings['language_name'] . ' (' . $language . ')'
: $language;
}
return $languages;
}
/**
* Get the strings for the currently active language.
*
* @return string[]
*/
public static function getStrings()
{
return self::$strings;
}
/**
* Initialise the Language helper, try load the language file for the user's browser or the configured default.
*/
public static function init(
ConfigurationInterface $config,
StoreRegistry $storeRegistry,
?string $languageForce = null,
?int $sessionUserId = null
) {
self::$defaultStrings = self::loadLanguage(self::DEFAULT_LANGUAGE);
self::loadAvailableLanguages();
if ($languageForce && self::setLanguage($languageForce)) {
return;
}
$user = null;
if (!empty($sessionUserId)) {
/** @var UserStore $userStore */
$userStore = $storeRegistry->get('User');
$user = $userStore->getById($sessionUserId);
}
if ($user) {
$language = $user->getLanguage();
if ($user && self::setLanguage($language)) {
return;
}
}
// Try the installation default language:
$language = $config->get('php-censor.language', self::DEFAULT_LANGUAGE);
if (self::setLanguage($language)) {
return;
}
}
/**
* Load a specific language file.
*
* @param string $language
*
* @return string[]|null
*/
protected static function loadLanguage($language = null)
{
$language = $language
? $language
: self::$language;
$langFile = SRC_DIR . 'Languages/lang.' . $language . '.php';
if (!\file_exists($langFile)) {
return null;
}
$strings = include($langFile);
if (\is_null($strings) || !\is_array($strings) || !\count($strings)) {
return null;
}
return $strings;
}
/**
* Load the names of all available languages.
*/
protected static function loadAvailableLanguages()
{
$matches = [];
foreach (\glob(SRC_DIR . 'Languages/lang.*.php') as $file) {
if (\preg_match('/lang\.([a-z]{2}\-?[a-z]*)\.php/', $file, $matches)) {
self::$languages[] = $matches[1];
}
}
}
}