Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
helper.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Language
5  *
6  * @copyright Copyright (C) 2005 - 2013 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  */
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 boolean $installed Get only installed languages
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 
40  if ($installed)
41  {
42  $db = JFactory::getDbo();
43  $query = $db->getQuery(true)
44  ->select('element')
45  ->from('#__extensions')
46  ->where('type=' . $db->quote('language'))
47  ->where('state=0')
48  ->where('enabled=1')
49  ->where('client_id=' . ($basePath == JPATH_ADMINISTRATOR ? 1 : 0));
50  $db->setQuery($query);
51  $installed_languages = $db->loadObjectList('element');
52  }
53 
54  foreach ($langs as $lang => $metadata)
55  {
56  if (!$installed || array_key_exists($lang, $installed_languages))
57  {
58  $option = array();
59 
60  $option['text'] = $metadata['name'];
61  $option['value'] = $lang;
62 
63  if ($lang == $actualLanguage)
64  {
65  $option['selected'] = 'selected="selected"';
66  }
67 
68  $list[] = $option;
69  }
70  }
71 
72  return $list;
73  }
74 
75  /**
76  * Tries to detect the language.
77  *
78  * @return string locale or null if not found
79  *
80  * @since 11.1
81  */
82  public static function detectLanguage()
83  {
84  if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
85  {
86  $browserLangs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
87  $systemLangs = self::getLanguages();
88 
89  foreach ($browserLangs as $browserLang)
90  {
91  // Slice out the part before ; on first step, the part before - on second, place into array
92  $browserLang = substr($browserLang, 0, strcspn($browserLang, ';'));
93  $primary_browserLang = substr($browserLang, 0, 2);
94 
95  foreach ($systemLangs as $systemLang)
96  {
97  // Take off 3 letters iso code languages as they can't match browsers' languages and default them to en
98  $Jinstall_lang = $systemLang->lang_code;
99 
100  if (strlen($Jinstall_lang) < 6)
101  {
102  if (strtolower($browserLang) == strtolower(substr($systemLang->lang_code, 0, strlen($browserLang))))
103  {
104  return $systemLang->lang_code;
105  }
106  elseif ($primary_browserLang == substr($systemLang->lang_code, 0, 2))
107  {
108  $primaryDetectedLang = $systemLang->lang_code;
109  }
110  }
111  }
112 
113  if (isset($primaryDetectedLang))
114  {
115  return $primaryDetectedLang;
116  }
117  }
118  }
119 
120  return null;
121  }
122 
123  /**
124  * Get available languages
125  *
126  * @param string $key Array key
127  *
128  * @return array An array of published languages
129  *
130  * @since 11.1
131  */
132  public static function getLanguages($key = 'default')
133  {
134  static $languages;
135 
136  if (empty($languages))
137  {
138  // Installation uses available languages
139  if (JFactory::getApplication()->getClientId() == 2)
140  {
141  $languages[$key] = array();
142  $knownLangs = JLanguage::getKnownLanguages(JPATH_BASE);
143 
144  foreach ($knownLangs as $metadata)
145  {
146  // Take off 3 letters iso code languages as they can't match browsers' languages and default them to en
147  $obj = new stdClass;
148  $obj->lang_code = $metadata['tag'];
149  $languages[$key][] = $obj;
150  }
151  }
152  else
153  {
154  $cache = JFactory::getCache('com_languages', '');
155 
156  if (!$languages = $cache->get('languages'))
157  {
158  $db = JFactory::getDbo();
159  $query = $db->getQuery(true)
160  ->select('*')
161  ->from('#__languages')
162  ->where('published=1')
163  ->order('ordering ASC');
164  $db->setQuery($query);
165 
166  $languages['default'] = $db->loadObjectList();
167  $languages['sef'] = array();
168  $languages['lang_code'] = array();
169 
170  if (isset($languages['default'][0]))
171  {
172  foreach ($languages['default'] as $lang)
173  {
174  $languages['sef'][$lang->sef] = $lang;
175  $languages['lang_code'][$lang->lang_code] = $lang;
176  }
177  }
178 
179  $cache->store($languages, 'languages');
180  }
181  }
182  }
183 
184  return $languages[$key];
185  }
186 }