some comment changes for prior commit
[openemr.git] / library / translation.inc.php
blob650cfc07a0f35b48a0082ede293e838e7330fa70
1 <?php
2 include_once(dirname(__FILE__) . '/sql.inc'); // fixes vulnerability with register_globals
3 require_once(dirname(__FILE__) . '/formdata.inc.php');
5 // Translation function
6 // This is the translation engine
7 // Note that it is recommended to no longer use the mode, prepend, or append
8 // parameters, since this is not compatible with the htmlspecialchars() php
9 // function.
10 function xl($constant,$mode='r',$prepend='',$append='') {
11 // set language id
12 if (!empty($_SESSION['language_choice'])) {
13 $lang_id = $_SESSION['language_choice'];
15 else {
16 $lang_id = 1;
19 if ($lang_id == 1 && !empty($GLOBALS['skip_english_translation'])) {
20 // language id = 1, so no need to translate
21 // -- remove comments
22 $string = preg_replace('/\{\{.*\}\}/', '', $constant);
24 else {
25 // TRANSLATE
26 // first, clean lines
27 // convert new lines to spaces and remove windows end of lines
28 $patterns = array ('/\n/','/\r/');
29 $replace = array (' ','');
30 $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 == '') { $string = "$constant"; }
41 // remove dangerous characters and remove comments
42 $patterns = array ('/\n/','/\r/','/"/',"/'/",'/\{\{.*\}\}/');
43 $replace = array (' ','','`','`','');
44 $string = preg_replace($patterns, $replace, $string);
47 $string = "$prepend" . "$string" . "$append";
48 if ($mode=='e') {
49 echo $string;
50 } else {
51 return $string;
55 // ----------- xl() function wrappers ------------------------------
57 // Use above xl() function the majority of time for translations. The
58 // below wrappers are only for specific situations in order to support
59 // granular control of translations in certain parts of OpenEMR.
60 // Wrappers:
61 // xl_list_label()
62 // xl_layout_label()
63 // xl_gacl_group()
64 // xl_form_title()
65 // xl_document_category()
66 // xl_appt_category()
68 // Added 5-09 by BM for translation of list labels (when applicable)
69 // Only translates if the $GLOBALS['translate_lists'] is set to true.
70 function xl_list_label($constant,$mode='r',$prepend='',$append='') {
71 if ($GLOBALS['translate_lists']) {
72 // TRANSLATE
73 if ($mode == "e") {
74 xl($constant,$mode,$prepend,$append);
76 else {
77 return xl($constant,$mode,$prepend,$append);
80 else {
81 // DO NOT TRANSLATE
82 if ($mode == "e") {
83 echo $prepend.$constant.$append;
85 else {
86 return $prepend.$constant.$append;
90 // Added 5-09 by BM for translation of layout labels (when applicable)
91 // Only translates if the $GLOBALS['translate_layout'] is set to true.
92 function xl_layout_label($constant,$mode='r',$prepend='',$append='') {
93 if ($GLOBALS['translate_layout']) {
94 // TRANSLATE
95 if ($mode == "e") {
96 xl($constant,$mode,$prepend,$append);
98 else {
99 return xl($constant,$mode,$prepend,$append);
102 else {
103 // DO NOT TRANSLATE
104 if ($mode == "e") {
105 echo $prepend.$constant.$append;
107 else {
108 return $prepend.$constant.$append;
112 // Added 6-2009 by BM for translation of access control group labels
113 // (when applicable)
114 // Only translates if the $GLOBALS['translate_gacl_groups'] is set to true.
115 function xl_gacl_group($constant,$mode='r',$prepend='',$append='') {
116 if ($GLOBALS['translate_gacl_groups']) {
117 // TRANSLATE
118 if ($mode == "e") {
119 xl($constant,$mode,$prepend,$append);
121 else {
122 return xl($constant,$mode,$prepend,$append);
125 else {
126 // DO NOT TRANSLATE
127 if ($mode == "e") {
128 echo $prepend.$constant.$append;
130 else {
131 return $prepend.$constant.$append;
135 // Added 6-2009 by BM for translation of patient form (notes) titles
136 // (when applicable)
137 // Only translates if the $GLOBALS['translate_form_titles'] is set to true.
138 function xl_form_title($constant,$mode='r',$prepend='',$append='') {
139 if ($GLOBALS['translate_form_titles']) {
140 // TRANSLATE
141 if ($mode == "e") {
142 xl($constant,$mode,$prepend,$append);
144 else {
145 return xl($constant,$mode,$prepend,$append);
148 else {
149 // DO NOT TRANSLATE
150 if ($mode == "e") {
151 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='') {
163 if ($GLOBALS['translate_document_categories']) {
164 // TRANSLATE
165 if ($mode == "e") {
166 xl($constant,$mode,$prepend,$append);
168 else {
169 return xl($constant,$mode,$prepend,$append);
172 else {
173 // DO NOT TRANSLATE
174 if ($mode == "e") {
175 echo $prepend.$constant.$append;
177 else {
178 return $prepend.$constant.$append;
183 // Added 6-2009 by BM for translation of appointment categories
184 // (when applicable)
185 // Only translates if the $GLOBALS['translate_appt_categories'] is set to true.
186 function xl_appt_category($constant,$mode='r',$prepend='',$append='') {
187 if ($GLOBALS['translate_appt_categories']) {
188 // TRANSLATE
189 if ($mode == "e") {
190 xl($constant,$mode,$prepend,$append);
192 else {
193 return xl($constant,$mode,$prepend,$append);
196 else {
197 // DO NOT TRANSLATE
198 if ($mode == "e") {
199 echo $prepend.$constant.$append;
201 else {
202 return $prepend.$constant.$append;
206 // ---------------------------------------------------------------------------
208 // ---------------------------------
209 // Miscellaneous language translation functions
211 // Function to return the title of a language from the id
212 // @param integer (language id)
213 // return string (language title)
214 function getLanguageTitle($val) {
216 // validate language id
217 if (!empty($val)) {
218 $lang_id = $val;
220 else {
221 $lang_id = 1;
224 // get language title
225 $res = sqlStatement("select lang_description from lang_languages where lang_id =?",array($lang_id));
226 for ($iter = 0;$row = sqlFetchArray($res);$iter++) $result[$iter] = $row;
227 $languageTitle = $result[0]{"lang_description"};
228 return $languageTitle;
231 //----------------------------------
233 // ----------------------------------------------------------------------------
235 HEADER HTML
237 shows some informations for pages html header
239 @param none
240 @return void
242 function html_header_show() {
244 // Below line was commented by the UTF-8 project on 05-2009 by BM.
245 // We commented this out since we are now standardizing encoding
246 // in the globals.php file.
247 // echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> '."\n";
251 // ----------------------------------------------------------------------------
253 * Returns a string padded to a certain length with another string.
255 * This method behaves exactly like str_pad but is multibyte safe.
257 * @param string $input The string to be padded.
258 * @param int $length The length of the resulting string.
259 * @param string $pad The string to pad the input string with. Must
260 * be in the same charset like the input string.
261 * @param const $type The padding type. One of STR_PAD_LEFT,
262 * STR_PAD_RIGHT, or STR_PAD_BOTH.
263 * @param string $charset The charset of the input and the padding
264 * strings.
266 * @return string The padded string.
268 function mb_strpad($input, $length, $pad = ' ', $type = STR_PAD_RIGHT, $charset = 'UTF-8') {
269 mb_internal_encoding($charset);
270 $mb_length = mb_strlen($input, $charset);
271 $sb_length = strlen($input);
272 $pad_length = mb_strlen($pad, $charset);
274 /* Return if we already have the length. */
275 if ($mb_length >= $length) {
276 return $input;
279 /* Shortcut for single byte strings. */
280 if ($mb_length == $sb_length && $pad_length == strlen($pad)) {
281 return str_pad($input, $length, $pad, $type);
284 switch ($type) {
285 case STR_PAD_LEFT:
286 $left = $length - $mb_length;
287 $output = mb_substr(str_repeat($pad, ceil($left / $pad_length)), 0, $left, $charset) . $input;
288 break;
289 case STR_PAD_BOTH:
290 $left = floor(($length - $mb_length) / 2);
291 $right = ceil(($length - $mb_length) / 2);
292 $output = mb_substr(str_repeat($pad, ceil($left / $pad_length)), 0, $left, $charset) .
293 $input .
294 mb_substr(str_repeat($pad, ceil($right / $pad_length)), 0, $right, $charset);
295 break;
296 case STR_PAD_RIGHT:
297 $right = $length - $mb_length;
298 $output = $input . mb_substr(str_repeat($pad, ceil($right / $pad_length)), 0, $right, $charset);
299 break;
302 return $output;