language.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. foreach ($negotiation as $id => $provider) {
  169. if (isset($provider['callbacks']['switcher'])) {
  170. if (isset($provider['file'])) {
  171. require_once DRUPAL_ROOT . '/' . $provider['file'];
  172. }
  173. $callback = $provider['callbacks']['switcher'];
  174. $result = $callback($type, $path);
  175. if (!empty($result)) {
  176. // Allow modules to provide translations for specific links.
  177. drupal_alter('language_switch_links', $result, $type, $path);
  178. $links = (object) array('links' => $result, 'provider' => $id);
  179. break;
  180. }
  181. }
  182. }
  183. return $links;
  184. }
  185. /**
  186. * Updates language configuration to remove any language provider that is no longer defined.
  187. */
  188. function language_negotiation_purge() {
  189. // Ensure that we are getting the defined language negotiation information. An
  190. // invocation of module_enable() or module_disable() could outdate the cached
  191. // information.
  192. drupal_static_reset('language_negotiation_info');
  193. drupal_static_reset('language_types_info');
  194. $defined_providers = language_negotiation_info();
  195. foreach (language_types_info() as $type => $type_info) {
  196. $weight = 0;
  197. $negotiation = array();
  198. foreach (variable_get("language_negotiation_$type", array()) as $id => $provider) {
  199. if (isset($defined_providers[$id])) {
  200. $negotiation[$id] = $weight++;
  201. }
  202. }
  203. language_negotiation_set($type, $negotiation);
  204. }
  205. }
  206. /**
  207. * Save a list of language providers.
  208. *
  209. * @param $type
  210. * The language negotiation type.
  211. * @param $language_providers
  212. * An array of language provider weights keyed by id.
  213. * @see language_provider_weight()
  214. */
  215. function language_negotiation_set($type, $language_providers) {
  216. // Save only the necessary fields.
  217. $provider_fields = array('callbacks', 'file', 'cache');
  218. $negotiation = array();
  219. $providers_weight = array();
  220. $defined_providers = language_negotiation_info();
  221. $default_types = language_types_configurable(FALSE);
  222. // Initialize the providers weight list.
  223. foreach ($language_providers as $id => $provider) {
  224. $providers_weight[$id] = language_provider_weight($provider);
  225. }
  226. // Order providers list by weight.
  227. asort($providers_weight);
  228. foreach ($providers_weight as $id => $weight) {
  229. if (isset($defined_providers[$id])) {
  230. $provider = $defined_providers[$id];
  231. // If the provider does not express any preference about types, make it
  232. // available for any configurable type.
  233. $types = array_flip(isset($provider['types']) ? $provider['types'] : $default_types);
  234. // Check if the provider is defined and has the right type.
  235. if (isset($types[$type])) {
  236. $provider_data = array();
  237. foreach ($provider_fields as $field) {
  238. if (isset($provider[$field])) {
  239. $provider_data[$field] = $provider[$field];
  240. }
  241. }
  242. $negotiation[$id] = $provider_data;
  243. }
  244. }
  245. }
  246. variable_set("language_negotiation_$type", $negotiation);
  247. }
  248. /**
  249. * Return all the defined language providers.
  250. *
  251. * @return
  252. * An array of language providers.
  253. */
  254. function language_negotiation_info() {
  255. $language_providers = &drupal_static(__FUNCTION__);
  256. if (!isset($language_providers)) {
  257. // Collect all the module-defined language negotiation providers.
  258. $language_providers = module_invoke_all('language_negotiation_info');
  259. // Add the default language provider.
  260. $language_providers[LANGUAGE_NEGOTIATION_DEFAULT] = array(
  261. 'callbacks' => array('language' => 'language_from_default'),
  262. 'weight' => 10,
  263. 'name' => t('Default'),
  264. 'description' => t('Use the default site language (@language_name).', array('@language_name' => language_default()->native)),
  265. );
  266. // Let other modules alter the list of language providers.
  267. drupal_alter('language_negotiation_info', $language_providers);
  268. }
  269. return $language_providers;
  270. }
  271. /**
  272. * Helper function used to cache the language providers results.
  273. *
  274. * @param $provider_id
  275. * The language provider ID.
  276. * @param $provider
  277. * The language provider to be invoked. If not passed it will be explicitly
  278. * loaded through language_negotiation_info().
  279. *
  280. * @return
  281. * The language provider's return value.
  282. */
  283. function language_provider_invoke($provider_id, $provider = NULL) {
  284. $results = &drupal_static(__FUNCTION__);
  285. if (!isset($results[$provider_id])) {
  286. global $user;
  287. // Get languages grouped by status and select only the enabled ones.
  288. $languages = language_list('enabled');
  289. $languages = $languages[1];
  290. if (!isset($provider)) {
  291. $providers = language_negotiation_info();
  292. $provider = $providers[$provider_id];
  293. }
  294. if (isset($provider['file'])) {
  295. require_once DRUPAL_ROOT . '/' . $provider['file'];
  296. }
  297. // If the language provider has no cache preference or this is satisfied
  298. // we can execute the callback.
  299. $cache = !isset($provider['cache']) || $user->uid || $provider['cache'] == variable_get('cache', 0);
  300. $callback = isset($provider['callbacks']['language']) ? $provider['callbacks']['language'] : FALSE;
  301. $langcode = $cache && function_exists($callback) ? $callback($languages) : FALSE;
  302. $results[$provider_id] = isset($languages[$langcode]) ? $languages[$langcode] : FALSE;
  303. }
  304. // Since objects are resources we need to return a clone to prevent the
  305. // provider cache to be unintentionally altered. The same providers might be
  306. // used with different language types based on configuration.
  307. return !empty($results[$provider_id]) ? clone($results[$provider_id]) : $results[$provider_id];
  308. }
  309. /**
  310. * Return the passed language provider weight or a default value.
  311. *
  312. * @param $provider
  313. * A language provider data structure.
  314. *
  315. * @return
  316. * A numeric weight.
  317. */
  318. function language_provider_weight($provider) {
  319. $default = is_numeric($provider) ? $provider : 0;
  320. return isset($provider['weight']) && is_numeric($provider['weight']) ? $provider['weight'] : $default;
  321. }
  322. /**
  323. * Choose a language for the given type based on language negotiation settings.
  324. *
  325. * @param $type
  326. * The language type.
  327. *
  328. * @return
  329. * The negotiated language object.
  330. */
  331. function language_initialize($type) {
  332. // Execute the language providers in the order they were set up and return the
  333. // first valid language found.
  334. $negotiation = variable_get("language_negotiation_$type", array());
  335. foreach ($negotiation as $provider_id => $provider) {
  336. $language = language_provider_invoke($provider_id, $provider);
  337. if ($language) {
  338. $language->provider = $provider_id;
  339. return $language;
  340. }
  341. }
  342. // If no other language was found use the default one.
  343. $language = language_default();
  344. $language->provider = LANGUAGE_NEGOTIATION_DEFAULT;
  345. return $language;
  346. }
  347. /**
  348. * Default language provider.
  349. *
  350. * @return
  351. * The default language code.
  352. */
  353. function language_from_default() {
  354. return language_default()->language;
  355. }
  356. /**
  357. * Split the given path into prefix and actual path.
  358. *
  359. * Parse the given path and return the language object identified by the
  360. * prefix and the actual path.
  361. *
  362. * @param $path
  363. * The path to split.
  364. * @param $languages
  365. * An array of valid languages.
  366. *
  367. * @return
  368. * An array composed of:
  369. * - A language object corresponding to the identified prefix on success,
  370. * FALSE otherwise.
  371. * - The path without the prefix on success, the given path otherwise.
  372. */
  373. function language_url_split_prefix($path, $languages) {
  374. $args = empty($path) ? array() : explode('/', $path);
  375. $prefix = array_shift($args);
  376. // Search prefix within enabled languages.
  377. foreach ($languages as $language) {
  378. if (!empty($language->prefix) && $language->prefix == $prefix) {
  379. // Rebuild $path with the language removed.
  380. return array($language, implode('/', $args));
  381. }
  382. }
  383. return array(FALSE, $path);
  384. }
  385. /**
  386. * Return the possible fallback languages ordered by language weight.
  387. *
  388. * @param
  389. * The language type.
  390. *
  391. * @return
  392. * An array of language codes.
  393. */
  394. function language_fallback_get_candidates($type = LANGUAGE_TYPE_CONTENT) {
  395. $fallback_candidates = &drupal_static(__FUNCTION__);
  396. if (!isset($fallback_candidates)) {
  397. $fallback_candidates = array();
  398. // Get languages ordered by weight.
  399. // Use array keys to avoid duplicated entries.
  400. foreach (language_list('weight') as $languages) {
  401. foreach ($languages as $language) {
  402. $fallback_candidates[$language->language] = NULL;
  403. }
  404. }
  405. $fallback_candidates = array_keys($fallback_candidates);
  406. $fallback_candidates[] = LANGUAGE_NONE;
  407. // Let other modules hook in and add/change candidates.
  408. drupal_alter('language_fallback_candidates', $fallback_candidates);
  409. }
  410. return $fallback_candidates;
  411. }