Merge branch 'master' of git://github.com/openemr/openemr
[openemr.git] / library / translation.inc.php
blob1a17aa13439a6098b10f98db4e69313ddfee6454
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 function xl($constant,$mode='r',$prepend='',$append='') {
8 // set language id
9 if (!empty($_SESSION['language_choice'])) {
10 $lang_id = $_SESSION['language_choice'];
12 else {
13 $lang_id = 1;
16 if ($lang_id == 1 && !empty($GLOBALS['skip_english_translation'])) {
17 // language id = 1, so no need to translate
18 $string = $constant;
20 else {
21 // TRANSLATE
22 // first, clean lines
23 // convert new lines to spaces and remove windows end of lines
24 $patterns = array ('/\n/','/\r/');
25 $replace = array (' ','');
26 $constant = preg_replace($patterns, $replace, $constant);
28 // second, attempt translation
29 $sql="SELECT * FROM lang_definitions JOIN lang_constants ON " .
30 "lang_definitions.cons_id = lang_constants.cons_id WHERE " .
31 "lang_id='$lang_id' AND constant_name = '" .
32 add_escape_custom($constant) . "' LIMIT 1";
33 $res = SqlStatement($sql);
34 $row = SqlFetchArray($res);
35 $string = $row['definition'];
36 if ($string == '') { $string = "$constant"; }
38 // remove dangerous characters
39 $patterns = array ('/\n/','/\r/','/"/',"/'/");
40 $replace = array (' ','','`','`');
41 $string = preg_replace($patterns, $replace, $string);
44 $string = "$prepend" . "$string" . "$append";
45 if ($mode=='e') {
46 echo $string;
47 } else {
48 return $string;
52 // ----------- xl() function wrappers ------------------------------
54 // Use above xl() function the majority of time for translations. The
55 // below wrappers are only for specific situations in order to support
56 // granular control of translations in certain parts of OpenEMR.
57 // Wrappers:
58 // xl_list_label()
59 // xl_layout_label()
60 // xl_gacl_group()
61 // xl_form_title()
62 // xl_document_category()
63 // xl_appt_category()
65 // Added 5-09 by BM for translation of list labels (when applicable)
66 // Only translates if the $GLOBALS['translate_lists'] is set to true.
67 function xl_list_label($constant,$mode='r',$prepend='',$append='') {
68 if ($GLOBALS['translate_lists']) {
69 // TRANSLATE
70 if ($mode == "e") {
71 xl($constant,$mode,$prepend,$append);
73 else {
74 return xl($constant,$mode,$prepend,$append);
77 else {
78 // DO NOT TRANSLATE
79 if ($mode == "e") {
80 echo $prepend.$constant.$append;
82 else {
83 return $prepend.$constant.$append;
87 // Added 5-09 by BM for translation of layout labels (when applicable)
88 // Only translates if the $GLOBALS['translate_layout'] is set to true.
89 function xl_layout_label($constant,$mode='r',$prepend='',$append='') {
90 if ($GLOBALS['translate_layout']) {
91 // TRANSLATE
92 if ($mode == "e") {
93 xl($constant,$mode,$prepend,$append);
95 else {
96 return xl($constant,$mode,$prepend,$append);
99 else {
100 // DO NOT TRANSLATE
101 if ($mode == "e") {
102 echo $prepend.$constant.$append;
104 else {
105 return $prepend.$constant.$append;
109 // Added 6-2009 by BM for translation of access control group labels
110 // (when applicable)
111 // Only translates if the $GLOBALS['translate_gacl_groups'] is set to true.
112 function xl_gacl_group($constant,$mode='r',$prepend='',$append='') {
113 if ($GLOBALS['translate_gacl_groups']) {
114 // TRANSLATE
115 if ($mode == "e") {
116 xl($constant,$mode,$prepend,$append);
118 else {
119 return xl($constant,$mode,$prepend,$append);
122 else {
123 // DO NOT TRANSLATE
124 if ($mode == "e") {
125 echo $prepend.$constant.$append;
127 else {
128 return $prepend.$constant.$append;
132 // Added 6-2009 by BM for translation of patient form (notes) titles
133 // (when applicable)
134 // Only translates if the $GLOBALS['translate_form_titles'] is set to true.
135 function xl_form_title($constant,$mode='r',$prepend='',$append='') {
136 if ($GLOBALS['translate_form_titles']) {
137 // TRANSLATE
138 if ($mode == "e") {
139 xl($constant,$mode,$prepend,$append);
141 else {
142 return xl($constant,$mode,$prepend,$append);
145 else {
146 // DO NOT TRANSLATE
147 if ($mode == "e") {
148 echo $prepend.$constant.$append;
150 else {
151 return $prepend.$constant.$append;
156 // Added 6-2009 by BM for translation of document categories
157 // (when applicable)
158 // Only translates if the $GLOBALS['translate_document_categories'] is set to true.
159 function xl_document_category($constant,$mode='r',$prepend='',$append='') {
160 if ($GLOBALS['translate_document_categories']) {
161 // TRANSLATE
162 if ($mode == "e") {
163 xl($constant,$mode,$prepend,$append);
165 else {
166 return xl($constant,$mode,$prepend,$append);
169 else {
170 // DO NOT TRANSLATE
171 if ($mode == "e") {
172 echo $prepend.$constant.$append;
174 else {
175 return $prepend.$constant.$append;
180 // Added 6-2009 by BM for translation of appointment categories
181 // (when applicable)
182 // Only translates if the $GLOBALS['translate_appt_categories'] is set to true.
183 function xl_appt_category($constant,$mode='r',$prepend='',$append='') {
184 if ($GLOBALS['translate_appt_categories']) {
185 // TRANSLATE
186 if ($mode == "e") {
187 xl($constant,$mode,$prepend,$append);
189 else {
190 return xl($constant,$mode,$prepend,$append);
193 else {
194 // DO NOT TRANSLATE
195 if ($mode == "e") {
196 echo $prepend.$constant.$append;
198 else {
199 return $prepend.$constant.$append;
203 // ---------------------------------------------------------------------------
205 // ---------------------------------
206 // Miscellaneous language translation functions
208 // Function to return the title of a language from the id
209 // @param integer (language id)
210 // return string (language title)
211 function getLanguageTitle($val) {
213 // validate language id
214 if (!empty($val)) {
215 $lang_id = $val;
217 else {
218 $lang_id = 1;
221 // get language title
222 $res = sqlStatement("select lang_description from lang_languages where lang_id = '".$lang_id."'");
223 for ($iter = 0;$row = sqlFetchArray($res);$iter++) $result[$iter] = $row;
224 $languageTitle = $result[0]{"lang_description"};
225 return $languageTitle;
228 //----------------------------------
230 // ----------------------------------------------------------------------------
232 HEADER HTML
234 shows some informations for pages html header
236 @param none
237 @return void
239 function html_header_show() {
241 // Below line was commented by the UTF-8 project on 05-2009 by BM.
242 // We commented this out since we are now standardizing encoding
243 // in the globals.php file.
244 // echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> '."\n";
248 // ----------------------------------------------------------------------------
250 * Returns a string padded to a certain length with another string.
252 * This method behaves exactly like str_pad but is multibyte safe.
254 * @param string $input The string to be padded.
255 * @param int $length The length of the resulting string.
256 * @param string $pad The string to pad the input string with. Must
257 * be in the same charset like the input string.
258 * @param const $type The padding type. One of STR_PAD_LEFT,
259 * STR_PAD_RIGHT, or STR_PAD_BOTH.
260 * @param string $charset The charset of the input and the padding
261 * strings.
263 * @return string The padded string.
265 function mb_strpad($input, $length, $pad = ' ', $type = STR_PAD_RIGHT, $charset = 'UTF-8') {
266 mb_internal_encoding($charset);
267 $mb_length = mb_strlen($input, $charset);
268 $sb_length = strlen($input);
269 $pad_length = mb_strlen($pad, $charset);
271 /* Return if we already have the length. */
272 if ($mb_length >= $length) {
273 return $input;
276 /* Shortcut for single byte strings. */
277 if ($mb_length == $sb_length && $pad_length == strlen($pad)) {
278 return str_pad($input, $length, $pad, $type);
281 switch ($type) {
282 case STR_PAD_LEFT:
283 $left = $length - $mb_length;
284 $output = mb_substr(str_repeat($pad, ceil($left / $pad_length)), 0, $left, $charset) . $input;
285 break;
286 case STR_PAD_BOTH:
287 $left = floor(($length - $mb_length) / 2);
288 $right = ceil(($length - $mb_length) / 2);
289 $output = mb_substr(str_repeat($pad, ceil($left / $pad_length)), 0, $left, $charset) .
290 $input .
291 mb_substr(str_repeat($pad, ceil($right / $pad_length)), 0, $right, $charset);
292 break;
293 case STR_PAD_RIGHT:
294 $right = $length - $mb_length;
295 $output = $input . mb_substr(str_repeat($pad, ceil($right / $pad_length)), 0, $right, $charset);
296 break;
299 return $output;