language.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. <?php
  2. /**
  3. * @file
  4. * Multiple language handling functionality.
  5. */
  6. /**
  7. * No language negotiation. The default language is used.
  8. */
  9. define('LANGUAGE_NEGOTIATION_DEFAULT', 'language-default');
  10. /**
  11. * Return all the defined language types.
  12. *
  13. * @return
  14. * An array of language type names. The name will be used as the global
  15. * variable name the language value will be stored in.
  16. */
  17. function language_types_info() {
  18. $language_types = &drupal_static(__FUNCTION__);
  19. if (!isset($language_types)) {
  20. $language_types = module_invoke_all('language_types_info');
  21. // Let other modules alter the list of language types.
  22. drupal_alter('language_types_info', $language_types);
  23. }
  24. return $language_types;
  25. }
  26. /**
  27. * Return only the configurable language types.
  28. *
  29. * A language type maybe configurable or fixed. A fixed language type is a type
  30. * whose negotiation values are unchangeable and defined while defining the
  31. * language type itself.
  32. *
  33. * @param $stored
  34. * Optional. By default retrieves values from the 'language_types' variable to
  35. * avoid unnecessary hook invocations.
  36. * If set to FALSE retrieves values from the actual language type definitions.
  37. * This allows to react to alterations performed on the definitions by modules
  38. * installed after the 'language_types' variable is set.
  39. *
  40. * @return
  41. * An array of language type names.
  42. */
  43. function language_types_configurable($stored = TRUE) {
  44. $configurable = &drupal_static(__FUNCTION__);
  45. if ($stored && !isset($configurable)) {
  46. $types = variable_get('language_types', drupal_language_types());
  47. $configurable = array_keys(array_filter($types));
  48. }
  49. if (!$stored) {
  50. $result = array();
  51. foreach (language_types_info() as $type => $info) {
  52. if (!isset($info['fixed'])) {
  53. $result[] = $type;
  54. }
  55. }
  56. return $result;
  57. }
  58. return $configurable;
  59. }
  60. /**
  61. * Disable the given language types.
  62. *
  63. * @param $types
  64. * An array of language types.
  65. */
  66. function language_types_disable($types) {
  67. $enabled_types = variable_get('language_types', drupal_language_types());
  68. foreach ($types as $type) {
  69. unset($enabled_types[$type]);
  70. }
  71. variable_set('language_types', $enabled_types);
  72. }
  73. /**
  74. * Updates the language type configuration.
  75. */
  76. function language_types_set() {
  77. // Ensure that we are getting the defined language negotiation information. An
  78. // invocation of module_enable() or module_disable() could outdate the cached
  79. // information.
  80. drupal_static_reset('language_types_info');
  81. drupal_static_reset('language_negotiation_info');
  82. // Determine which language types are configurable and which not by checking
  83. // whether the 'fixed' key is defined. Non-configurable (fixed) language types
  84. // have their language negotiation settings stored there.
  85. $defined_providers = language_negotiation_info();
  86. foreach (language_types_info() as $type => $info) {
  87. if (isset($info['fixed'])) {
  88. $language_types[$type] = FALSE;
  89. $negotiation = array();
  90. foreach ($info['fixed'] as $weight => $id) {
  91. if (isset($defined_providers[$id])) {
  92. $negotiation[$id] = $weight;
  93. }
  94. }
  95. language_negotiation_set($type, $negotiation);
  96. }
  97. else {
  98. $language_types[$type] = TRUE;
  99. }
  100. }
  101. // Save language types.
  102. variable_set('language_types', $language_types);
  103. // Ensure that subsequent calls of language_types_configurable() return the
  104. // updated language type information.
  105. drupal_static_reset('language_types_configurable');
  106. }
  107. /**
  108. * Check if a language provider is enabled.
  109. *
  110. * This has two possible behaviors:
  111. * - If $provider_id is given return its ID if enabled, FALSE otherwise.
  112. * - If no ID is passed the first enabled language provider is returned.
  113. *
  114. * @param $type
  115. * The language negotiation type.
  116. * @param $provider_id
  117. * The language provider ID.
  118. *
  119. * @return
  120. * The provider ID if it is enabled, FALSE otherwise.
  121. */
  122. function language_negotiation_get($type, $provider_id = NULL) {
  123. $negotiation = variable_get("language_negotiation_$type", array());
  124. if (empty($negotiation)) {
  125. return empty($provider_id) ? LANGUAGE_NEGOTIATION_DEFAULT : FALSE;
  126. }
  127. if (empty($provider_id)) {
  128. return key($negotiation);
  129. }
  130. if (isset($negotiation[$provider_id])) {
  131. return $provider_id;
  132. }
  133. return FALSE;
  134. }
  135. /**
  136. * Check if the given language provider is enabled for any configurable language
  137. * type.
  138. *
  139. * @param $provider_id
  140. * The language provider ID.
  141. *
  142. * @return
  143. * TRUE if there is at least one language type for which the give language
  144. * provider is enabled, FALSE otherwise.
  145. */
  146. function language_negotiation_get_any($provider_id) {
  147. foreach (language_types_configurable() as $type) {
  148. if (language_negotiation_get($type, $provider_id)) {
  149. return TRUE;
  150. }
  151. }
  152. return FALSE;
  153. }
  154. /**
  155. * Return the language switch links for the given language.
  156. *
  157. * @param $type
  158. * The language negotiation type.
  159. * @param $path
  160. * The internal path the switch links will be relative to.
  161. *
  162. * @return
  163. * A keyed array of links ready to be themed.
  164. */
  165. function language_negotiation_get_switch_links($type, $path) {
  166. $links = FALSE;
  167. $negotiation = variable_get("language_negotiation_$type", array());
  168. // Only get the languages if we have more than one.
  169. if (count(language_list()) >= 2) {
  170. $language = language_initialize($type);
  171. }
  172. foreach ($negotiation as $id => $provider) {
  173. if (isset($provider['callbacks']['switcher'])) {
  174. if (isset($provider['file'])) {
  175. require_once DRUPAL_ROOT . '/' . $provider['file'];
  176. }
  177. $callback = $provider['callbacks']['switcher'];
  178. $result = $callback($type, $path);
  179. // Add support for WCAG 2.0's Language of Parts to add language identifiers.
  180. // http://www.w3.org/TR/UNDERSTANDING-WCAG20/meaning-other-lang-id.html
  181. foreach ($result as $langcode => $link) {
  182. $result[$langcode]['attributes']['lang'] = $langcode;
  183. }
  184. if (!empty($result)) {
  185. // Allow modules to provide translations for specific links.
  186. drupal_alter('language_switch_links', $result, $type, $path);
  187. $links = (object) array('links' => $result, 'provider' => $id);
  188. break;
  189. }
  190. }
  191. }
  192. return $links;
  193. }
  194. /**
  195. * Updates language configuration to remove any language provider that is no longer defined.
  196. */
  197. function language_negotiation_purge() {
  198. // Ensure that we are getting the defined language negotiation information. An
  199. // invocation of module_enable() or module_disable() could outdate the cached
  200. // information.
  201. drupal_static_reset('language_negotiation_info');
  202. drupal_static_reset('language_types_info');
  203. $defined_providers = language_negotiation_info();
  204. foreach (language_types_info() as $type => $type_info) {
  205. $weight = 0;
  206. $negotiation = array();
  207. foreach (variable_get("language_negotiation_$type", array()) as $id => $provider) {
  208. if (isset($defined_providers[$id])) {
  209. $negotiation[$id] = $weight++;
  210. }
  211. }
  212. language_negotiation_set($type, $negotiation);
  213. }
  214. }
  215. /**
  216. * Save a list of language providers.
  217. *
  218. * @param $type
  219. * The language negotiation type.
  220. * @param $language_providers
  221. * An array of language provider weights keyed by id.
  222. * @see language_provider_weight()
  223. */
  224. function language_negotiation_set($type, $language_providers) {
  225. // Save only the necessary fields.
  226. $provider_fields = array('callbacks', 'file', 'cache');
  227. $negotiation = array();
  228. $providers_weight = array();
  229. $defined_providers = language_negotiation_info();
  230. $default_types = language_types_configurable(FALSE);
  231. // Initialize the providers weight list.
  232. foreach ($language_providers as $id => $provider) {
  233. $providers_weight[$id] = language_provider_weight($provider);
  234. }
  235. // Order providers list by weight.
  236. asort($providers_weight);
  237. foreach ($providers_weight as $id => $weight) {
  238. if (isset($defined_providers[$id])) {
  239. $provider = $defined_providers[$id];
  240. // If the provider does not express any preference about types, make it
  241. // available for any configurable type.
  242. $types = array_flip(isset($provider['types']) ? $provider['types'] : $default_types);
  243. // Check if the provider is defined and has the right type.
  244. if (isset($types[$type])) {
  245. $provider_data = array();
  246. foreach ($provider_fields as $field) {
  247. if (isset($provider[$field])) {
  248. $provider_data[$field] = $provider[$field];
  249. }
  250. }
  251. $negotiation[$id] = $provider_data;
  252. }
  253. }
  254. }
  255. variable_set("language_negotiation_$type", $negotiation);
  256. }
  257. /**
  258. * Return all the defined language providers.
  259. *
  260. * @return
  261. * An array of language providers.
  262. */
  263. function language_negotiation_info() {
  264. $language_providers = &drupal_static(__FUNCTION__);
  265. if (!isset($language_providers)) {
  266. // Collect all the module-defined language negotiation providers.
  267. $language_providers = module_invoke_all('language_negotiation_info');
  268. // Add the default language provider.
  269. $language_providers[LANGUAGE_NEGOTIATION_DEFAULT] = array(
  270. 'callbacks' => array('language' => 'language_from_default'),
  271. 'weight' => 10,
  272. 'name' => t('Default'),
  273. 'description' => t('Use the default site language (@language_name).', array('@language_name' => language_default()->native)),
  274. );
  275. // Let other modules alter the list of language providers.
  276. drupal_alter('language_negotiation_info', $language_providers);
  277. }
  278. return $language_providers;
  279. }
  280. /**
  281. * Helper function used to cache the language providers results.
  282. *
  283. * @param $provider_id
  284. * The language provider ID.
  285. * @param $provider
  286. * The language provider to be invoked. If not passed it will be explicitly
  287. * loaded through language_negotiation_info().
  288. *
  289. * @return
  290. * The language provider's return value.
  291. */
  292. function language_provider_invoke($provider_id, $provider = NULL) {
  293. $results = &drupal_static(__FUNCTION__);
  294. if (!isset($results[$provider_id])) {
  295. global $user;
  296. // Get languages grouped by status and select only the enabled ones.
  297. $languages = language_list('enabled');
  298. $languages = $languages[1];
  299. if (!isset($provider)) {
  300. $providers = language_negotiation_info();
  301. $provider = $providers[$provider_id];
  302. }
  303. if (isset($provider['file'])) {
  304. require_once DRUPAL_ROOT . '/' . $provider['file'];
  305. }
  306. // If the language provider has no cache preference or this is satisfied
  307. // we can execute the callback.
  308. $cache = !isset($provider['cache']) || $user->uid || $provider['cache'] == variable_get('cache', 0);
  309. $callback = isset($provider['callbacks']['language']) ? $provider['callbacks']['language'] : FALSE;
  310. $langcode = $cache && function_exists($callback) ? $callback($languages) : FALSE;
  311. $results[$provider_id] = isset($languages[$langcode]) ? $languages[$langcode] : FALSE;
  312. }
  313. // Since objects are resources we need to return a clone to prevent the
  314. // provider cache to be unintentionally altered. The same providers might be
  315. // used with different language types based on configuration.
  316. return !empty($results[$provider_id]) ? clone($results[$provider_id]) : $results[$provider_id];
  317. }
  318. /**
  319. * Return the passed language provider weight or a default value.
  320. *
  321. * @param $provider
  322. * A language provider data structure.
  323. *
  324. * @return
  325. * A numeric weight.
  326. */
  327. function language_provider_weight($provider) {
  328. $default = is_numeric($provider) ? $provider : 0;
  329. return isset($provider['weight']) && is_numeric($provider['weight']) ? $provider['weight'] : $default;
  330. }
  331. /**
  332. * Choose a language for the given type based on language negotiation settings.
  333. *
  334. * @param $type
  335. * The language type.
  336. *
  337. * @return
  338. * The negotiated language object.
  339. */
  340. function language_initialize($type) {
  341. // Execute the language providers in the order they were set up and return the
  342. // first valid language found.
  343. $negotiation = variable_get("language_negotiation_$type", array());
  344. foreach ($negotiation as $provider_id => $provider) {
  345. $language = language_provider_invoke($provider_id, $provider);
  346. if ($language) {
  347. $language->provider = $provider_id;
  348. return $language;
  349. }
  350. }
  351. // If no other language was found use the default one.
  352. $language = language_default();
  353. $language->provider = LANGUAGE_NEGOTIATION_DEFAULT;
  354. return $language;
  355. }
  356. /**
  357. * Default language provider.
  358. *
  359. * @return
  360. * The default language code.
  361. */
  362. function language_from_default() {
  363. return language_default()->language;
  364. }
  365. /**
  366. * Splits the given path into prefix and actual path.
  367. *
  368. * Parse the given path and return the language object identified by the
  369. * prefix and the actual path.
  370. *
  371. * @param $path
  372. * The path to split.
  373. * @param $languages
  374. * An array of valid languages.
  375. *
  376. * @return
  377. * An array composed of:
  378. * - A language object corresponding to the identified prefix on success,
  379. * FALSE otherwise.
  380. * - The path without the prefix on success, the given path otherwise.
  381. */
  382. function language_url_split_prefix($path, $languages) {
  383. $args = empty($path) ? array() : explode('/', $path);
  384. $prefix = array_shift($args);
  385. // Search prefix within enabled languages.
  386. foreach ($languages as $language) {
  387. if (!empty($language->prefix) && $language->prefix == $prefix) {
  388. // Rebuild $path with the language removed.
  389. return array($language, implode('/', $args));
  390. }
  391. }
  392. return array(FALSE, $path);
  393. }
  394. /**
  395. * Returns the possible fallback languages ordered by language weight.
  396. *
  397. * @param
  398. * (optional) The language type. Defaults to LANGUAGE_TYPE_CONTENT.
  399. *
  400. * @return
  401. * An array of language codes.
  402. */
  403. function language_fallback_get_candidates($type = LANGUAGE_TYPE_CONTENT) {
  404. $fallback_candidates = &drupal_static(__FUNCTION__);
  405. if (!isset($fallback_candidates)) {
  406. $fallback_candidates = array();
  407. // Get languages ordered by weight.
  408. // Use array keys to avoid duplicated entries.
  409. foreach (language_list('weight') as $languages) {
  410. foreach ($languages as $language) {
  411. $fallback_candidates[$language->language] = NULL;
  412. }
  413. }
  414. $fallback_candidates = array_keys($fallback_candidates);
  415. $fallback_candidates[] = LANGUAGE_NONE;
  416. // Let other modules hook in and add/change candidates.
  417. drupal_alter('language_fallback_candidates', $fallback_candidates);
  418. }
  419. return $fallback_candidates;
  420. }