more internationalization and input validation project
[openemr.git] / library / translation.inc.php
blobb7077311fa964814606a94e2b5a80eb607fec50e
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"; }
38 $string = "$prepend" . "$string" . "$append";
39 if ($mode=='e') {
40 echo $string;
41 } else {
42 return $string;
46 // ----------- xl() function wrappers ------------------------------
48 // Use above xl() function the majority of time for translations. The
49 // below wrappers are only for specific situations in order to support
50 // granular control of translations in certain parts of OpenEMR.
51 // Wrappers:
52 // xl_list_label()
53 // xl_layout_label()
54 // xl_gacl_group()
55 // xl_form_title()
56 // xl_document_category()
57 // xl_appt_category()
59 // Added 5-09 by BM for translation of list labels (when applicable)
60 // Only translates if the $GLOBALS['translate_lists'] is set to true.
61 function xl_list_label($constant,$mode='r',$prepend='',$append='') {
62 if ($GLOBALS['translate_lists']) {
63 // TRANSLATE
64 if ($mode == "e") {
65 xl($constant,$mode,$prepend,$append);
67 else {
68 return xl($constant,$mode,$prepend,$append);
71 else {
72 // DO NOT TRANSLATE
73 if ($mode == "e") {
74 echo $prepend.$constant.$append;
76 else {
77 return $prepend.$constant.$append;
81 // Added 5-09 by BM for translation of layout labels (when applicable)
82 // Only translates if the $GLOBALS['translate_layout'] is set to true.
83 function xl_layout_label($constant,$mode='r',$prepend='',$append='') {
84 if ($GLOBALS['translate_layout']) {
85 // TRANSLATE
86 if ($mode == "e") {
87 xl($constant,$mode,$prepend,$append);
89 else {
90 return xl($constant,$mode,$prepend,$append);
93 else {
94 // DO NOT TRANSLATE
95 if ($mode == "e") {
96 echo $prepend.$constant.$append;
98 else {
99 return $prepend.$constant.$append;
103 // Added 6-2009 by BM for translation of access control group labels
104 // (when applicable)
105 // Only translates if the $GLOBALS['translate_gacl_groups'] is set to true.
106 function xl_gacl_group($constant,$mode='r',$prepend='',$append='') {
107 if ($GLOBALS['translate_gacl_groups']) {
108 // TRANSLATE
109 if ($mode == "e") {
110 xl($constant,$mode,$prepend,$append);
112 else {
113 return xl($constant,$mode,$prepend,$append);
116 else {
117 // DO NOT TRANSLATE
118 if ($mode == "e") {
119 echo $prepend.$constant.$append;
121 else {
122 return $prepend.$constant.$append;
126 // Added 6-2009 by BM for translation of patient form (notes) titles
127 // (when applicable)
128 // Only translates if the $GLOBALS['translate_form_titles'] is set to true.
129 function xl_form_title($constant,$mode='r',$prepend='',$append='') {
130 if ($GLOBALS['translate_form_titles']) {
131 // TRANSLATE
132 if ($mode == "e") {
133 xl($constant,$mode,$prepend,$append);
135 else {
136 return xl($constant,$mode,$prepend,$append);
139 else {
140 // DO NOT TRANSLATE
141 if ($mode == "e") {
142 echo $prepend.$constant.$append;
144 else {
145 return $prepend.$constant.$append;
150 // Added 6-2009 by BM for translation of document categories
151 // (when applicable)
152 // Only translates if the $GLOBALS['translate_document_categories'] is set to true.
153 function xl_document_category($constant,$mode='r',$prepend='',$append='') {
154 if ($GLOBALS['translate_document_categories']) {
155 // TRANSLATE
156 if ($mode == "e") {
157 xl($constant,$mode,$prepend,$append);
159 else {
160 return xl($constant,$mode,$prepend,$append);
163 else {
164 // DO NOT TRANSLATE
165 if ($mode == "e") {
166 echo $prepend.$constant.$append;
168 else {
169 return $prepend.$constant.$append;
174 // Added 6-2009 by BM for translation of appointment categories
175 // (when applicable)
176 // Only translates if the $GLOBALS['translate_appt_categories'] is set to true.
177 function xl_appt_category($constant,$mode='r',$prepend='',$append='') {
178 if ($GLOBALS['translate_appt_categories']) {
179 // TRANSLATE
180 if ($mode == "e") {
181 xl($constant,$mode,$prepend,$append);
183 else {
184 return xl($constant,$mode,$prepend,$append);
187 else {
188 // DO NOT TRANSLATE
189 if ($mode == "e") {
190 echo $prepend.$constant.$append;
192 else {
193 return $prepend.$constant.$append;
197 // ---------------------------------------------------------------------------
199 // ---------------------------------
200 // Miscellaneous language translation functions
202 // Function to return the title of a language from the id
203 // @param integer (language id)
204 // return string (language title)
205 function getLanguageTitle($val) {
207 // validate language id
208 if (!empty($val)) {
209 $lang_id = $val;
211 else {
212 $lang_id = 1;
215 // get language title
216 $res = sqlStatement("select lang_description from lang_languages where lang_id = '".$lang_id."'");
217 for ($iter = 0;$row = sqlFetchArray($res);$iter++) $result[$iter] = $row;
218 $languageTitle = $result[0]{"lang_description"};
219 return $languageTitle;
222 //----------------------------------
224 // ----------------------------------------------------------------------------
226 HEADER HTML
228 shows some informations for pages html header
230 @param none
231 @return void
233 function html_header_show() {
235 // Below line was commented by the UTF-8 project on 05-2009 by BM.
236 // We commented this out since we are now standardizing encoding
237 // in the globals.php file.
238 // echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> '."\n";
242 // ----------------------------------------------------------------------------
244 * Returns a string padded to a certain length with another string.
246 * This method behaves exactly like str_pad but is multibyte safe.
248 * @param string $input The string to be padded.
249 * @param int $length The length of the resulting string.
250 * @param string $pad The string to pad the input string with. Must
251 * be in the same charset like the input string.
252 * @param const $type The padding type. One of STR_PAD_LEFT,
253 * STR_PAD_RIGHT, or STR_PAD_BOTH.
254 * @param string $charset The charset of the input and the padding
255 * strings.
257 * @return string The padded string.
259 function mb_strpad($input, $length, $pad = ' ', $type = STR_PAD_RIGHT, $charset = 'UTF-8') {
260 mb_internal_encoding($charset);
261 $mb_length = mb_strlen($input, $charset);
262 $sb_length = strlen($input);
263 $pad_length = mb_strlen($pad, $charset);
265 /* Return if we already have the length. */
266 if ($mb_length >= $length) {
267 return $input;
270 /* Shortcut for single byte strings. */
271 if ($mb_length == $sb_length && $pad_length == strlen($pad)) {
272 return str_pad($input, $length, $pad, $type);
275 switch ($type) {
276 case STR_PAD_LEFT:
277 $left = $length - $mb_length;
278 $output = mb_substr(str_repeat($pad, ceil($left / $pad_length)), 0, $left, $charset) . $input;
279 break;
280 case STR_PAD_BOTH:
281 $left = floor(($length - $mb_length) / 2);
282 $right = ceil(($length - $mb_length) / 2);
283 $output = mb_substr(str_repeat($pad, ceil($left / $pad_length)), 0, $left, $charset) .
284 $input .
285 mb_substr(str_repeat($pad, ceil($right / $pad_length)), 0, $right, $charset);
286 break;
287 case STR_PAD_RIGHT:
288 $right = $length - $mb_length;
289 $output = $input . mb_substr(str_repeat($pad, ceil($right / $pad_length)), 0, $right, $charset);
290 break;
293 return $output;