8960928377024f8071c6cf0e0b1dfaaab1eb4e29
[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, $dir = 'ltr') 
28         {
29                 $this->name = $name;
30                 $this->code = $code;
31                 $this->encoding = $encoding;
32                 $this->dir = $dir;
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, $installed_languages;
49
50                 $changed = $this->code != $code;
51                 $lang = array_search_value($code, $installed_languages, 'code');
52
53                 if ($lang && $changed)
54                 {
55                 // flush cache as we can use several languages in one account
56                         flush_dir($comp_path.'/'.user_company().'/js_cache');
57
58                         $this->name = $lang['name'];
59                         $this->code = $lang['code'];
60                         $this->encoding = $lang['encoding'];
61                         $this->dir = isset($lang['rtl']) ? 'rtl' : 'ltr';
62                         $locale = $path_to_root . "/lang/" . $this->code . "/locale.inc";
63                         $this->is_locale_file = file_exists($locale);
64                 }
65
66                 $_SESSION['get_text']->set_language($this->code, $this->encoding);
67                 $_SESSION['get_text']->add_domain($this->code, $path_to_root . "/lang");
68
69                 // Necessary for ajax calls. Due to bug in php 4.3.10 for this 
70                 // version set globally in php.ini
71                 ini_set('default_charset', $this->encoding);
72
73                 if (isset($_SESSION['App']) && $changed)
74                         $_SESSION['App']->init(); // refresh menu
75         }
76 }
77
78 function _set($key,$value) 
79 {
80         $_SESSION['get_text']->set_var($key,$value);
81 }
82
83 if (!function_exists("_")) 
84 {
85         function _($text) 
86         {
87                 $retVal = $_SESSION['get_text']->gettext($text);
88                 if ($retVal == "")
89                         return $text;
90                 return $retVal;
91         }
92 }
93 ?>