migrated ubiquitous libraries to composer autoloader (#421)
[openemr.git] / library / translation.inc.php
blob496a021310228caf0f79f669a491dfdac19237fa
1 <?php
2 include_once(dirname(__FILE__) . '/sql.inc'); // fixes vulnerability with register_globals
4 // Translation function
5 // This is the translation engine
6 // Note that it is recommended to no longer use the mode, prepend, or append
7 // parameters, since this is not compatible with the htmlspecialchars() php
8 // function.
9 function xl($constant,$mode='r',$prepend='',$append='') {
10 // set language id
11 if (!empty($_SESSION['language_choice'])) {
12 $lang_id = $_SESSION['language_choice'];
14 else {
15 $lang_id = 1;
18 if ($lang_id == 1 && !empty($GLOBALS['skip_english_translation'])) {
19 // language id = 1, so no need to translate
20 // -- remove comments
21 $string = preg_replace('/\{\{.*\}\}/', '', $constant);
23 else {
24 // TRANSLATE
25 // first, clean lines
26 // convert new lines to spaces and remove windows end of lines
27 $patterns = array ('/\n/','/\r/');
28 $replace = array (' ','');
29 $constant = preg_replace($patterns, $replace, $constant);
31 // second, attempt translation
32 $sql="SELECT * FROM lang_definitions JOIN lang_constants ON " .
33 "lang_definitions.cons_id = lang_constants.cons_id WHERE " .
34 "lang_id=? AND constant_name = ? LIMIT 1";
35 $res = sqlStatementNoLog($sql,array($lang_id,$constant));
36 $row = SqlFetchArray($res);
37 $string = $row['definition'];
38 if ($string == '') { $string = "$constant"; }
40 // remove dangerous characters and remove comments
41 $patterns = array ('/\n/','/\r/','/"/',"/'/",'/\{\{.*\}\}/');
42 $replace = array (' ','','`','`','');
43 $string = preg_replace($patterns, $replace, $string);
46 $string = "$prepend" . "$string" . "$append";
47 if ($mode=='e') {
48 echo $string;
49 } else {
50 return $string;
54 // ----------- xl() function wrappers ------------------------------
56 // Use above xl() function the majority of time for translations. The
57 // below wrappers are only for specific situations in order to support
58 // granular control of translations in certain parts of OpenEMR.
59 // Wrappers:
60 // xl_list_label()
61 // xl_layout_label()
62 // xl_gacl_group()
63 // xl_form_title()
64 // xl_document_category()
65 // xl_appt_category()
67 // Added 5-09 by BM for translation of list labels (when applicable)
68 // Only translates if the $GLOBALS['translate_lists'] is set to true.
69 function xl_list_label($constant,$mode='r',$prepend='',$append='') {
70 if ($GLOBALS['translate_lists']) {
71 // TRANSLATE
72 if ($mode == "e") {
73 xl($constant,$mode,$prepend,$append);
75 else {
76 return xl($constant,$mode,$prepend,$append);
79 else {
80 // DO NOT TRANSLATE
81 if ($mode == "e") {
82 echo $prepend.$constant.$append;
84 else {
85 return $prepend.$constant.$append;
89 // Added 5-09 by BM for translation of layout labels (when applicable)
90 // Only translates if the $GLOBALS['translate_layout'] is set to true.
91 function xl_layout_label($constant,$mode='r',$prepend='',$append='') {
92 if ($GLOBALS['translate_layout']) {
93 // TRANSLATE
94 if ($mode == "e") {
95 xl($constant,$mode,$prepend,$append);
97 else {
98 return xl($constant,$mode,$prepend,$append);
101 else {
102 // DO NOT TRANSLATE
103 if ($mode == "e") {
104 echo $prepend.$constant.$append;
106 else {
107 return $prepend.$constant.$append;
111 // Added 6-2009 by BM for translation of access control group labels
112 // (when applicable)
113 // Only translates if the $GLOBALS['translate_gacl_groups'] is set to true.
114 function xl_gacl_group($constant,$mode='r',$prepend='',$append='') {
115 if ($GLOBALS['translate_gacl_groups']) {
116 // TRANSLATE
117 if ($mode == "e") {
118 xl($constant,$mode,$prepend,$append);
120 else {
121 return xl($constant,$mode,$prepend,$append);
124 else {
125 // DO NOT TRANSLATE
126 if ($mode == "e") {
127 echo $prepend.$constant.$append;
129 else {
130 return $prepend.$constant.$append;
134 // Added 6-2009 by BM for translation of patient form (notes) titles
135 // (when applicable)
136 // Only translates if the $GLOBALS['translate_form_titles'] is set to true.
137 function xl_form_title($constant,$mode='r',$prepend='',$append='') {
138 if ($GLOBALS['translate_form_titles']) {
139 // TRANSLATE
140 if ($mode == "e") {
141 xl($constant,$mode,$prepend,$append);
143 else {
144 return xl($constant,$mode,$prepend,$append);
147 else {
148 // DO NOT TRANSLATE
149 if ($mode == "e") {
150 echo $prepend.$constant.$append;
152 else {
153 return $prepend.$constant.$append;
158 // Added 6-2009 by BM for translation of document categories
159 // (when applicable)
160 // Only translates if the $GLOBALS['translate_document_categories'] is set to true.
161 function xl_document_category($constant,$mode='r',$prepend='',$append='') {
162 if ($GLOBALS['translate_document_categories']) {
163 // TRANSLATE
164 if ($mode == "e") {
165 xl($constant,$mode,$prepend,$append);
167 else {
168 return xl($constant,$mode,$prepend,$append);
171 else {
172 // DO NOT TRANSLATE
173 if ($mode == "e") {
174 echo $prepend.$constant.$append;
176 else {
177 return $prepend.$constant.$append;
182 // Added 6-2009 by BM for translation of appointment categories
183 // (when applicable)
184 // Only translates if the $GLOBALS['translate_appt_categories'] is set to true.
185 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);
191 else {
192 return xl($constant,$mode,$prepend,$append);
195 else {
196 // DO NOT TRANSLATE
197 if ($mode == "e") {
198 echo $prepend.$constant.$append;
200 else {
201 return $prepend.$constant.$append;
205 // ---------------------------------------------------------------------------
207 // ---------------------------------
208 // Miscellaneous language translation functions
210 // Function to return the title of a language from the id
211 // @param integer (language id)
212 // return string (language title)
213 function getLanguageTitle($val) {
215 // validate language id
216 if (!empty($val)) {
217 $lang_id = $val;
219 else {
220 $lang_id = 1;
223 // get language title
224 $res = sqlStatement("select lang_description from lang_languages where lang_id =?",array($lang_id));
225 for ($iter = 0;$row = sqlFetchArray($res);$iter++) $result[$iter] = $row;
226 $languageTitle = $result[0]{"lang_description"};
227 return $languageTitle;
234 * Returns language directionality as string 'rtl' or 'ltr'
235 * @param int $lang_id language code
236 * @return string 'ltr' 'rtl'
237 * @author Amiel <amielel@matrix.co.il>
239 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';
248 //----------------------------------
250 // ----------------------------------------------------------------------------
252 HEADER HTML
254 shows some informations for pages html header
256 @param none
257 @return void
259 function html_header_show() {
261 // Below line was commented by the UTF-8 project on 05-2009 by BM.
262 // We commented this out since we are now standardizing encoding
263 // in the globals.php file.
264 // echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> '."\n";
266 // Keeping this function, since it may prove useful for user interface improvements