language.api.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the base system for language support.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Allows modules to act after language initialization has been performed.
  12. *
  13. * This is primarily needed to provide translation for configuration variables
  14. * in the proper bootstrap phase. Variables are user-defined strings and
  15. * therefore should not be translated via t(), since the source string can
  16. * change without notice and any previous translation would be lost. Moreover,
  17. * since variables can be used in the bootstrap phase, we need a bootstrap hook
  18. * to provide a translation early enough to avoid misalignments between code
  19. * using the original values and code using the translated values. However
  20. * modules implementing hook_boot() should be aware that language initialization
  21. * did not happen yet and thus they cannot rely on translated variables.
  22. */
  23. function hook_language_init() {
  24. global $language, $conf;
  25. switch ($language->language) {
  26. case 'it':
  27. $conf['site_name'] = 'Il mio sito Drupal';
  28. break;
  29. case 'fr':
  30. $conf['site_name'] = 'Mon site Drupal';
  31. break;
  32. }
  33. }
  34. /**
  35. * Perform alterations on language switcher links.
  36. *
  37. * A language switcher link may need to point to a different path or use a
  38. * translated link text before going through l(), which will just handle the
  39. * path aliases.
  40. *
  41. * @param $links
  42. * Nested array of links keyed by language code.
  43. * @param $type
  44. * The language type the links will switch.
  45. * @param $path
  46. * The current path.
  47. */
  48. function hook_language_switch_links_alter(array &$links, $type, $path) {
  49. global $language;
  50. if ($type == LANGUAGE_TYPE_CONTENT && isset($links[$language->language])) {
  51. foreach ($links[$language->language] as $link) {
  52. $link['attributes']['class'][] = 'active-language';
  53. }
  54. }
  55. }
  56. /**
  57. * Allow modules to define their own language types.
  58. *
  59. * @return
  60. * An array of language type definitions. Each language type has an identifier
  61. * key. The language type definition is an associative array that may contain
  62. * the following key-value pairs:
  63. * - "name": The human-readable language type identifier.
  64. * - "description": A description of the language type.
  65. * - "fixed": An array of language provider identifiers. Defining this key
  66. * makes the language type non-configurable.
  67. */
  68. function hook_language_types_info() {
  69. return array(
  70. 'custom_language_type' => array(
  71. 'name' => t('Custom language'),
  72. 'description' => t('A custom language type.'),
  73. ),
  74. 'fixed_custom_language_type' => array(
  75. 'fixed' => array('custom_language_provider'),
  76. ),
  77. );
  78. }
  79. /**
  80. * Perform alterations on language types.
  81. *
  82. * @param $language_types
  83. * Array of language type definitions.
  84. */
  85. function hook_language_types_info_alter(array &$language_types) {
  86. if (isset($language_types['custom_language_type'])) {
  87. $language_types['custom_language_type_custom']['description'] = t('A far better description.');
  88. }
  89. }
  90. /**
  91. * Allow modules to define their own language providers.
  92. *
  93. * @return
  94. * An array of language provider definitions. Each language provider has an
  95. * identifier key. The language provider definition is an associative array
  96. * that may contain the following key-value pairs:
  97. * - "types": An array of allowed language types. If a language provider does
  98. * not specify which language types it should be used with, it will be
  99. * available for all the configurable language types.
  100. * - "callbacks": An array of functions that will be called to perform various
  101. * tasks. Possible key-value pairs are:
  102. * - "language": Required. The callback that will determine the language
  103. * value.
  104. * - "switcher": The callback that will determine the language switch links
  105. * associated to the current language provider.
  106. * - "url_rewrite": The callback that will provide URL rewriting.
  107. * - "file": A file that will be included before the callback is invoked; this
  108. * allows callback functions to be in separate files.
  109. * - "weight": The default weight the language provider has.
  110. * - "name": A human-readable identifier.
  111. * - "description": A description of the language provider.
  112. * - "config": An internal path pointing to the language provider
  113. * configuration page.
  114. * - "cache": The value Drupal's page cache should be set to for the current
  115. * language provider to be invoked.
  116. */
  117. function hook_language_negotiation_info() {
  118. return array(
  119. 'custom_language_provider' => array(
  120. 'callbacks' => array(
  121. 'language' => 'custom_language_provider_callback',
  122. 'switcher' => 'custom_language_switcher_callback',
  123. 'url_rewrite' => 'custom_language_url_rewrite_callback',
  124. ),
  125. 'file' => drupal_get_path('module', 'custom') . '/custom.module',
  126. 'weight' => -4,
  127. 'types' => array('custom_language_type'),
  128. 'name' => t('Custom language provider'),
  129. 'description' => t('This is a custom language provider.'),
  130. 'cache' => 0,
  131. ),
  132. );
  133. }
  134. /**
  135. * Perform alterations on language providers.
  136. *
  137. * @param $language_providers
  138. * Array of language provider definitions.
  139. */
  140. function hook_language_negotiation_info_alter(array &$language_providers) {
  141. if (isset($language_providers['custom_language_provider'])) {
  142. $language_providers['custom_language_provider']['config'] = 'admin/config/regional/language/configure/custom-language-provider';
  143. }
  144. }
  145. /**
  146. * Perform alterations on the language fallback candidates.
  147. *
  148. * @param $fallback_candidates
  149. * An array of language codes whose order will determine the language fallback
  150. * order.
  151. */
  152. function hook_language_fallback_candidates_alter(array &$fallback_candidates) {
  153. $fallback_candidates = array_reverse($fallback_candidates);
  154. }
  155. /**
  156. * @} End of "addtogroup hooks".
  157. */