| [ Index ] |
PHP Cross Reference of Joomla 2.5.4 DE |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @package Joomla.Platform 4 * @subpackage Language 5 * 6 * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved. 7 * @license GNU General Public License version 2 or later; see LICENSE 8 */ 9 10 defined('JPATH_PLATFORM') or die; 11 12 /** 13 * Language helper class 14 * 15 * @package Joomla.Platform 16 * @subpackage Language 17 * @since 11.1 18 */ 19 class JLanguageHelper 20 { 21 /** 22 * Builds a list of the system languages which can be used in a select option 23 * 24 * @param string $actualLanguage Client key for the area 25 * @param string $basePath Base path to use 26 * @param boolean $caching True if caching is used 27 * @param array $installed An array of arrays (text, value, selected) 28 * 29 * @return array List of system languages 30 * 31 * @since 11.1 32 */ 33 public static function createLanguageList($actualLanguage, $basePath = JPATH_BASE, $caching = false, $installed = false) 34 { 35 $list = array(); 36 37 // Cache activation 38 $langs = JLanguage::getKnownLanguages($basePath); 39 if ($installed) 40 { 41 $db = JFactory::getDBO(); 42 $query = $db->getQuery(true); 43 $query->select('element'); 44 $query->from('#__extensions'); 45 $query->where('type=' . $db->quote('language')); 46 $query->where('state=0'); 47 $query->where('enabled=1'); 48 $query->where('client_id=' . ($basePath == JPATH_ADMINISTRATOR ? 1 : 0)); 49 $db->setQuery($query); 50 $installed_languages = $db->loadObjectList('element'); 51 } 52 53 foreach ($langs as $lang => $metadata) 54 { 55 if (!$installed || array_key_exists($lang, $installed_languages)) 56 { 57 $option = array(); 58 59 $option['text'] = $metadata['name']; 60 $option['value'] = $lang; 61 if ($lang == $actualLanguage) 62 { 63 $option['selected'] = 'selected="selected"'; 64 } 65 $list[] = $option; 66 } 67 } 68 69 return $list; 70 } 71 72 /** 73 * Tries to detect the language. 74 * 75 * @return string locale or null if not found 76 * 77 * @since 11.1 78 */ 79 public static function detectLanguage() 80 { 81 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) 82 { 83 $browserLangs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']); 84 $systemLangs = self::getLanguages(); 85 foreach ($browserLangs as $browserLang) 86 { 87 // Slice out the part before ; on first step, the part before - on second, place into array 88 $browserLang = substr($browserLang, 0, strcspn($browserLang, ';')); 89 $primary_browserLang = substr($browserLang, 0, 2); 90 foreach ($systemLangs as $systemLang) 91 { 92 // Take off 3 letters iso code languages as they can't match browsers' languages and default them to en 93 $Jinstall_lang = $systemLang->lang_code; 94 95 if (strlen($Jinstall_lang) < 6) 96 { 97 if (strtolower($browserLang) == strtolower(substr($systemLang->lang_code, 0, strlen($browserLang)))) 98 { 99 return $systemLang->lang_code; 100 } 101 elseif ($primary_browserLang == substr($systemLang->lang_code, 0, 2)) 102 { 103 $primaryDetectedLang = $systemLang->lang_code; 104 } 105 } 106 } 107 108 if (isset($primaryDetectedLang)) 109 { 110 return $primaryDetectedLang; 111 } 112 } 113 } 114 115 return null; 116 } 117 118 /** 119 * Get available languages 120 * 121 * @param string $key Array key 122 * 123 * @return array An array of published languages 124 * 125 * @since 11.1 126 */ 127 public static function getLanguages($key = 'default') 128 { 129 static $languages; 130 131 if (empty($languages)) 132 { 133 // Installation uses available languages 134 if (JFactory::getApplication()->getClientId() == 2) 135 { 136 $languages[$key] = array(); 137 $knownLangs = JLanguage::getKnownLanguages(JPATH_BASE); 138 foreach ($knownLangs as $metadata) 139 { 140 // take off 3 letters iso code languages as they can't match browsers' languages and default them to en 141 $languages[$key][] = new JObject(array('lang_code' => $metadata['tag'])); 142 } 143 } 144 else 145 { 146 $cache = JFactory::getCache('com_languages', ''); 147 if (!$languages = $cache->get('languages')) 148 { 149 $db = JFactory::getDBO(); 150 $query = $db->getQuery(true); 151 $query->select('*') 152 ->from('#__languages') 153 ->where('published=1') 154 ->order('ordering ASC'); 155 $db->setQuery($query); 156 157 $languages['default'] = $db->loadObjectList(); 158 $languages['sef'] = array(); 159 $languages['lang_code'] = array(); 160 161 if (isset($languages['default'][0])) 162 { 163 foreach ($languages['default'] as $lang) 164 { 165 $languages['sef'][$lang->sef] = $lang; 166 $languages['lang_code'][$lang->lang_code] = $lang; 167 } 168 } 169 170 $cache->store($languages, 'languages'); 171 } 172 } 173 } 174 return $languages[$key]; 175 } 176 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Tue Apr 3 11:40:28 2012 | Cross-referenced by PHPXref 0.7.1 |