Refactoring. Replacing getKey() with get().
[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   // getKey is a legacy function that we are replacing with get.
56   function getKey($kword) {
57     return $this->get($kword);
58   }
59
60   // TODO: refactoring ongoing down from here...
61     function getWeekDayName($id) {
62       $id = (int) $id;
63       return $this->weekdayNames[$id];
64     }
65
66     function load($localName) {
67     $kw = array();
68     $filename = strtolower($localName) . '.lang.php';
69     $inc_filename = RESOURCE_DIR . '/' . $this->defaultLang . '.lang.php';
70
71     if (file_exists($inc_filename)) {
72       include($inc_filename);
73
74       $this->monthNames = $i18n_months;
75       $this->weekdayNames = $i18n_weekdays;
76
77         $this->weekdayShortNames = $i18n_weekdays_short;
78 //      if (defined('SHOW_HOLIDAYS') && isTrue(SHOW_HOLIDAYS)) {
79         $this->holidays = $i18n_holidays;
80 //      }
81
82       foreach ($i18n_key_words as $kword=>$value) {
83         $pos = strpos($kword, ".");
84         if (!($pos === false)) {
85           $p = explode(".", $kword);
86           $str = "";
87           foreach ($p as $w) {
88             $str .= "[\"".$w."\"]";
89           }
90           eval("\$this->keys".$str."='".$value."';");
91         } else {
92           $this->keys[$kword] = $value;
93         }
94       }
95     }
96
97     $inc_filename = RESOURCE_DIR . '/' . $filename;
98     if (file_exists($inc_filename) && ($localName != $this->defaultLang)) {
99       require($inc_filename);
100
101       $this->lang = $localName;
102       $this->monthNames = $i18n_months;
103       $this->weekdayNames = $i18n_weekdays;
104         $this->weekdayShortNames = $i18n_weekdays_short;
105 //      if (defined('SHOW_HOLIDAYS') && isTrue(SHOW_HOLIDAYS)) {
106         $this->holidays = $i18n_holidays;
107 //      }
108       foreach ($i18n_key_words as $kword=>$value) {
109         if (!$value) continue;
110         $pos = strpos($kword, ".");
111         if (!($pos === false)) {
112           $p = explode(".", $kword);
113           $str = "";
114           foreach ($p as $w) {
115              $str .= "[\"".$w."\"]";
116           }
117           eval("\$this->keys".$str."='".$value."';");
118         } else {
119           $this->keys[$kword] = $value;
120         }
121       }
122       return true;
123     }
124   }
125
126   function hasLang($lang)
127   {
128     $filename = RESOURCE_DIR . '/' . strtolower($lang) . '.lang.php';
129     return file_exists($filename);
130   }
131
132   // getBrowserLanguage() returns a first supported language from browser settings.
133   function getBrowserLanguage()
134   {
135     $acclang = @$_SERVER['HTTP_ACCEPT_LANGUAGE'];
136     if (empty($acclang)) {
137       return false;
138     }
139     $lang_prefs = explode(',', $acclang);
140     foreach ($lang_prefs as $lang_pref) {
141       $lang_pref_parts = explode(';', trim($lang_pref));
142       $lang = $lang_pref_parts[0];
143       if ($this->hasLang($lang)) {
144         return $lang; // Return full language designation (if available), such as pt-BR.
145       }
146
147       if (strlen($lang) <= 2)
148         continue; // Do not bother determining main language because we already have it.
149
150       $lang_parts = explode('-', trim($lang));
151       $lang_main = $lang_parts[0];
152       if ($lang_main != $lang && $this->hasLang($lang_main)) {
153         return $lang_main; // Return main language designation, such as pt.
154       }
155     }
156     return false;
157   }
158
159   // getLangFileList() returns a list of language files.
160   static function getLangFileList() {
161     $fileList = array();
162     $d = @opendir(RESOURCE_DIR);
163     while (($file = @readdir($d))) {
164       if (($file != ".") && ($file != "..")) {
165         if (strpos($file, ".lang.php")) {
166           $fileList[] = @basename($file);
167         }
168       }
169     }
170     @closedir($d);
171     return $fileList;
172   }
173
174   static function getLangFromFilename($filename)
175   {
176     return substr($filename, 0, strpos($filename, '.'));
177   }
178 }