Removed terminating PHP tag in more files
[timetracker.git] / WEB-INF / lib / I18n.class.php
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
10 // |
11 // | There are only two ways to violate the license:
12 // |
13 // | 1. To redistribute this code in source form, with the copyright
14 // |    notice or license removed or altered. (Distributing in compiled
15 // |    forms without embedded copyright notices is permitted).
16 // |
17 // | 2. To redistribute modified versions of this code in *any* form
18 // |    that bears insufficient indications that the modifications are
19 // |    not the work of the original author(s).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 class I18n {
30   var $lang = 'en'; // Language for the class.
31   var $defaultLang = 'en'; // English is the default language.
32   var $monthNames;
33   var $weekdayNames;
34   var $weekdayShortNames;
35   var $holidays;
36   var $keys = array(); // These are our localized strings.
37
38   // The getKey obtains localized keyword value.
39   function getKey($kword) {
40     $value = '';
41     $pos = strpos($kword, '.'); // Keywords can have separating dots such as in form.login.about.
42     if (!($pos === false)) {
43       $words = explode('.', $kword);
44       $str = '';
45       foreach ($words as $word) {
46         $str .= "['".$word."']";
47       }
48       eval("\$value = \$this->keys".$str.";");
49     } else {
50       $value = $this->keys[$kword];
51     }
52     return $value;
53   }
54
55   // TODO: refactoring ongoing down from here...
56     function getWeekDayName($id) {
57       $id = intval($id);
58       return $this->weekdayNames[$id];
59     }
60
61     function load($localName) {
62     $kw = array();
63     $filename = strtolower($localName) . '.lang.php';
64     $inc_filename = RESOURCE_DIR . '/' . $this->defaultLang . '.lang.php';
65
66     if (file_exists($inc_filename)) {
67       include($inc_filename);
68
69       $this->monthNames = $i18n_months;
70       $this->weekdayNames = $i18n_weekdays;
71         
72         $this->weekdayShortNames = $i18n_weekdays_short;
73       if (defined('SHOW_HOLIDAYS') && isTrue(SHOW_HOLIDAYS)) {
74         $this->holidays = $i18n_holidays;
75       }
76     
77       foreach ($i18n_key_words as $kword=>$value) {
78          $pos = strpos($kword, ".");
79          if (!($pos === false)) {
80                    $p = explode(".", $kword);
81                    $str = "";
82                    foreach ($p as $w) {
83                        $str .= "[\"".$w."\"]";
84                    }
85                    //$value = addslashes($value);
86                    eval("\$this->keys".$str."='".$value."';");
87                } else {
88                    $this->keys[$kword] = $value;
89                }
90       }
91     }
92
93     $inc_filename = RESOURCE_DIR . '/' . $filename;
94     if (file_exists($inc_filename) && ($localName != $this->defaultLang)) {
95       require($inc_filename);
96
97       $this->lang = $localName;
98       $this->monthNames = $i18n_months;
99       $this->weekdayNames = $i18n_weekdays;
100         $this->weekdayShortNames = $i18n_weekdays_short;
101       if (defined('SHOW_HOLIDAYS') && isTrue(SHOW_HOLIDAYS)) {
102         $this->holidays = $i18n_holidays;
103       }
104       foreach ($i18n_key_words as $kword=>$value) {
105          if (!$value) continue;
106          $pos = strpos($kword, ".");
107          if (!($pos === false)) {
108                    $p = explode(".", $kword);
109                    $str = "";
110                    foreach ($p as $w) {
111                        $str .= "[\"".$w."\"]";
112                    }
113                    //$value = addslashes($value);
114                    eval("\$this->keys".$str."='".$value."';");
115                } else {
116                    $this->keys[$kword] = $value;
117                }
118       }
119         return true;
120     }
121   }
122
123   function hasLang($lang)
124   {
125     $filename = RESOURCE_DIR . '/' . strtolower($lang) . '.lang.php';
126     return file_exists($filename);
127   }
128
129   function getBrowserLanguage()
130   {
131     $acclang = @$_SERVER['HTTP_ACCEPT_LANGUAGE'];
132     if (empty($acclang)) {
133       return "";
134     }
135     $lang_prefs = explode(',', $acclang);
136     foreach ($lang_prefs as $lang_pref) {
137       $lang_pref_parts = explode(';', trim($lang_pref));
138       $lang_parts = explode('-', trim($lang_pref_parts[0]));
139       $lang_main = $lang_parts[0];
140       if ($this->hasLang($lang_main)) {
141         return $lang_main;
142       }
143     }
144     return "";
145   }
146
147   // getLangFileList() returns a list of language files.
148   static function getLangFileList() {
149     $fileList = array();
150     $d = @opendir(RESOURCE_DIR);
151     while (($file = @readdir($d))) {
152       if (($file != ".") && ($file != "..")) {
153                 if (strpos($file, ".lang.php")) {
154                   $fileList[] = @basename($file);
155                 }
156       }
157     }
158     @closedir($d);
159     return $fileList;
160   }
161    
162   static function getLangFromFilename($filename)
163   {
164     return substr($filename, 0, strpos($filename, '.'));
165   }
166 }