Page reload problem, progress bar in backup/restore,
[fa-stable.git] / includes / lang / language.php
1 <?php
2 /**********************************************************************
3     Copyright (C) 2008  FrontAccounting, LLC.
4         Released under the terms of the GNU Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-3.0.html>.
11 ***********************************************************************/
12 if (!isset($path_to_root) || isset($_GET['path_to_root']) || isset($_POST['path_to_root']))
13         die("Restricted access");
14 include_once($path_to_root . "/lang/installed_languages.inc");
15 include_once($path_to_root . "/includes/lang/gettext.php");
16
17 class language 
18 {
19         var $name;
20         var $code;                      // eg. ar_EG, en_GB
21         var $encoding;          // eg. UTF-8, CP1256, ISO8859-1
22         var     $dir;                   // Currently support for Left-to-Right (ltr) and
23                                                 // Right-To-Left (rtl)
24         var $is_locale_file;
25         
26         function language($name, $code, $encoding) 
27         {
28                 $this->name = $name;
29                 $this->code = $code;
30                 $this->encoding = $encoding;
31                 $this->dir = "ltr";
32         }
33
34         function get_language_dir() 
35         {
36                 return "lang/" . $this->code;
37         }
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                 if (isset($_SESSION['languages'][$code]) &&
51                         $_SESSION['language'] != $_SESSION['languages'][$code]) 
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                     //reload_page("");
60                         meta_forward($_SERVER['PHP_SELF']);
61                 }
62         }
63
64         /**
65          * This method loads an array of language objects into a session variable
66      * called $_SESSIONS['languages']. Only supported languages are added.
67      */
68         function load_languages() 
69         {
70                 global $installed_languages;
71
72                 $_SESSION['languages'] = array();
73
74         foreach ($installed_languages as $lang) 
75         {
76                         $l = new language($lang['name'],$lang['code'],$lang['encoding']);
77                         if (isset($lang['rtl']))
78                                 $l->dir = "rtl";
79                         $_SESSION['languages'][$l->code] = $l;
80         }
81
82                 if (!isset($_SESSION['language']))
83                         $_SESSION['language'] = $_SESSION['languages']['en_GB'];
84         }
85
86 }
87 /*
88         Test if named function is defined in locale.inc file.
89 */
90 function has_locale($fun=null)
91 {
92         global $path_to_root;
93         
94         if ($_SESSION['language']->is_locale_file)
95         {
96                 global $path_to_root;
97                 include_once($path_to_root . "/lang/" . 
98                         $_SESSION['language']->code . "/locale.inc");
99
100                 if (!isset($fun) || function_exists($fun))
101                         return true;
102         }
103         return false;
104 }
105
106 session_name('FrontAccounting'.user_company());
107 session_start();
108 // this is to fix the "back-do-you-want-to-refresh" issue - thanx PHPFreaks
109 header("Cache-control: private");
110
111 // Page Initialisation
112 if (!isset($_SESSION['languages'])) 
113 {
114         language::load_languages();
115 }
116
117 $lang = $_SESSION['language'];
118
119 // get_text support
120 get_text::init();
121 get_text::set_language($lang->code, $lang->encoding);
122 //get_text::add_domain("wa", $path_to_root . "/lang");
123 get_text::add_domain($lang->code, $path_to_root . "/lang");
124 // Unnecessary for ajax calls. 
125 // Due to bug in php 4.3.10 for this version set globally in php.ini
126 ini_set('default_charset', $_SESSION['language']->encoding);
127
128 if (!function_exists("_")) 
129 {
130         function _($text) 
131         {
132                 $retVal = get_text::gettext($text);
133                 if ($retVal == "")
134                         return $text;
135                 return $retVal;
136         }
137 }
138
139 function _set($key,$value) 
140 {
141         get_text::set_var($key,$value);
142 }
143 /*
144 function reload_page($msg) 
145 {
146 //      header("Location: $_SERVER['PHP_SELF']."");
147 //      exit;
148         echo "<html>";
149         echo "<head>";
150     echo "<title>Changing Languages</title>";
151         echo '<meta http-equiv="refresh" content="0;url=' . $_SERVER['PHP_SELF'] . '">';
152         echo '</head>';
153         echo '<body>';
154         echo '<div>';
155         if ($msg != "")
156                 echo $msg . " " . $_SERVER['PHP_SELF'];
157         echo "</div>";  
158         echo "</body>";
159         echo "</html>";
160 }
161 */
162
163
164 ?>