2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2010 */
6 /* Inclusive Design Institute */
8 /* This program is free software. You can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License */
10 /* as published by the Free Software Foundation. */
11 /************************************************************************/
14 define('AT_LANGUAGE_LOCALE_SEP', '-');
18 * Class for accessing information about a single language.
20 * @author Joel Kronenberg
21 * @see LanguageManager::getLanguage()
22 * @see LanguageManager::getMyLanguage()
30 var $regularExpression;
39 function Language($language_row) {
44 if (is_array($language_row)) {
45 $this->code = $language_row['language_code'];
46 $this->characterSet = $language_row['char_set'];
47 $this->direction = $language_row['direction'];
48 $this->regularExpression = $language_row['reg_exp'];
49 $this->nativeName = $language_row['native_name'];
50 $this->englishName = $language_row['english_name'];
51 $this->status = $language_row['status'];
52 $this->atutor_version = isset($language_row['version']) ? $language_row['version'] : VERSION;
54 } else if (is_object($language_row)) {
55 $this->cloneThis($language_row);
60 // copies the properties from $from to $this Object
61 function cloneThis($from) {
62 $vars = get_object_vars($from);
63 foreach ($vars as $key => $value) {
68 // returns whether or not the $search_string matches the regular expression
69 function isMatchHttpAcceptLanguage($search_string) {
70 return preg_match('/^(' . $this->regularExpression . ')(;q=[0-9]\\.[0-9])?$/', $search_string);
73 // returns boolean whether or not $search_string is in HTTP_USER_AGENT
74 function isMatchHttpUserAgent($search_string) {
75 return preg_match('/(\(|\[|;[\s])(' . $this->regularExpression . ')(;|\]|\))/', $search_string);
83 function getCharacterSet() {
84 return $this->characterSet;
87 function getDirection() {
88 return $this->direction;
91 function getRegularExpression() {
92 return $this->regularExpression;
95 function getAtutorVersion() {
96 return $this->atutor_version;
99 function getTranslatedName() {
100 if ($this->code == $_SESSION['lang']) {
101 return $this->nativeName;
103 // this code has to be translated:
104 return _AT('lang_' . str_replace('-', '_', $this->code));
107 function getNativeName() {
108 return $this->nativeName;
111 function getEnglishName() {
112 return $this->englishName;
115 function getStatus() {
116 return $this->status;
121 function sendContentTypeHeader() {
122 header('Content-Type: text/html; charset=' . $this->characterSet);
126 function saveToSession() {
127 $_SESSION['lang'] = $this->code;
132 * @param member_id or login for members and admin respectively
133 * @param 1 for admin, 0 for members, all other integers are ignored.
135 function saveToPreferences($id, $is_admin) {
138 if ($is_admin === 0) {
139 $sql = "UPDATE ".TABLE_PREFIX."members SET language='".$this->code."', creation_date=creation_date, last_login=last_login WHERE member_id=$id";
140 } elseif ($is_admin === 1) {
141 $sql = "UPDATE ".TABLE_PREFIX."admins SET language='".$this->code."', last_login=last_login WHERE login='$id'";
143 mysql_query($sql,$db);
148 // returns whether or not this language is right-to-left
149 // possible langues are: arabic, farsi, hebrew, urdo
151 if ($this->direction == 'rtl') {
159 // can be called staticly
160 function getParentCode($code = '') {
161 if (!$code && isset($this)) {
164 $peices = explode(AT_LANGUAGE_LOCALE_SEP, $code, 2);
169 // can be called staticly
170 function getLocale($code = '') {
171 if (!$code && isset($this)) {
174 $peices = explode(AT_LANGUAGE_LOCALE_SEP, $code, 2);
180 function getTerm($term) {
181 $sql = "SELECT *, UNIX_TIMESTAMP(L.revised_date) AS revised_date_unix FROM ".TABLE_PREFIX."language_text L WHERE L.language_code='".$this->getCode()."' AND L.variable='_template' AND L.term='$term'";
183 $result = mysql_query($sql, $this->db);
184 $row = mysql_fetch_assoc($result);
188 function getXML($part=FALSE) {
190 $xml = '<?xml version="1.0" encoding="iso-8859-1"?>
191 <!-- This is an ATutor language pack - http://www.atutor.ca-->
194 <!ELEMENT atutor-version (#PCDATA)>
195 <!ELEMENT code (#PCDATA)>
196 <!ELEMENT charset (#PCDATA)>
197 <!ELEMENT direction (#PCDATA)>
198 <!ELEMENT reg-exp (#PCDATA)>
199 <!ELEMENT native-name (#PCDATA)>
200 <!ELEMENT english-name (#PCDATA)>
201 <!ELEMENT status (#PCDATA)>
203 <!ATTLIST language code ID #REQUIRED>
207 $xml .= '<language code="'.$this->code.'">
208 <atutor-version>'.VERSION.'</atutor-version>
209 <charset>'.$this->characterSet.'</charset>
210 <direction>'.$this->direction.'</direction>
211 <reg-exp>'.$this->regularExpression.'</reg-exp>
212 <native-name>'.$this->nativeName.'</native-name>
213 <english-name>'.$this->englishName.'</english-name>
214 <status>'.$this->status.'</status>