Bug fix (#6333)
[openemr.git] / library / translation.inc.php
blobcd0cab2578707ca1de31226d7c0e97322fd87752
1 <?php
3 // Translation function
4 // This is the translation engine
5 // Note that it is recommended to no longer use the mode, prepend, or append
6 // parameters, since this is not compatible with the htmlspecialchars() php
7 // function.
8 //
9 // Note there are cases in installation where this function has already been
10 // declared, so check to ensure has not been declared yet.
12 if (!(function_exists('xl'))) {
13 function xl($constant, $mode = 'r', $prepend = '', $append = '')
15 if (!empty($GLOBALS['temp_skip_translations'])) {
16 return $constant;
19 // set language id
20 if (!empty($_SESSION['language_choice'])) {
21 $lang_id = $_SESSION['language_choice'];
22 } else {
23 $lang_id = 1;
26 // TRANSLATE
27 // first, clean lines
28 // convert new lines to spaces and remove windows end of lines
29 $patterns = array ('/\n/','/\r/');
30 $replace = array (' ','');
31 $constant = preg_replace($patterns, $replace, $constant ?? '');
32 // second, attempt translation
33 $sql = "SELECT * FROM lang_definitions JOIN lang_constants ON " .
34 "lang_definitions.cons_id = lang_constants.cons_id WHERE " .
35 "lang_id=? AND constant_name = ? LIMIT 1";
36 $res = sqlStatementNoLog($sql, array($lang_id,$constant));
37 $row = SqlFetchArray($res);
38 $string = $row['definition'] ?? '';
39 if ($string == '') {
40 $string = "$constant";
42 // remove dangerous characters and remove comments
43 if (!empty($GLOBALS['translate_no_safe_apostrophe'])) {
44 $patterns = array ('/\n/','/\r/','/\{\{.*\}\}/');
45 $replace = array (' ','','');
46 $string = preg_replace($patterns, $replace, $string);
47 } else {
48 // convert apostrophes and quotes to safe apostrophe
49 $patterns = array ('/\n/','/\r/','/"/',"/'/",'/\{\{.*\}\}/');
50 $replace = array (' ','','`','`','');
51 $string = preg_replace($patterns, $replace, $string);
54 $string = "$prepend" . "$string" . "$append";
55 if ($mode == 'e') {
56 echo $string;
57 } else {
58 return $string;
63 // ----------- xl() function wrappers ------------------------------
65 // Use above xl() function the majority of time for translations. The
66 // below wrappers are only for specific situations in order to support
67 // granular control of translations in certain parts of OpenEMR.
68 // Wrappers:
69 // xl_list_label()
70 // xl_layout_label()
71 // xl_gacl_group()
72 // xl_form_title()
73 // xl_document_category()
74 // xl_appt_category()
76 // Added 5-09 by BM for translation of list labels (when applicable)
77 // Only translates if the $GLOBALS['translate_lists'] is set to true.
78 function xl_list_label($constant, $mode = 'r', $prepend = '', $append = '')
80 if ($GLOBALS['translate_lists']) {
81 // TRANSLATE
82 if ($mode == "e") {
83 xl($constant, $mode, $prepend, $append);
84 } else {
85 return xl($constant, $mode, $prepend, $append);
87 } else {
88 // DO NOT TRANSLATE
89 if ($mode == "e") {
90 echo $prepend . $constant . $append;
91 } else {
92 return $prepend . $constant . $append;
96 // Added 5-09 by BM for translation of layout labels (when applicable)
97 // Only translates if the $GLOBALS['translate_layout'] is set to true.
98 function xl_layout_label($constant, $mode = 'r', $prepend = '', $append = '')
100 if ($GLOBALS['translate_layout']) {
101 // TRANSLATE
102 if ($mode == "e") {
103 xl($constant, $mode, $prepend, $append);
104 } else {
105 return xl($constant, $mode, $prepend, $append);
107 } else {
108 // DO NOT TRANSLATE
109 if ($mode == "e") {
110 echo $prepend . $constant . $append;
111 } else {
112 return $prepend . $constant . $append;
116 // Added 6-2009 by BM for translation of access control group labels
117 // (when applicable)
118 // Only translates if the $GLOBALS['translate_gacl_groups'] is set to true.
119 function xl_gacl_group($constant, $mode = 'r', $prepend = '', $append = '')
121 if ($GLOBALS['translate_gacl_groups']) {
122 // TRANSLATE
123 if ($mode == "e") {
124 xl($constant, $mode, $prepend, $append);
125 } else {
126 return xl($constant, $mode, $prepend, $append);
128 } else {
129 // DO NOT TRANSLATE
130 if ($mode == "e") {
131 echo $prepend . $constant . $append;
132 } else {
133 return $prepend . $constant . $append;
137 // Added 6-2009 by BM for translation of patient form (notes) titles
138 // (when applicable)
139 // Only translates if the $GLOBALS['translate_form_titles'] is set to true.
140 function xl_form_title($constant, $mode = 'r', $prepend = '', $append = '')
142 if ($GLOBALS['translate_form_titles']) {
143 // TRANSLATE
144 if ($mode == "e") {
145 xl($constant, $mode, $prepend, $append);
146 } else {
147 return xl($constant, $mode, $prepend, $append);
149 } else {
150 // DO NOT TRANSLATE
151 if ($mode == "e") {
152 echo $prepend . $constant . $append;
153 } else {
154 return $prepend . $constant . $append;
159 // Added 6-2009 by BM for translation of document categories
160 // (when applicable)
161 // Only translates if the $GLOBALS['translate_document_categories'] is set to true.
162 function xl_document_category($constant, $mode = 'r', $prepend = '', $append = '')
164 if ($GLOBALS['translate_document_categories']) {
165 // TRANSLATE
166 if ($mode == "e") {
167 xl($constant, $mode, $prepend, $append);
168 } else {
169 return xl($constant, $mode, $prepend, $append);
171 } else {
172 // DO NOT TRANSLATE
173 if ($mode == "e") {
174 echo $prepend . $constant . $append;
175 } else {
176 return $prepend . $constant . $append;
181 // Added 6-2009 by BM for translation of appointment categories
182 // (when applicable)
183 // Only translates if the $GLOBALS['translate_appt_categories'] is set to true.
184 function xl_appt_category($constant, $mode = 'r', $prepend = '', $append = '')
186 if ($GLOBALS['translate_appt_categories']) {
187 // TRANSLATE
188 if ($mode == "e") {
189 xl($constant, $mode, $prepend, $append);
190 } else {
191 return xl($constant, $mode, $prepend, $append);
193 } else {
194 // DO NOT TRANSLATE
195 if ($mode == "e") {
196 echo $prepend . $constant . $append;
197 } else {
198 return $prepend . $constant . $append;
202 // ---------------------------------------------------------------------------
204 // ---------------------------------
205 // Miscellaneous language translation functions
207 // Function to return the title of a language from the id
208 // @param integer (language id)
209 // return string (language title)
210 function getLanguageTitle($val)
213 // validate language id
214 if (!empty($val)) {
215 $lang_id = $val;
216 } else {
217 $lang_id = 1;
220 // get language title
221 $res = sqlStatement("select lang_description from lang_languages where lang_id =?", array($lang_id));
222 for ($iter = 0; $row = sqlFetchArray($res); $iter++) {
223 $result[$iter] = $row;
225 $languageTitle = $result[0]["lang_description"];
226 return $languageTitle;
233 * Returns language directionality as string 'rtl' or 'ltr'
234 * @param int $lang_id language code
235 * @return string 'ltr' 'rtl'
236 * @author Amiel <amielel@matrix.co.il>
238 function getLanguageDir($lang_id)
240 // validate language id
241 $lang_id = empty($lang_id) ? 1 : $lang_id;
242 // get language code
243 $row = sqlQuery('SELECT * FROM lang_languages WHERE lang_id = ?', array($lang_id));
245 return !empty($row['lang_is_rtl']) ? 'rtl' : 'ltr';