15799c23ee909a47685094f731eb2a3d2337e260
[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   // get - obtains a localized value from $keys array.
39   function get($key) {
40     $value = '';
41     $pos = strpos($key, '.'); // Keywords can have separating dots such as in form.login.about.
42     if (!($pos === false)) {
43       $words = explode('.', $key);
44       $str = '';
45       foreach ($words as $word) {
46         $str .= "['".$word."']";
47       }
48       eval("\$value = \$this->keys".$str.";");
49     } else {
50       $value = $this->keys[$key];
51     }
52     return $value;
53   }
54
55   // TODO: refactoring ongoing down from here...
56     function getWeekDayName($id) {
57       $id = (int) $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         $this->holidays = $i18n_holidays;
74
75       foreach ($i18n_key_words as $kword=>$value) {
76         $pos = strpos($kword, ".");
77         if (!($pos === false)) {
78           $p = explode(".", $kword);
79           $str = "";
80           foreach ($p as $w) {
81             $str .= "[\"".$w."\"]";
82           }
83           eval("\$this->keys".$str."='".$value."';");
84         } else {
85           $this->keys[$kword] = $value;
86         }
87       }
88     }
89
90     $inc_filename = RESOURCE_DIR . '/' . $filename;
91     if (file_exists($inc_filename) && ($localName != $this->defaultLang)) {
92       require($inc_filename);
93
94       $this->lang = $localName;
95       $this->monthNames = $i18n_months;
96       $this->weekdayNames = $i18n_weekdays;
97         $this->weekdayShortNames = $i18n_weekdays_short;
98         $this->holidays = $i18n_holidays;
99       foreach ($i18n_key_words as $kword=>$value) {
100         if (!$value) continue;
101         $pos = strpos($kword, ".");
102         if (!($pos === false)) {
103           $p = explode(".", $kword);
104           $str = "";
105           foreach ($p as $w) {
106              $str .= "[\"".$w."\"]";
107           }
108           eval("\$this->keys".$str."='".$value."';");
109         } else {
110           $this->keys[$kword] = $value;
111         }
112       }
113       return true;
114     }
115   }
116
117   function hasLang($lang)
118   {
119     $filename = RESOURCE_DIR . '/' . strtolower($lang) . '.lang.php';
120     return file_exists($filename);
121   }
122
123   // getBrowserLanguage() returns a first supported language from browser settings.
124   function getBrowserLanguage()
125   {
126     $acclang = @$_SERVER['HTTP_ACCEPT_LANGUAGE'];
127     if (empty($acclang)) {
128       return false;
129     }
130     $lang_prefs = explode(',', $acclang);
131     foreach ($lang_prefs as $lang_pref) {
132       $lang_pref_parts = explode(';', trim($lang_pref));
133       $lang = $lang_pref_parts[0];
134       if ($this->hasLang($lang)) {
135         return $lang; // Return full language designation (if available), such as pt-BR.
136       }
137
138       if (strlen($lang) <= 2)
139         continue; // Do not bother determining main language because we already have it.
140
141       $lang_parts = explode('-', trim($lang));
142       $lang_main = $lang_parts[0];
143       if ($lang_main != $lang && $this->hasLang($lang_main)) {
144         return $lang_main; // Return main language designation, such as pt.
145       }
146     }
147     return false;
148   }
149
150   // getLangFileList() returns a list of language files.
151   static function getLangFileList() {
152     $fileList = array();
153     $d = @opendir(RESOURCE_DIR);
154     while (($file = @readdir($d))) {
155       if (($file != ".") && ($file != "..")) {
156         if (strpos($file, ".lang.php")) {
157           $fileList[] = @basename($file);
158         }
159       }
160     }
161     @closedir($d);
162     return $fileList;
163   }
164
165   static function getLangFromFilename($filename)
166   {
167     return substr($filename, 0, strpos($filename, '.'));
168   }
169 }