1609cdc5db5b905bcc99b5de87e5fe4985922d44
[fa-stable.git] / includes / lang / language.inc
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.inc");
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 $version; // lang package version
26         var $is_locale_file;
27         
28         function language($name, $code, $encoding, $dir = 'ltr') 
29         {
30                 global $dflt_lang;
31                 
32                 $this->name = $name;
33                 $this->code = $code ? $code : ($dflt_lang ? $dflt_lang : 'C');
34                 $this->encoding = $encoding;
35                 $this->dir = $dir;
36         }
37
38         function get_language_dir() 
39         {
40                 return "lang/" . $this->code;
41         }
42
43         function get_current_language_dir() 
44         {
45                 $lang = $_SESSION['language'];
46                 return "lang/" . $lang->code;
47         }
48
49         function set_language($code) 
50         {
51             global $path_to_root, $installed_languages, $GetText;
52
53                 $lang = array_search_value($code, $installed_languages, 'code');
54                 $changed = $this->code != $code || $this->version != @$lang['version'];
55
56                 if ($lang && $changed)
57                 {
58                         $this->name = $lang['name'];
59                         $this->code = $lang['code'];
60                         $this->encoding = $lang['encoding'];
61                         $this->version = @$lang['version'];
62                         $this->dir = (isset($lang['rtl']) && $lang['rtl'] === true) ? 'rtl' : 'ltr';
63                         $locale = $path_to_root . "/lang/" . $this->code . "/locale.inc";
64                         $this->is_locale_file = file_exists($locale);
65                 }
66
67                 $GetText->set_language($this->code, $this->encoding);
68                 $GetText->add_domain($this->code, $path_to_root . "/lang", $this->version);
69
70                 // Necessary for ajax calls. Due to bug in php 4.3.10 for this 
71                 // version set globally in php.ini
72                 ini_set('default_charset', $this->encoding);
73
74                 if (isset($_SESSION['wa_current_user']) && $_SESSION['wa_current_user']->logged_in() && isset($_SESSION['App']) && $changed)
75                         $_SESSION['App']->init(); // refresh menu
76         }
77 }
78
79 if (!function_exists("_")) 
80 {
81         function _($text) 
82         {
83                 global $GetText;
84                 if (!isset($GetText)) // Don't allow using gettext if not is net.
85                         return $text;
86
87                 $retVal = $GetText->gettext($text);
88                 if ($retVal == "")
89                         return $text;
90                 return $retVal;
91         }
92 }