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.
11 // | There are only two ways to violate the license:
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).
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).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
30 var $lang = 'en'; // Language for the class.
31 var $defaultLang = 'en'; // English is the default language.
34 var $weekdayShortNames;
36 var $keys = array(); // These are our localized strings.
38 // The getKey obtains localized keyword value.
39 function getKey($kword) {
41 $pos = strpos($kword, '.'); // Keywords can have separating dots such as in form.login.about.
42 if (!($pos === false)) {
43 $words = explode('.', $kword);
45 foreach ($words as $word) {
46 $str .= "['".$word."']";
48 eval("\$value = \$this->keys".$str.";");
50 $value = $this->keys[$kword];
55 // TODO: refactoring ongoing down from here...
56 function getWeekDayName($id) {
58 return $this->weekdayNames[$id];
61 function load($localName) {
63 $filename = strtolower($localName) . '.lang.php';
64 $inc_filename = RESOURCE_DIR . '/' . $this->defaultLang . '.lang.php';
66 if (file_exists($inc_filename)) {
67 include($inc_filename);
69 $this->monthNames = $i18n_months;
70 $this->weekdayNames = $i18n_weekdays;
72 $this->weekdayShortNames = $i18n_weekdays_short;
73 // if (defined('SHOW_HOLIDAYS') && isTrue(SHOW_HOLIDAYS)) {
74 $this->holidays = $i18n_holidays;
77 foreach ($i18n_key_words as $kword=>$value) {
78 $pos = strpos($kword, ".");
79 if (!($pos === false)) {
80 $p = explode(".", $kword);
83 $str .= "[\"".$w."\"]";
85 eval("\$this->keys".$str."='".$value."';");
87 $this->keys[$kword] = $value;
92 $inc_filename = RESOURCE_DIR . '/' . $filename;
93 if (file_exists($inc_filename) && ($localName != $this->defaultLang)) {
94 require($inc_filename);
96 $this->lang = $localName;
97 $this->monthNames = $i18n_months;
98 $this->weekdayNames = $i18n_weekdays;
99 $this->weekdayShortNames = $i18n_weekdays_short;
100 // if (defined('SHOW_HOLIDAYS') && isTrue(SHOW_HOLIDAYS)) {
101 $this->holidays = $i18n_holidays;
103 foreach ($i18n_key_words as $kword=>$value) {
104 if (!$value) continue;
105 $pos = strpos($kword, ".");
106 if (!($pos === false)) {
107 $p = explode(".", $kword);
110 $str .= "[\"".$w."\"]";
112 eval("\$this->keys".$str."='".$value."';");
114 $this->keys[$kword] = $value;
121 function hasLang($lang)
123 $filename = RESOURCE_DIR . '/' . strtolower($lang) . '.lang.php';
124 return file_exists($filename);
127 // getBrowserLanguage() returns a first supported language from browser settings.
128 function getBrowserLanguage()
130 $acclang = @$_SERVER['HTTP_ACCEPT_LANGUAGE'];
131 if (empty($acclang)) {
134 $lang_prefs = explode(',', $acclang);
135 foreach ($lang_prefs as $lang_pref) {
136 $lang_pref_parts = explode(';', trim($lang_pref));
137 $lang = $lang_pref_parts[0];
138 if ($this->hasLang($lang)) {
139 return $lang; // Return full language designation (if available), such as pt-BR.
142 if (strlen($lang) <= 2)
143 continue; // Do not bother determining main language because we already have it.
145 $lang_parts = explode('-', trim($lang));
146 $lang_main = $lang_parts[0];
147 if ($lang_main != $lang && $this->hasLang($lang_main)) {
148 return $lang_main; // Return main language designation, such as pt.
154 // getLangFileList() returns a list of language files.
155 static function getLangFileList() {
157 $d = @opendir(RESOURCE_DIR);
158 while (($file = @readdir($d))) {
159 if (($file != ".") && ($file != "..")) {
160 if (strpos($file, ".lang.php")) {
161 $fileList[] = @basename($file);
169 static function getLangFromFilename($filename)
171 return substr($filename, 0, strpos($filename, '.'));