2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2004 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto */
9 /* This program is free software. You can redistribute it and/or */
10 /* modify it under the terms of the GNU General Public License */
11 /* as published by the Free Software Foundation. */
12 /************************************************************************/
15 define('AT_LANGUAGE_LOCALE_SEP', '-');
19 * Class for accessing information about a single language.
21 * @author Joel Kronenberg
22 * @see LanguageManager::getLanguage()
23 * @see LanguageManager::getMyLanguage()
31 var $regularExpression;
40 function Language($language_row) {
45 if (is_array($language_row)) {
46 $this->code = $language_row['language_code'];
47 $this->characterSet = $language_row['char_set'];
48 $this->direction = $language_row['direction'];
49 $this->regularExpression = $language_row['reg_exp'];
50 $this->nativeName = $language_row['native_name'];
51 $this->englishName = $language_row['english_name'];
52 $this->status = $language_row['status'];
53 $this->atutor_version = $language_row['version'];
55 } else if (is_object($language_row)) {
56 $this->cloneThis($language_row);
61 // copies the properties from $from to $this Object
62 function cloneThis($from) {
63 $vars = get_object_vars($from);
64 foreach ($vars as $key => $value) {
69 // returns whether or not the $search_string matches the regular expression
70 function isMatchHttpAcceptLanguage($search_string) {
71 return eregi('^(' . $this->regularExpression . ')(;q=[0-9]\\.[0-9])?$', $search_string);
74 // returns boolean whether or not $search_string is in HTTP_USER_AGENT
75 function isMatchHttpUserAgent($search_string) {
76 return eregi('(\(|\[|;[[:space:]])(' . $this->regularExpression . ')(;|\]|\))', $search_string);
84 function getCharacterSet() {
85 return $this->characterSet;
88 function getDirection() {
89 return $this->direction;
92 function getRegularExpression() {
93 return $this->regularExpression;
96 function getAtutorVersion() {
97 return $this->atutor_version;
100 function getTranslatedName() {
101 if ($this->code == $_SESSION['lang']) {
102 return $this->nativeName;
104 // this code has to be translated:
105 return _AT('lang_' . str_replace('-', '_', $this->code));
108 function getNativeName() {
109 return $this->nativeName;
112 function getEnglishName() {
113 return $this->englishName;
116 function getStatus() {
117 return $this->status;
122 function sendContentTypeHeader() {
123 header('Content-Type: text/html; charset=' . $this->characterSet);
127 function saveToSession() {
128 $_SESSION['lang'] = $this->code;
132 function saveToPreferences($id) {
135 $sql = "UPDATE ".TABLE_PREFIX."members SET language='".$this->code."', creation_date=creation_date, last_login=last_login WHERE member_id=$id";
136 mysql_query($sql,$db);
141 // returns whether or not this language is right-to-left
142 // possible langues are: arabic, farsi, hebrew, urdo
144 if ($this->direction == 'rtl') {
152 // can be called staticly
153 function getParentCode($code = '') {
154 if (!$code && isset($this)) {
157 $peices = explode(AT_LANGUAGE_LOCALE_SEP, $code, 2);
162 // can be called staticly
163 function getLocale($code = '') {
164 if (!$code && isset($this)) {
167 $peices = explode(AT_LANGUAGE_LOCALE_SEP, $code, 2);
173 function getTerm($term) {
174 $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'";
176 $result = mysql_query($sql, $this->db);
177 $row = mysql_fetch_assoc($result);
181 function getXML($part=FALSE) {
183 $xml = '<?xml version="1.0" encoding="iso-8859-1"?>
184 <!-- This is an ATutor language pack - http://www.atutor.ca-->
187 <!ELEMENT atutor-version (#PCDATA)>
188 <!ELEMENT code (#PCDATA)>
189 <!ELEMENT charset (#PCDATA)>
190 <!ELEMENT direction (#PCDATA)>
191 <!ELEMENT reg-exp (#PCDATA)>
192 <!ELEMENT native-name (#PCDATA)>
193 <!ELEMENT english-name (#PCDATA)>
194 <!ELEMENT status (#PCDATA)>
196 <!ATTLIST language code ID #REQUIRED>
200 $xml .= '<language code="'.$this->code.'">
201 <atutor-version>'.VERSION.'</atutor-version>
202 <charset>'.$this->characterSet.'</charset>
203 <direction>'.$this->direction.'</direction>
204 <reg-exp>'.$this->regularExpression.'</reg-exp>
205 <native-name>'.$this->nativeName.'</native-name>
206 <english-name>'.$this->englishName.'</english-name>
207 <status>'.$this->status.'</status>