2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 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 /************************************************************************/
15 * Class for accessing information about a single language.
17 * @author Joel Kronenberg
18 * @see LanguageManager::getLanguage()
19 * @see LanguageManager::getMyLanguage()
22 include_once(TR_INCLUDE_PATH.'classes/DAO/LangCodesDAO.class.php');
29 var $regularExpression;
33 var $transformable_version;
36 function Language($language_row) {
38 if (is_array($language_row)) {
39 $this->code = $language_row['language_code'];
40 $this->characterSet = $language_row['charset'];
41 $this->regularExpression = $language_row['reg_exp'];
42 $this->nativeName = $language_row['native_name'];
43 $this->englishName = $language_row['english_name'];
44 $this->status = $language_row['status'];
45 $this->transformable_version = isset($language_row['version']) ? $language_row['version'] : VERSION;
47 $langCodesDAO = new LangCodesDAO();
48 $row_langCodes = $langCodesDAO->GetLangCodeBy3LetterCode($this->getParentCode($language_row['language_code']));
50 $this->direction = $row_langCodes['direction'];
52 } else if (is_object($language_row)) {
53 $this->cloneThis($language_row);
58 // copies the properties from $from to $this Object
59 function cloneThis($from) {
60 $vars = get_object_vars($from);
61 foreach ($vars as $key => $value) {
66 // returns whether or not the $search_string matches the regular expression
67 function isMatchHttpAcceptLanguage($search_string) {
68 return preg_match('/^(' . $this->regularExpression . ')(;q=[0-9]\\.[0-9])?$/i', $search_string);
71 // returns boolean whether or not $search_string is in HTTP_USER_AGENT
72 function isMatchHttpUserAgent($search_string) {
73 return preg_match('/(\(|\[|;[\s])(' . $this->regularExpression . ')(;|\]|\))/', $search_string);
81 function getCharacterSet() {
82 return $this->characterSet;
85 function getDirection() {
86 return $this->direction;
89 function getRegularExpression() {
90 return $this->regularExpression;
93 function getTransformableVersion() {
94 return $this->transformable_version;
97 function getTranslatedName() {
98 if ($this->code == $_SESSION['lang']) {
99 return $this->nativeName;
101 // this code has to be translated:
102 return _AT('lang_' . str_replace('-', '_', $this->code));
105 function getNativeName() {
106 return $this->nativeName;
109 function getEnglishName() {
110 return $this->englishName;
113 function getStatus() {
114 return $this->status;
119 function sendContentTypeHeader() {
120 header('Content-Type: text/html; charset=' . $this->characterSet);
124 function saveToSession() {
125 $_SESSION['lang'] = $this->code;
130 * @param member_id or login for members and admin respectively
131 * @param 1 for admin, 0 for members, all other integers are ignored.
133 function saveToPreferences($id, $is_admin) {
136 if ($is_admin === 0) {
137 $sql = "UPDATE ".TABLE_PREFIX."members SET language='".$this->code."', creation_date=creation_date, last_login=last_login WHERE member_id=$id";
138 } elseif ($is_admin === 1) {
139 $sql = "UPDATE ".TABLE_PREFIX."admins SET language='".$this->code."', last_login=last_login WHERE login='$id'";
141 mysql_query($sql,$db);
146 // returns whether or not this language is right-to-left
147 // possible langues are: arabic, farsi, hebrew, urdo
149 if ($this->direction == 'rtl') {
156 function getParentCode($code = '') {
157 if (!$code && isset($this)) {
160 $peices = explode(TR_LANGUAGE_LOCALE_SEP, $code, 2);
165 // can be called staticly
166 function getLocale($code = '') {
167 if (!$code && isset($this)) {
170 $peices = explode(TR_LANGUAGE_LOCALE_SEP, $code, 2);
174 function getXML($part=FALSE) {
176 $xml = '<?xml version="1.0" encoding="iso-8859-1"?>
177 <!-- This is an AContent language pack -->
180 <!ELEMENT transformable-version (#PCDATA)>
181 <!ELEMENT charset (#PCDATA)>
182 <!ELEMENT reg-exp (#PCDATA)>
183 <!ELEMENT native-name (#PCDATA)>
184 <!ELEMENT english-name (#PCDATA)>
185 <!ELEMENT status (#PCDATA)>
187 <!ATTLIST language code ID #REQUIRED>
191 $xml .= '<language code="'.$this->code.'">
192 <transformable-version>'.VERSION.'</transformable-version>
193 <charset>'.$this->characterSet.'</charset>
194 <reg-exp>'.$this->regularExpression.'</reg-exp>
195 <native-name>'.$this->nativeName.'</native-name>
196 <english-name>'.$this->englishName.'</english-name>
197 <status>'.$this->status.'</status>