Merging version 2.1 RC to main trunk.
[fa-stable.git] / includes / lang / language.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 // Prevent register_globals vulnerability
13 if (isset($_GET['path_to_root']) || isset($_POST['path_to_root']))
14         die("Restricted access");
15 include_once($path_to_root . "/lang/installed_languages.inc");
16 include_once($path_to_root . "/includes/lang/gettext.php");
17
18 class language 
19 {
20         var $name;
21         var $code;                      // eg. ar_EG, en_GB
22         var $encoding;          // eg. UTF-8, CP1256, ISO8859-1
23         var     $dir;                   // Currently support for Left-to-Right (ltr) and
24                                                 // Right-To-Left (rtl)
25         var $is_locale_file;
26         
27         function language($name, $code, $encoding) 
28         {
29                 $this->name = $name;
30                 $this->code = $code;
31                 $this->encoding = $encoding;
32                 $this->dir = "ltr";
33         }
34
35         function get_language_dir() 
36         {
37                 return "lang/" . $this->code;
38         }
39
40         function get_current_language_dir() 
41         {
42                 $lang = $_SESSION['language'];
43                 return "lang/" . $lang->code;
44         }
45
46         function set_language($code) 
47         {
48             global $comp_path, $path_to_root;
49                 
50                 $changed = $_SESSION['language']->code != $code;
51                 if (isset($_SESSION['languages'][$code]) && $changed)
52                 {
53                 // flush cache as we can use several languages in one account
54                     flush_dir($comp_path.'/'.user_company().'/js_cache');
55                     $_SESSION['language'] = $_SESSION['languages'][$code];
56                         $locale = $path_to_root . "/lang/" . $_SESSION['language']->code . "/locale.inc";
57                         // check id file exists only once for session
58                         $_SESSION['language']->is_locale_file = file_exists($locale);
59                 }
60
61                 $lang = $_SESSION['language'];
62                 get_text::set_language($lang->code, $lang->encoding);
63                 get_text::add_domain($lang->code, $path_to_root . "/lang");
64                 
65                 // Necessary for ajax calls. Due to bug in php 4.3.10 for this 
66                 // version set globally in php.ini
67                 ini_set('default_charset', $lang->encoding);
68
69                 if (isset($_SESSION['App']) && $changed)
70                         $_SESSION['App']->init(); // refresh menu
71         }
72
73         /**
74          * This method loads an array of language objects into a session variable
75      * called $_SESSIONS['languages']. Only supported languages are added.
76      */
77         function load_languages() 
78         {
79                 global $installed_languages, $dflt_lang;
80
81                 $_SESSION['languages'] = array();
82
83         foreach ($installed_languages as $lang) 
84         {
85                         $l = new language($lang['name'],$lang['code'],$lang['encoding']);
86                         if (isset($lang['rtl']))
87                                 $l->dir = "rtl";
88                         $_SESSION['languages'][$l->code] = $l;
89         }
90
91                 if (!isset($_SESSION['language']))
92                         $_SESSION['language'] = $_SESSION['languages'][$dflt_lang];
93         }
94
95 }
96 /*
97         Test if named function is defined in locale.inc file.
98 */
99 function has_locale($fun=null)
100 {
101         global $path_to_root;
102         
103         if ($_SESSION['language']->is_locale_file)
104         {
105                 global $path_to_root;
106                 include_once($path_to_root . "/lang/" . 
107                         $_SESSION['language']->code . "/locale.inc");
108
109                 if (!isset($fun) || function_exists($fun))
110                         return true;
111         }
112         return false;
113 }
114
115 function _set($key,$value) 
116 {
117         get_text::set_var($key,$value);
118 }
119
120 if (!function_exists("_")) 
121 {
122         function _($text) 
123         {
124                 $retVal = get_text::gettext($text);
125                 if ($retVal == "")
126                         return $text;
127                 return $retVal;
128         }
129 }
130 ?>