Add translation
[openemr.git] / library / translation.inc.php
blob99e3351bd71bc3fe010f749e4a4177901ff6a00e
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;
235 * Returns language directionality as string 'rtl' or 'ltr'
236 * @param int $lang_id language code
237 * @return string 'ltr' 'rtl'
238 * @author Amiel <amielel@matrix.co.il>
240 function getLanguageDir($lang_id) {
241 // validate language id
242 $lang_id = empty($lang_id) ? 1 : $lang_id;
243 // get language code
244 $row = sqlQuery('SELECT * FROM lang_languages WHERE lang_id = ?', array($lang_id));
246 return !empty($row['lang_is_rtl']) ? 'rtl' : 'ltr';
249 //----------------------------------
251 // ----------------------------------------------------------------------------
253 HEADER HTML
255 shows some informations for pages html header
257 @param none
258 @return void
260 function html_header_show() {
262 // Below line was commented by the UTF-8 project on 05-2009 by BM.
263 // We commented this out since we are now standardizing encoding
264 // in the globals.php file.
265 // echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> '."\n";
269 // ----------------------------------------------------------------------------
271 * Returns a string padded to a certain length with another string.
273 * This method behaves exactly like str_pad but is multibyte safe.
275 * @param string $input The string to be padded.
276 * @param int $length The length of the resulting string.
277 * @param string $pad The string to pad the input string with. Must
278 * be in the same charset like the input string.
279 * @param const $type The padding type. One of STR_PAD_LEFT,
280 * STR_PAD_RIGHT, or STR_PAD_BOTH.
281 * @param string $charset The charset of the input and the padding
282 * strings.
284 * @return string The padded string.
286 function mb_strpad($input, $length, $pad = ' ', $type = STR_PAD_RIGHT, $charset = 'UTF-8') {
287 mb_internal_encoding($charset);
288 $mb_length = mb_strlen($input, $charset);
289 $sb_length = strlen($input);
290 $pad_length = mb_strlen($pad, $charset);
292 /* Return if we already have the length. */
293 if ($mb_length >= $length) {
294 return $input;
297 /* Shortcut for single byte strings. */
298 if ($mb_length == $sb_length && $pad_length == strlen($pad)) {
299 return str_pad($input, $length, $pad, $type);
302 switch ($type) {
303 case STR_PAD_LEFT:
304 $left = $length - $mb_length;
305 $output = mb_substr(str_repeat($pad, ceil($left / $pad_length)), 0, $left, $charset) . $input;
306 break;
307 case STR_PAD_BOTH:
308 $left = floor(($length - $mb_length) / 2);
309 $right = ceil(($length - $mb_length) / 2);
310 $output = mb_substr(str_repeat($pad, ceil($left / $pad_length)), 0, $left, $charset) .
311 $input .
312 mb_substr(str_repeat($pad, ceil($right / $pad_length)), 0, $right, $charset);
313 break;
314 case STR_PAD_RIGHT:
315 $right = $length - $mb_length;
316 $output = $input . mb_substr(str_repeat($pad, ceil($right / $pad_length)), 0, $right, $charset);
317 break;
320 return $output;