stop reporting of a custom error (it is benign) to the php logfile
[openemr.git] / library / translation.inc.php
blobdb117a73913983121462b7ebae420905083cd630
1 <?php
2 include_once(dirname(__FILE__) . '/sql.inc'); // fixes vulnerability with register_globals
4 // Translation function
5 // This is the translation engine
6 function xl($constant,$mode='r',$prepend='',$append='') {
7 // set language id
8 if (!empty($_SESSION['language_choice'])) {
9 $lang_id = $_SESSION['language_choice'];
11 else {
12 $lang_id = 1;
15 if ($lang_id == 1 && !empty($GLOBALS['skip_english_translation'])) {
16 // language id = 1, so no need to translate
17 $string = $constant;
19 else {
20 // TRANSLATE
21 // first, clean lines
22 // convert new lines to spaces and remove windows end of lines
23 $patterns = array ('/\n/','/\r/');
24 $replace = array (' ','');
25 $constant = preg_replace($patterns, $replace, $constant);
27 // second, attempt translation
28 $sql="SELECT * FROM lang_definitions JOIN lang_constants ON " .
29 "lang_definitions.cons_id = lang_constants.cons_id WHERE " .
30 "lang_id='$lang_id' AND constant_name = '" .
31 addslashes($constant) . "' LIMIT 1";
32 $res = SqlStatement($sql);
33 $row = SqlFetchArray($res);
34 $string = $row['definition'];
35 if ($string == '') { $string = "$constant"; }
37 // remove dangerous characters
38 $patterns = array ('/\n/','/\r/','/"/',"/'/");
39 $replace = array (' ','','`','`');
40 $string = preg_replace($patterns, $replace, $string);
43 $string = "$prepend" . "$string" . "$append";
44 if ($mode=='e') {
45 echo $string;
46 } else {
47 return $string;
51 // ----------- xl() function wrappers ------------------------------
53 // Use above xl() function the majority of time for translations. The
54 // below wrappers are only for specific situations in order to support
55 // granular control of translations in certain parts of OpenEMR.
56 // Wrappers:
57 // xl_list_label()
58 // xl_layout_label()
59 // xl_gacl_group()
60 // xl_form_title()
61 // xl_document_category()
62 // xl_appt_category()
64 // Added 5-09 by BM for translation of list labels (when applicable)
65 // Only translates if the $GLOBALS['translate_lists'] is set to true.
66 function xl_list_label($constant,$mode='r',$prepend='',$append='') {
67 if ($GLOBALS['translate_lists']) {
68 // TRANSLATE
69 if ($mode == "e") {
70 xl($constant,$mode,$prepend,$append);
72 else {
73 return xl($constant,$mode,$prepend,$append);
76 else {
77 // DO NOT TRANSLATE
78 if ($mode == "e") {
79 echo $prepend.$constant.$append;
81 else {
82 return $prepend.$constant.$append;
86 // Added 5-09 by BM for translation of layout labels (when applicable)
87 // Only translates if the $GLOBALS['translate_layout'] is set to true.
88 function xl_layout_label($constant,$mode='r',$prepend='',$append='') {
89 if ($GLOBALS['translate_layout']) {
90 // TRANSLATE
91 if ($mode == "e") {
92 xl($constant,$mode,$prepend,$append);
94 else {
95 return xl($constant,$mode,$prepend,$append);
98 else {
99 // DO NOT TRANSLATE
100 if ($mode == "e") {
101 echo $prepend.$constant.$append;
103 else {
104 return $prepend.$constant.$append;
108 // Added 6-2009 by BM for translation of access control group labels
109 // (when applicable)
110 // Only translates if the $GLOBALS['translate_gacl_groups'] is set to true.
111 function xl_gacl_group($constant,$mode='r',$prepend='',$append='') {
112 if ($GLOBALS['translate_gacl_groups']) {
113 // TRANSLATE
114 if ($mode == "e") {
115 xl($constant,$mode,$prepend,$append);
117 else {
118 return xl($constant,$mode,$prepend,$append);
121 else {
122 // DO NOT TRANSLATE
123 if ($mode == "e") {
124 echo $prepend.$constant.$append;
126 else {
127 return $prepend.$constant.$append;
131 // Added 6-2009 by BM for translation of patient form (notes) titles
132 // (when applicable)
133 // Only translates if the $GLOBALS['translate_form_titles'] is set to true.
134 function xl_form_title($constant,$mode='r',$prepend='',$append='') {
135 if ($GLOBALS['translate_form_titles']) {
136 // TRANSLATE
137 if ($mode == "e") {
138 xl($constant,$mode,$prepend,$append);
140 else {
141 return xl($constant,$mode,$prepend,$append);
144 else {
145 // DO NOT TRANSLATE
146 if ($mode == "e") {
147 echo $prepend.$constant.$append;
149 else {
150 return $prepend.$constant.$append;
155 // Added 6-2009 by BM for translation of document categories
156 // (when applicable)
157 // Only translates if the $GLOBALS['translate_document_categories'] is set to true.
158 function xl_document_category($constant,$mode='r',$prepend='',$append='') {
159 if ($GLOBALS['translate_document_categories']) {
160 // TRANSLATE
161 if ($mode == "e") {
162 xl($constant,$mode,$prepend,$append);
164 else {
165 return xl($constant,$mode,$prepend,$append);
168 else {
169 // DO NOT TRANSLATE
170 if ($mode == "e") {
171 echo $prepend.$constant.$append;
173 else {
174 return $prepend.$constant.$append;
179 // Added 6-2009 by BM for translation of appointment categories
180 // (when applicable)
181 // Only translates if the $GLOBALS['translate_appt_categories'] is set to true.
182 function xl_appt_category($constant,$mode='r',$prepend='',$append='') {
183 if ($GLOBALS['translate_appt_categories']) {
184 // TRANSLATE
185 if ($mode == "e") {
186 xl($constant,$mode,$prepend,$append);
188 else {
189 return xl($constant,$mode,$prepend,$append);
192 else {
193 // DO NOT TRANSLATE
194 if ($mode == "e") {
195 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) {
212 // validate language id
213 if (!empty($val)) {
214 $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 = '".$lang_id."'");
222 for ($iter = 0;$row = sqlFetchArray($res);$iter++) $result[$iter] = $row;
223 $languageTitle = $result[0]{"lang_description"};
224 return $languageTitle;
227 //----------------------------------
229 // ----------------------------------------------------------------------------
231 HEADER HTML
233 shows some informations for pages html header
235 @param none
236 @return void
238 function html_header_show() {
240 // Below line was commented by the UTF-8 project on 05-2009 by BM.
241 // We commented this out since we are now standardizing encoding
242 // in the globals.php file.
243 // echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> '."\n";
247 // ----------------------------------------------------------------------------
249 * Returns a string padded to a certain length with another string.
251 * This method behaves exactly like str_pad but is multibyte safe.
253 * @param string $input The string to be padded.
254 * @param int $length The length of the resulting string.
255 * @param string $pad The string to pad the input string with. Must
256 * be in the same charset like the input string.
257 * @param const $type The padding type. One of STR_PAD_LEFT,
258 * STR_PAD_RIGHT, or STR_PAD_BOTH.
259 * @param string $charset The charset of the input and the padding
260 * strings.
262 * @return string The padded string.
264 function mb_strpad($input, $length, $pad = ' ', $type = STR_PAD_RIGHT, $charset = 'UTF-8') {
265 mb_internal_encoding($charset);
266 $mb_length = mb_strlen($input, $charset);
267 $sb_length = strlen($input);
268 $pad_length = mb_strlen($pad, $charset);
270 /* Return if we already have the length. */
271 if ($mb_length >= $length) {
272 return $input;
275 /* Shortcut for single byte strings. */
276 if ($mb_length == $sb_length && $pad_length == strlen($pad)) {
277 return str_pad($input, $length, $pad, $type);
280 switch ($type) {
281 case STR_PAD_LEFT:
282 $left = $length - $mb_length;
283 $output = mb_substr(str_repeat($pad, ceil($left / $pad_length)), 0, $left, $charset) . $input;
284 break;
285 case STR_PAD_BOTH:
286 $left = floor(($length - $mb_length) / 2);
287 $right = ceil(($length - $mb_length) / 2);
288 $output = mb_substr(str_repeat($pad, ceil($left / $pad_length)), 0, $left, $charset) .
289 $input .
290 mb_substr(str_repeat($pad, ceil($right / $pad_length)), 0, $right, $charset);
291 break;
292 case STR_PAD_RIGHT:
293 $right = $length - $mb_length;
294 $output = $input . mb_substr(str_repeat($pad, ceil($right / $pad_length)), 0, $right, $charset);
295 break;
298 return $output;