language.api.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. * Define language types.
  58. *
  59. * @return
  60. * An associative array of language type definitions. The keys are the
  61. * identifiers, which are also used as names for global variables representing
  62. * the types in the bootstrap phase. The values are associative arrays that
  63. * may contain the following elements:
  64. * - name: The human-readable language type identifier.
  65. * - description: A description of the language type.
  66. * - fixed: A fixed array of language negotiation provider identifiers to use
  67. * to initialize this language. Defining this key makes the language type
  68. * non-configurable, so it will always use the specified providers in the
  69. * given priority order. Omit to make the language type configurable.
  70. *
  71. * @see hook_language_types_info_alter()
  72. * @ingroup language_negotiation
  73. */
  74. function hook_language_types_info() {
  75. return array(
  76. 'custom_language_type' => array(
  77. 'name' => t('Custom language'),
  78. 'description' => t('A custom language type.'),
  79. ),
  80. 'fixed_custom_language_type' => array(
  81. 'fixed' => array('custom_language_provider'),
  82. ),
  83. );
  84. }
  85. /**
  86. * Perform alterations on language types.
  87. *
  88. * @param $language_types
  89. * Array of language type definitions.
  90. *
  91. * @see hook_language_types_info()
  92. * @ingroup language_negotiation
  93. */
  94. function hook_language_types_info_alter(array &$language_types) {
  95. if (isset($language_types['custom_language_type'])) {
  96. $language_types['custom_language_type_custom']['description'] = t('A far better description.');
  97. }
  98. }
  99. /**
  100. * Define language negotiation providers.
  101. *
  102. * @return
  103. * An associative array of language negotiation provider definitions. The keys
  104. * are provider identifiers, and the values are associative arrays defining
  105. * each provider, with the following elements:
  106. * - types: An array of allowed language types. If a language negotiation
  107. * provider does not specify which language types it should be used with, it
  108. * will be available for all the configurable language types.
  109. * - callbacks: An associative array of functions that will be called to
  110. * perform various tasks. Possible elements are:
  111. * - language: (required) Name of the callback function that determines the
  112. * language value.
  113. * - switcher: (optional) Name of the callback function that determines
  114. * links for a language switcher block associated with this provider. See
  115. * language_switcher_url() for an example.
  116. * - url_rewrite: (optional) Name of the callback function that provides URL
  117. * rewriting, if needed by this provider.
  118. * - file: The file where callback functions are defined (this file will be
  119. * included before the callbacks are invoked).
  120. * - weight: The default weight of the provider.
  121. * - name: The translated human-readable name for the provider.
  122. * - description: A translated longer description of the provider.
  123. * - config: An internal path pointing to the provider's configuration page.
  124. * - cache: The value Drupal's page cache should be set to for the current
  125. * provider to be invoked.
  126. *
  127. * @see hook_language_negotiation_info_alter()
  128. * @ingroup language_negotiation
  129. */
  130. function hook_language_negotiation_info() {
  131. return array(
  132. 'custom_language_provider' => array(
  133. 'callbacks' => array(
  134. 'language' => 'custom_language_provider_callback',
  135. 'switcher' => 'custom_language_switcher_callback',
  136. 'url_rewrite' => 'custom_language_url_rewrite_callback',
  137. ),
  138. 'file' => drupal_get_path('module', 'custom') . '/custom.module',
  139. 'weight' => -4,
  140. 'types' => array('custom_language_type'),
  141. 'name' => t('Custom language negotiation provider'),
  142. 'description' => t('This is a custom language negotiation provider.'),
  143. 'cache' => 0,
  144. ),
  145. );
  146. }
  147. /**
  148. * Perform alterations on language negoiation providers.
  149. *
  150. * @param $language_providers
  151. * Array of language negotiation provider definitions.
  152. *
  153. * @see hook_language_negotiation_info()
  154. * @ingroup language_negotiation
  155. */
  156. function hook_language_negotiation_info_alter(array &$language_providers) {
  157. if (isset($language_providers['custom_language_provider'])) {
  158. $language_providers['custom_language_provider']['config'] = 'admin/config/regional/language/configure/custom-language-provider';
  159. }
  160. }
  161. /**
  162. * Perform alterations on the language fallback candidates.
  163. *
  164. * @param $fallback_candidates
  165. * An array of language codes whose order will determine the language fallback
  166. * order.
  167. */
  168. function hook_language_fallback_candidates_alter(array &$fallback_candidates) {
  169. $fallback_candidates = array_reverse($fallback_candidates);
  170. }
  171. /**
  172. * @} End of "addtogroup hooks".
  173. */