35
35
//log::add('debug_translate', 'error', $_content);
36
36
*/
37
37
38
+ /**
39
+ * Handles internationalization (i18n) for Jeedom system
40
+ *
41
+ * Provides translation capabilities for both core system and plugins,
42
+ * with caching mechanisms for better performance.
43
+ *
44
+ * @see config For language configuration
45
+ * @see plugin For plugin-related translations
46
+ */
38
47
class translate {
39
48
/* * *************************Attributs****************************** */
40
49
50
+ /** @var array<string, array<string, array<string, string>>> Cached translations by language and namespace */
41
51
protected static $ translation = array ();
52
+
53
+ /** @var string|null Current active language */
42
54
protected static $ language = null ;
55
+
56
+ /** @var array<string, string>|null System configuration cache */
43
57
private static $ config = null ;
58
+
59
+ /** @var array<string, bool> Loaded plugins tracking */
44
60
private static $ pluginLoad = array ();
61
+
62
+ /** @var array<string, array<string, array<string, string>>> Loaded widgets translations */
45
63
private static $ widgetLoad = array ();
46
64
47
65
/* * ***********************Methode static*************************** */
48
66
67
+ /**
68
+ * Retrieves a configuration value
69
+ *
70
+ * @param string $_key Configuration key to retrieve
71
+ * @param string $_default Default value if key not found
72
+ * @return string Configuration value
73
+ * @see config::byKeys() For configuration retrieval
74
+ */
49
75
public static function getConfig ($ _key , $ _default = '' ) {
50
76
if (self ::$ config === null ) {
51
77
self ::$ config = config::byKeys (array ('language ' ));
@@ -56,6 +82,12 @@ public static function getConfig($_key, $_default = '') {
56
82
return $ _default ;
57
83
}
58
84
85
+ /**
86
+ * Gets translations for a plugin
87
+ *
88
+ * @param string $_plugin Plugin identifier
89
+ * @return array<string, array<string, string>> Translation mappings
90
+ */
59
91
public static function getTranslation ($ _plugin ) {
60
92
if (!isset (self ::$ translation [self ::getLanguage ()])) {
61
93
self ::$ translation [self ::getLanguage ()] = array ();
@@ -67,6 +99,12 @@ public static function getTranslation($_plugin) {
67
99
return self ::$ translation [self ::getLanguage ()];
68
100
}
69
101
102
+ /**
103
+ * Gets translations for a widget
104
+ *
105
+ * @param string $_widget Widget identifier
106
+ * @return array<string, array<string, string>> Translation mappings
107
+ */
70
108
public static function getWidgetTranslation ($ _widget ) {
71
109
if (!isset (self ::$ translation [self ::getLanguage ()]['core/template/widgets.html ' ])) {
72
110
self ::$ translation [self ::getLanguage ()]['core/template/widgets.html ' ] = array ();
@@ -77,10 +115,25 @@ public static function getWidgetTranslation($_widget) {
77
115
return self ::$ widgetLoad [$ _widget ];
78
116
}
79
117
118
+ /**
119
+ * Translates a single text key
120
+ *
121
+ * @param string $_content Translation key
122
+ * @param string $_name Plugin/widget identifier or path
123
+ * @param bool $_backslash Whether to escape single quotes
124
+ * @return string Translated text
125
+ * @see self::exec() For full translation processing
126
+ */
80
127
public static function sentence ($ _content , $ _name , $ _backslash = false ) {
81
128
return self ::exec ("{{ " . $ _content . "}} " , $ _name , $ _backslash );
82
129
}
83
130
131
+ /**
132
+ * Extracts plugin name from file path
133
+ *
134
+ * @param string $_name File path
135
+ * @return string Plugin name or 'core'
136
+ */
84
137
public static function getPluginFromName ($ _name ) {
85
138
if (strpos ($ _name , 'plugins/ ' ) === false ) {
86
139
return 'core ' ;
@@ -95,6 +148,17 @@ public static function getPluginFromName($_name) {
95
148
return $ matches [1 ];
96
149
}
97
150
151
+ /**
152
+ * Processes text content for internationalization
153
+ *
154
+ * @param string $_content Text with {{translation_keys}}
155
+ * @param string $_name Plugin/widget identifier or path
156
+ * @param bool $_backslash Whether to escape single quotes
157
+ * @return string Translated content
158
+ * @see self::getLanguage() For current language
159
+ * @see self::getTranslation() For plugin translations
160
+ * @see self::getWidgetTranslation() For widget translations
161
+ */
98
162
public static function exec ($ _content , $ _name = '' , $ _backslash = false ) {
99
163
if ($ _content == '' || $ _name == '' ) {
100
164
return $ _content ;
@@ -163,14 +227,33 @@ public static function exec($_content, $_name = '', $_backslash = false) {
163
227
return str_replace (array_keys ($ replace ), $ replace , $ _content );
164
228
}
165
229
230
+ /**
231
+ * Returns path to language translation file
232
+ *
233
+ * @param string $_language Language code
234
+ * @return string File path
235
+ */
166
236
public static function getPathTranslationFile ($ _language ) {
167
237
return __DIR__ . '/../i18n/ ' . $ _language . '.json ' ;
168
238
}
169
239
240
+ /**
241
+ * Returns path to widget translation file
242
+ *
243
+ * @param string $_widgetName Widget name
244
+ * @return string File path
245
+ */
170
246
public static function getWidgetPathTranslationFile ($ _widgetName ) {
171
247
return __DIR__ . '/../../data/customTemplates/i18n/ ' . $ _widgetName . '.json ' ;
172
248
}
173
249
250
+ /**
251
+ * Loads translations for core or plugin
252
+ *
253
+ * @param string|null $_plugin Plugin name or null for core
254
+ * @return array<string, array<string, string>> Translation mappings
255
+ * @see plugin::getTranslation() For plugin translations
256
+ */
174
257
public static function loadTranslation ($ _plugin = null ) {
175
258
$ return = array ();
176
259
if ($ _plugin == null || $ _plugin == 'core ' ) {
@@ -202,20 +285,40 @@ public static function loadTranslation($_plugin = null) {
202
285
return $ return ;
203
286
}
204
287
288
+ /**
289
+ * Gets current active language
290
+ *
291
+ * @return string Language code
292
+ */
205
293
public static function getLanguage () {
206
294
if (self ::$ language == null ) {
207
295
self ::$ language = self ::getConfig ('language ' , 'fr_FR ' );
208
296
}
209
297
return self ::$ language ;
210
298
}
211
299
300
+ /**
301
+ * Sets active language
302
+ *
303
+ * @param string $_langage Language code
304
+ * @return void
305
+ */
212
306
public static function setLanguage ($ _langage ) {
213
307
self ::$ language = $ _langage ;
214
308
}
215
309
216
310
/* * *********************Methode d'instance************************* */
217
311
}
218
312
313
+ /**
314
+ * Global translation helper for a single text key
315
+ *
316
+ * @param string $_content Translation key
317
+ * @param string $_name Plugin/widget identifier or path
318
+ * @param bool $_backslash Whether to escape single quotes
319
+ * @return string Translated text
320
+ * @see translate::sentence() Core translation method
321
+ */
219
322
function __ ($ _content , $ _name , $ _backslash = false ) {
220
323
return translate::sentence (str_replace ("\' " , "' " , $ _content ), $ _name , $ _backslash );
221
324
}
0 commit comments