3535//log::add('debug_translate', 'error', $_content);
3636*/
3737
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+ */
3847class translate {
3948 /* * *************************Attributs****************************** */
4049
50+ /** @var array<string, array<string, array<string, string>>> Cached translations by language and namespace */
4151 protected static $ translation = array ();
52+
53+ /** @var string|null Current active language */
4254 protected static $ language = null ;
55+
56+ /** @var array<string, string>|null System configuration cache */
4357 private static $ config = null ;
58+
59+ /** @var array<string, bool> Loaded plugins tracking */
4460 private static $ pluginLoad = array ();
61+
62+ /** @var array<string, array<string, array<string, string>>> Loaded widgets translations */
4563 private static $ widgetLoad = array ();
4664
4765 /* * ***********************Methode static*************************** */
4866
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+ */
4975 public static function getConfig ($ _key , $ _default = '' ) {
5076 if (self ::$ config === null ) {
5177 self ::$ config = config::byKeys (array ('language ' ));
@@ -56,6 +82,12 @@ public static function getConfig($_key, $_default = '') {
5682 return $ _default ;
5783 }
5884
85+ /**
86+ * Gets translations for a plugin
87+ *
88+ * @param string $_plugin Plugin identifier
89+ * @return array<string, array<string, string>> Translation mappings
90+ */
5991 public static function getTranslation ($ _plugin ) {
6092 if (!isset (self ::$ translation [self ::getLanguage ()])) {
6193 self ::$ translation [self ::getLanguage ()] = array ();
@@ -67,6 +99,12 @@ public static function getTranslation($_plugin) {
6799 return self ::$ translation [self ::getLanguage ()];
68100 }
69101
102+ /**
103+ * Gets translations for a widget
104+ *
105+ * @param string $_widget Widget identifier
106+ * @return array<string, array<string, string>> Translation mappings
107+ */
70108 public static function getWidgetTranslation ($ _widget ) {
71109 if (!isset (self ::$ translation [self ::getLanguage ()]['core/template/widgets.html ' ])) {
72110 self ::$ translation [self ::getLanguage ()]['core/template/widgets.html ' ] = array ();
@@ -77,10 +115,25 @@ public static function getWidgetTranslation($_widget) {
77115 return self ::$ widgetLoad [$ _widget ];
78116 }
79117
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+ */
80127 public static function sentence ($ _content , $ _name , $ _backslash = false ) {
81128 return self ::exec ("{{ " . $ _content . "}} " , $ _name , $ _backslash );
82129 }
83130
131+ /**
132+ * Extracts plugin name from file path
133+ *
134+ * @param string $_name File path
135+ * @return string Plugin name or 'core'
136+ */
84137 public static function getPluginFromName ($ _name ) {
85138 if (strpos ($ _name , 'plugins/ ' ) === false ) {
86139 return 'core ' ;
@@ -95,6 +148,17 @@ public static function getPluginFromName($_name) {
95148 return $ matches [1 ];
96149 }
97150
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+ */
98162 public static function exec ($ _content , $ _name = '' , $ _backslash = false ) {
99163 if ($ _content == '' || $ _name == '' ) {
100164 return $ _content ;
@@ -163,14 +227,33 @@ public static function exec($_content, $_name = '', $_backslash = false) {
163227 return str_replace (array_keys ($ replace ), $ replace , $ _content );
164228 }
165229
230+ /**
231+ * Returns path to language translation file
232+ *
233+ * @param string $_language Language code
234+ * @return string File path
235+ */
166236 public static function getPathTranslationFile ($ _language ) {
167237 return __DIR__ . '/../i18n/ ' . $ _language . '.json ' ;
168238 }
169239
240+ /**
241+ * Returns path to widget translation file
242+ *
243+ * @param string $_widgetName Widget name
244+ * @return string File path
245+ */
170246 public static function getWidgetPathTranslationFile ($ _widgetName ) {
171247 return __DIR__ . '/../../data/customTemplates/i18n/ ' . $ _widgetName . '.json ' ;
172248 }
173249
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+ */
174257 public static function loadTranslation ($ _plugin = null ) {
175258 $ return = array ();
176259 if ($ _plugin == null || $ _plugin == 'core ' ) {
@@ -202,20 +285,40 @@ public static function loadTranslation($_plugin = null) {
202285 return $ return ;
203286 }
204287
288+ /**
289+ * Gets current active language
290+ *
291+ * @return string Language code
292+ */
205293 public static function getLanguage () {
206294 if (self ::$ language == null ) {
207295 self ::$ language = self ::getConfig ('language ' , 'fr_FR ' );
208296 }
209297 return self ::$ language ;
210298 }
211299
300+ /**
301+ * Sets active language
302+ *
303+ * @param string $_langage Language code
304+ * @return void
305+ */
212306 public static function setLanguage ($ _langage ) {
213307 self ::$ language = $ _langage ;
214308 }
215309
216310 /* * *********************Methode d'instance************************* */
217311}
218312
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+ */
219322function __ ($ _content , $ _name , $ _backslash = false ) {
220323 return translate::sentence (str_replace ("\' " , "' " , $ _content ), $ _name , $ _backslash );
221324}
0 commit comments