1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Language controller class. |
4
|
|
|
* |
5
|
|
|
* @copyright YetiForce Sp. z o.o. |
6
|
|
|
* @license YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com) |
7
|
|
|
* @author Mariusz Krzaczkowski <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace App; |
11
|
|
|
|
12
|
|
|
class Language |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Language files format. |
16
|
|
|
*/ |
17
|
|
|
const FORMAT = 'json'; |
18
|
|
|
|
19
|
|
|
//Contains module language translations |
20
|
|
|
protected static $languageContainer = []; |
21
|
|
|
protected static $modules = false; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Functions that gets translated string. |
25
|
|
|
* |
26
|
|
|
* @param string $key - string which need to be translated |
27
|
|
|
* @param string $module - module scope in which the translation need to be check |
28
|
|
|
* @param string $currentLanguage |
29
|
|
|
* |
30
|
|
|
* @return string - translated string |
31
|
|
|
*/ |
32
|
|
|
public static function translate(string $key, string $module = 'Basic', string $currentLanguage = ''): string |
33
|
|
|
{ |
34
|
|
|
if (empty($currentLanguage)) { |
35
|
|
|
$currentLanguage = static::getLanguage(); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
//decoding for Start Date & Time and End Date & Time |
38
|
|
|
if (!\is_array($key)) { |
39
|
|
|
$key = html_entity_decode($key); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
$translatedString = static::getLanguageTranslatedString($currentLanguage, $key, $module); |
|
|
|
|
42
|
|
|
// label not found in users language pack, then check in the default language pack(config.inc.php) |
43
|
|
|
if (null === $translatedString) { |
44
|
|
|
$defaultLanguage = Config::get('language'); |
45
|
|
|
if (!empty($defaultLanguage) && 0 !== strcasecmp($defaultLanguage, $currentLanguage)) { |
46
|
|
|
$translatedString = static::getLanguageTranslatedString($defaultLanguage, $key, $module); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// If translation is not found then return label |
51
|
|
|
if (null === $translatedString) { |
52
|
|
|
$translatedString = $key; |
53
|
|
|
} |
54
|
|
|
return $translatedString; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Functions that gets translated string by $args. |
59
|
|
|
* |
60
|
|
|
* @param string $key - string which need to be translated |
61
|
|
|
* @param string $moduleName - module scope in which the translation need to be check |
62
|
|
|
* |
63
|
|
|
* @return string - translated string |
64
|
|
|
*/ |
65
|
|
|
public static function translateArgs(string $key, string $moduleName = 'Basic'): string |
66
|
|
|
{ |
67
|
|
|
$formattedString = static::translate($key, $moduleName); |
68
|
|
|
$args = \array_slice(\func_get_args(), 2); |
69
|
|
|
if (\is_array($args) && !empty($args)) { |
70
|
|
|
$formattedString = \call_user_func_array('vsprintf', [$formattedString, $args]); |
71
|
|
|
} |
72
|
|
|
return $formattedString; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Function that returns current language. |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public static function getLanguage() |
81
|
|
|
{ |
82
|
|
|
$userInstance = User::getUser(); |
83
|
|
|
$language = ''; |
|
|
|
|
84
|
|
|
if ($userInstance && $userInstance->has('language') && !empty($userInstance->get('language'))) { |
85
|
|
|
$language = $userInstance->get('language'); |
86
|
|
|
} else { |
87
|
|
|
$language = Config::get('language'); |
88
|
|
|
} |
89
|
|
|
return $language; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Function returns language specific translated string. |
94
|
|
|
* |
95
|
|
|
* @param string $language - en_us etc |
96
|
|
|
* @param string $key - label |
97
|
|
|
* @param string $module - module name |
98
|
|
|
* |
99
|
|
|
* @return string translated string or null if translation not found |
|
|
|
|
100
|
|
|
*/ |
101
|
|
|
public static function getLanguageTranslatedString(string $language, string $key, string $module = 'Basic') |
102
|
|
|
{ |
103
|
|
|
$moduleStrings = self::getModuleStringsFromFile($language, $module); |
104
|
|
|
if (!empty($moduleStrings['php'][$key])) { |
105
|
|
|
return stripslashes($moduleStrings['php'][$key]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$commonStrings = self::getModuleStringsFromFile($language); |
109
|
|
|
if (!empty($commonStrings['php'][$key])) { |
110
|
|
|
return stripslashes($commonStrings['php'][$key]); |
111
|
|
|
} |
112
|
|
|
return null; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public static function getModuleStringsFromFile(string $language, string $module = 'Basic') |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
if (empty(self::$languageContainer[$language][$module])) { |
118
|
|
|
static::loadLanguageFile($language, $module); |
119
|
|
|
} |
120
|
|
|
if (isset(self::$languageContainer[$language][$module])) { |
121
|
|
|
return self::$languageContainer[$language][$module]; |
122
|
|
|
} |
123
|
|
|
return []; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Load language file from JSON. |
128
|
|
|
* |
129
|
|
|
* @param string $language |
130
|
|
|
* @param string $moduleName |
131
|
|
|
* |
132
|
|
|
* @return void |
133
|
|
|
*/ |
134
|
|
|
public static function loadLanguageFile(string $language, string $moduleName = 'Basic') |
135
|
|
|
{ |
136
|
|
|
if (!isset(static::$languageContainer[$language][$moduleName])) { |
137
|
|
|
static::$languageContainer[$language][$moduleName] = []; |
138
|
|
|
$file = \DIRECTORY_SEPARATOR . 'languages' . \DIRECTORY_SEPARATOR . $language . \DIRECTORY_SEPARATOR . $moduleName . '.' . static::FORMAT; |
139
|
|
|
$langFile = ROOT_DIRECTORY . $file; |
140
|
|
|
if (file_exists($langFile)) { |
141
|
|
|
static::$languageContainer[$language][$moduleName] = Json::decode(file_get_contents($langFile), true) ?? []; |
142
|
|
|
Cache::save('LanguageFiles', $language . $moduleName, static::$languageContainer[$language][$moduleName], Cache::LONG); |
143
|
|
|
} |
144
|
|
|
} elseif (Cache::has('LanguageFiles', $language . $moduleName)) { |
145
|
|
|
static::$languageContainer[$language][$moduleName] = Cache::get('LanguageFiles', $language . $moduleName); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Functions that gets translated string for Client side. |
151
|
|
|
* |
152
|
|
|
* @param <String> $key - string which need to be translated |
|
|
|
|
153
|
|
|
* @param <String> $module - module scope in which the translation need to be check |
|
|
|
|
154
|
|
|
* @param mixed $language |
155
|
|
|
* |
156
|
|
|
* @return <String> - translated string |
|
|
|
|
157
|
|
|
*/ |
158
|
|
|
public static function jstranslate($language, $key, $module = 'Basic') |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
$moduleStrings = self::getModuleStringsFromFile($language, $module); |
161
|
|
|
if (!empty($moduleStrings['js'][$key])) { |
162
|
|
|
return $moduleStrings['js'][$key]; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$commonStrings = self::getModuleStringsFromFile($language); |
166
|
|
|
if (!empty($commonStrings['js'][$key])) { |
167
|
|
|
return $commonStrings['js'][$key]; |
168
|
|
|
} |
169
|
|
|
return $key; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Function to returns all language information. |
174
|
|
|
* |
175
|
|
|
* @return string[] |
176
|
|
|
*/ |
177
|
|
|
public static function getAllLanguages() |
178
|
|
|
{ |
179
|
|
|
return Config::$languages; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Function that returns current language short name. |
184
|
|
|
* |
185
|
|
|
* @return <String> - |
|
|
|
|
186
|
|
|
*/ |
187
|
|
|
public static function getShortLanguageName() |
188
|
|
|
{ |
189
|
|
|
$language = self::getLanguage(); |
190
|
|
|
return substr($language, 0, 2); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Function returns module strings. |
195
|
|
|
* |
196
|
|
|
* @param <String> $module - module Name |
|
|
|
|
197
|
|
|
* @param <String> languageStrings or jsLanguageStrings |
198
|
|
|
* @param mixed $type |
199
|
|
|
* |
200
|
|
|
* @return <Array> |
|
|
|
|
201
|
|
|
*/ |
202
|
|
|
public static function export($module, $type = 'php') |
|
|
|
|
203
|
|
|
{ |
204
|
|
|
$language = self::getLanguage(); |
205
|
|
|
$exportLangString = []; |
206
|
|
|
|
207
|
|
|
$moduleStrings = self::getModuleStringsFromFile($language, $module); |
208
|
|
|
if (!empty($moduleStrings[$type])) { |
209
|
|
|
$exportLangString = $moduleStrings[$type]; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$commonStrings = self::getModuleStringsFromFile($language); |
213
|
|
|
if (!empty($commonStrings[$type])) { |
214
|
|
|
$exportLangString += $commonStrings[$type]; |
215
|
|
|
} |
216
|
|
|
return $exportLangString; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public static function translateModule($module) |
|
|
|
|
220
|
|
|
{ |
221
|
|
|
if (!self::$modules) { |
222
|
|
|
$userInstance = User::getUser(); |
223
|
|
|
self::$modules = $userInstance->getModulesList(); |
224
|
|
|
} |
225
|
|
|
return self::$modules[$module] ?? $module; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.