language.api.php 6.19 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Hooks provided by the base system for language support.
6 7 8 9 10 11 12
 */

/**
 * @addtogroup hooks
 * @{
 */

13 14 15 16 17 18 19 20 21 22 23 24 25 26
/**
 * Allows modules to act after language initialization has been performed.
 *
 * This is primarily needed to provide translation for configuration variables
 * in the proper bootstrap phase. Variables are user-defined strings and
 * therefore should not be translated via t(), since the source string can
 * change without notice and any previous translation would be lost. Moreover,
 * since variables can be used in the bootstrap phase, we need a bootstrap hook
 * to provide a translation early enough to avoid misalignments between code
 * using the original values and code using the translated values. However
 * modules implementing hook_boot() should be aware that language initialization
 * did not happen yet and thus they cannot rely on translated variables.
 */
function hook_language_init() {
27
  global $conf;
28

29
  switch (drupal_container()->get(LANGUAGE_TYPE_INTERFACE)->langcode) {
30 31 32 33 34 35 36 37 38 39
    case 'it':
      $conf['site_name'] = 'Il mio sito Drupal';
      break;

    case 'fr':
      $conf['site_name'] = 'Mon site Drupal';
      break;
  }
}

40
/**
41
 * Perform alterations on language switcher links.
42
 *
43 44 45
 * A language switcher link may need to point to a different path or use a
 * translated link text before going through l(), which will just handle the
 * path aliases.
46 47 48
 *
 * @param $links
 *   Nested array of links keyed by language code.
49 50
 * @param $type
 *   The language type the links will switch.
51 52 53
 * @param $path
 *   The current path.
 */
54
function hook_language_switch_links_alter(array &$links, $type, $path) {
55
  global $language_interface;
56

57 58
  if ($type == LANGUAGE_TYPE_CONTENT && isset($links[$language_interface->langcode])) {
    foreach ($links[$language_interface->langcode] as $link) {
59
      $link['attributes']['class'][] = 'active-language';
60 61 62 63
    }
  }
}

64 65 66 67
/**
 * Allow modules to define their own language types.
 *
 * @return
68 69 70 71 72 73 74
 *   An associative array of language type definitions.
 *
 *   Each language type has an identifier key which is used as the name for the
 *   global variable corresponding to the language type in the bootstrap phase.
 *
 *   The language type definition is an associative array that may contain the
 *   following key-value pairs:
75 76
 *   - "name": The human-readable language type identifier.
 *   - "description": A description of the language type.
77 78 79
 *   - "fixed": A fixed array of language negotiation method identifiers to use
 *     to initialize this language. Defining this key makes the language type
 *     non-configurable and will always use the specified methods in the given
80
 *     priority order.
81 82 83 84 85 86 87
 */
function hook_language_types_info() {
  return array(
    'custom_language_type' => array(
      'name' => t('Custom language'),
      'description' => t('A custom language type.'),
    ),
88
    'fixed_custom_language_type' => array(
89
      'fixed' => array('custom_language_negotiation_method'),
90
    ),
91 92 93 94 95 96
  );
}

/**
 * Perform alterations on language types.
 *
97 98
 * @see hook_language_types_info().
 *
99 100 101 102 103 104 105 106 107 108
 * @param $language_types
 *   Array of language type definitions.
 */
function hook_language_types_info_alter(array &$language_types) {
  if (isset($language_types['custom_language_type'])) {
    $language_types['custom_language_type_custom']['description'] = t('A far better description.');
  }
}

/**
109
 * Allow modules to define their own language negotiation methods.
110 111
 *
 * @return
112 113 114 115 116 117
 *   An array of language negotiation method definitions. Each method has an
 *   identifier key. The language negotiation method definition is an indexed
 *   array that may contain the following key-value pairs:
 *   - "types": An array of allowed language types. If a language negotiation
 *     method does not specify which language types it should be used with, it
 *     will be available for all the configurable language types.
118 119
 *   - "callbacks": An array of functions that will be called to perform various
 *     tasks. Possible key-value pairs are:
120
 *     - "negotiation": Required. The callback that will determine the language
121
 *       value.
122 123
 *     - "language_switch": The callback that will determine the language
 *       switch links associated to the current language method.
124 125 126
 *     - "url_rewrite": The callback that will provide URL rewriting.
 *   - "file": A file that will be included before the callback is invoked; this
 *     allows callback functions to be in separate files.
127
 *   - "weight": The default weight the language negotiation method has.
128
 *   - "name": A human-readable identifier.
129 130
 *   - "description": A description of the language negotiation method.
 *   - "config": An internal path pointing to the language negotiation method
131 132
 *     configuration page.
 *   - "cache": The value Drupal's page cache should be set to for the current
133
 *     language negotiation method to be invoked.
134 135 136
 */
function hook_language_negotiation_info() {
  return array(
137
    'custom_language_negotiation_method' => array(
138
      'callbacks' => array(
139 140 141
        'negotiation' => 'custom_negotiation_callback',
        'language_switch' => 'custom_language_switch_callback',
        'url_rewrite' => 'custom_url_rewrite_callback',
142 143 144 145
      ),
      'file' => drupal_get_path('module', 'custom') . '/custom.module',
      'weight' => -4,
      'types' => array('custom_language_type'),
146 147
      'name' => t('Custom language negotiation method'),
      'description' => t('This is a custom language negotiation method.'),
148
      'cache' => 0,
149 150 151 152 153
    ),
  );
}

/**
154
 * Perform alterations on language negotiation methods.
155
 *
156 157
 * @param $negotiation_info
 *   Array of language negotiation method definitions.
158
 */
159 160 161
function hook_language_negotiation_info_alter(array &$negotiation_info) {
  if (isset($negotiation_info['custom_language_method'])) {
    $negotiation_info['custom_language_method']['config'] = 'admin/config/regional/language/detection/custom-language-method';
162 163 164
  }
}

165 166 167 168 169 170 171 172 173
/**
 * Perform alterations on the language fallback candidates.
 *
 * @param $fallback_candidates
 *   An array of language codes whose order will determine the language fallback
 *   order.
 */
function hook_language_fallback_candidates_alter(array &$fallback_candidates) {
  $fallback_candidates = array_reverse($fallback_candidates);
174 175
}

176 177 178
/**
 * @} End of "addtogroup hooks".
 */