[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/modules/locale/ -> locale.module (source)

   1  <?php
   2  
   3  /**
   4   * @file
   5   *   Add language handling functionality and enables the translation of the
   6   *   user interface to languages other than English.
   7   *
   8   *   When enabled, multiple languages can be set up. The site interface
   9   *   can be displayed in different languages, as well as nodes can have languages
  10   *   assigned. The setup of languages and translations is completely web based.
  11   *   Gettext portable object files are supported.
  12   */
  13  
  14  // ---------------------------------------------------------------------------------
  15  // Hook implementations
  16  
  17  /**
  18   * Implementation of hook_help().
  19   */
  20  function locale_help($path, $arg) {
  21    switch ($path) {
  22      case 'admin/help#locale':
  23        $output = '<p>'. t('The locale module allows your Drupal site to be presented in languages other than the default English, a defining feature of multi-lingual websites. The locale module works by examining text as it is about to be displayed: when a translation of the text is available in the language to be displayed, the translation is displayed rather than the original text. When a translation is unavailable, the original text is displayed, and then stored for later review by a translator.') .'</p>';
  24        $output .= '<p>'. t('Beyond translation of the Drupal interface, the locale module provides a feature set tailored to the needs of a multi-lingual site. Language negotiation allows your site to automatically change language based on the domain or path used for each request. Users may (optionally) select their preferred language on their <em>My account</em> page, and your site can be configured to honor a web browser\'s preferred language settings. Your site content can be created in (and translated to) any enabled language, and each post may have a language-appropriate alias for each of its translations. The locale module works in concert with the <a href="@content-help">content translation module</a> to manage translated content.', array('@content-help' => url('admin/help/translation'))) .'</p>';
  25        $output .= '<p>'. t('Translations may be provided by:') .'</p>';
  26        $output .= '<ul><li>'. t("translating the original text via the locale module's integrated web interface, or") .'</li>';
  27        $output .= '<li>'. t('importing files from a set of existing translations, known as a translation package. A translation package enables the display of a specific version of Drupal in a specific language, and contain files in the Gettext Portable Object (<em>.po</em>) format. Although not all languages are available for every version of Drupal, translation packages for many languages are available for download from the <a href="@translations">Drupal translation page</a>.', array('@translations' => 'http://localize.drupal.org')) .'</li></ul>';
  28        $output .= '<p>'. t('If an existing translation package does not meet your needs, the Gettext Portable Object (<em>.po</em>) files within a package may be modified, or new <em>.po</em> files may be created, using a desktop Gettext editor. The locale module\'s <a href="@import">import</a> feature allows the translated strings from a new or modified <em>.po</em> file to be added to your site. The locale module\'s <a href="@export">export</a> feature generates files from your site\'s translated strings, that can either be shared with others or edited offline by a Gettext translation editor.', array('@import' => url('admin/build/translate/import'), '@export' => url('admin/build/translate/export'))) .'</p>';
  29        $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@locale">Locale module</a>.', array('@locale' => 'http://drupal.org/handbook/modules/locale/')) .'</p>';
  30        return $output;
  31      case 'admin/settings/language':
  32        $output = '<p>'. t("This page provides an overview of your site's enabled languages. If multiple languages are available and enabled, the text on your site interface may be translated, registered users may select their preferred language on the <em>My account</em> page, and site authors may indicate a specific language when creating posts. The site's default language is used for anonymous visitors and for users who have not selected a preferred language.") .'</p>';
  33        $output .= '<p>'. t('For each language available on the site, use the <em>edit</em> link to configure language details, including name, an optional language-specific path or domain, and whether the language is natively presented either left-to-right or right-to-left. These languages also appear in the <em>Language</em> selection when creating a post of a content type with multilingual support.') .'</p>';
  34        $output .= '<p>'. t('Use the <a href="@add-language">add language page</a> to enable additional languages (and automatically import files from a translation package, if available), the <a href="@search">translate interface page</a> to locate strings for manual translation, or the <a href="@import">import page</a> to add translations from individual <em>.po</em> files. A number of contributed translation packages containing <em>.po</em> files are available on the <a href="@translations">Drupal.org translations page</a>.', array('@add-language' => url('admin/settings/language/add'), '@search' => url('admin/build/translate/search'), '@import' => url('admin/build/translate/import'), '@translations' => 'http://localize.drupal.org')) .'</p>';
  35        return $output;
  36      case 'admin/settings/language/add':
  37        return '<p>'. t('Add all languages to be supported by your site. If your desired language is not available in the <em>Language name</em> drop-down, click <em>Custom language</em> and provide a language code and other details manually. When providing a language code manually, be sure to enter a standardized language code, since this code may be used by browsers to determine an appropriate display language.') .'</p>';
  38      case 'admin/settings/language/configure':
  39        $output = '<p>'. t("Language negotiation settings determine the site's presentation language. Available options include:") .'</p>';
  40        $output .= '<ul><li>'. t('<strong>None.</strong> The default language is used for site presentation, though users may (optionally) select a preferred language on the <em>My Account</em> page. (User language preferences will be used for site e-mails, if available.)') .'</li>';
  41        $output .= '<li>'. t('<strong>Path prefix only.</strong> The presentation language is determined by examining the path for a language code or other custom string that matches the path prefix (if any) specified for each language. If a suitable prefix is not identified, the default language is used. <em>Example: "example.com/de/contact" sets presentation language to German based on the use of "de" within the path.</em>') .'</li>';
  42        $output .= '<li>'. t("<strong>Path prefix with language fallback.</strong> The presentation language is determined by examining the path for a language code or other custom string that matches the path prefix (if any) specified for each language. If a suitable prefix is not identified, the display language is determined by the user's language preferences from the <em>My Account</em> page, or by the browser's language settings. If a presentation language cannot be determined, the default language is used.") .'</li>';
  43        $output .= '<li>'. t('<strong>Domain name only.</strong> The presentation language is determined by examining the domain used to access the site, and comparing it to the language domain (if any) specified for each language. If a match is not identified, the default language is used. <em>Example: "http://de.example.com/contact" sets presentation language to German based on the use of "http://de.example.com" in the domain.</em>') .'</li></ul>';
  44        $output .= '<p>'. t('The path prefix or domain name for a language may be set by editing the <a href="@languages">available languages</a>. In the absence of an appropriate match, the site is displayed in the <a href="@languages">default language</a>.', array('@languages' => url('admin/settings/language'))) .'</p>';
  45        return $output;
  46      case 'admin/build/translate':
  47        $output = '<p>'. t('This page provides an overview of available translatable strings. Drupal displays translatable strings in text groups; modules may define additional text groups containing other translatable strings. Because text groups provide a method of grouping related strings, they are often used to focus translation efforts on specific areas of the Drupal interface.') .'</p>';
  48        $output .= '<p>'. t('Review the <a href="@languages">languages page</a> for more information on adding support for additional languages.', array('@languages' => url('admin/settings/language'))) .'</p>';
  49        return $output;
  50      case 'admin/build/translate/import':
  51        $output = '<p>'. t('This page imports the translated strings contained in an individual Gettext Portable Object (<em>.po</em>) file. Normally distributed as part of a translation package (each translation package may contain several <em>.po</em> files), a <em>.po</em> file may need to be imported after off-line editing in a Gettext translation editor. Importing an individual <em>.po</em> file may be a lengthy process.') .'</p>';
  52        $output .= '<p>'. t('Note that the <em>.po</em> files within a translation package are imported automatically (if available) when new modules or themes are enabled, or as new languages are added. Since this page only allows the import of one <em>.po</em> file at a time, it may be simpler to download and extract a translation package into your Drupal installation directory and <a href="@language-add">add the language</a> (which automatically imports all <em>.po</em> files within the package). Translation packages are available for download on the <a href="@translations">Drupal translation page</a>.', array('@language-add' => url('admin/settings/language/add'), '@translations' => 'http://localize.drupal.org')) .'</p>';
  53        return $output;
  54      case 'admin/build/translate/export':
  55        return '<p>'. t('This page exports the translated strings used by your site. An export file may be in Gettext Portable Object (<em>.po</em>) form, which includes both the original string and the translation (used to share translations with others), or in Gettext Portable Object Template (<em>.pot</em>) form, which includes the original strings only (used to create new translations with a Gettext translation editor).') .'</p>';
  56      case 'admin/build/translate/search':
  57        return '<p>'. t('This page allows a translator to search for specific translated and untranslated strings, and is used when creating or editing translations. (Note: For translation tasks involving many strings, it may be more convenient to <a href="@export">export</a> strings for off-line editing in a desktop Gettext translation editor.) Searches may be limited to strings found within a specific text group or in a specific language.', array('@export' => url('admin/build/translate/export'))) .'</p>';
  58      case 'admin/build/block/configure':
  59        if ($arg[4] == 'locale' && $arg[5] == 0) {
  60          return '<p>'. t('This block is only shown if <a href="@languages">at least two languages are enabled</a> and <a href="@configuration">language negotiation</a> is set to something other than <em>None</em>.', array('@languages' => url('admin/settings/language'), '@configuration' => url('admin/settings/language/configure'))) .'</p>';
  61        }
  62        break;
  63    }
  64  }
  65  
  66  /**
  67   * Implementation of hook_menu().
  68   *
  69   * Locale module only provides administrative menu items, so all
  70   * menu items are invoked through locale_inc_callback().
  71   */
  72  function locale_menu() {
  73    // Manage languages
  74    $items['admin/settings/language'] = array(
  75      'title' => 'Languages',
  76      'description' => 'Configure languages for content and the user interface.',
  77      'page callback' => 'locale_inc_callback',
  78      'page arguments' => array('drupal_get_form', 'locale_languages_overview_form'),
  79      'access arguments' => array('administer languages'),
  80    );
  81    $items['admin/settings/language/overview'] = array(
  82      'title' => 'List',
  83      'weight' => 0,
  84      'type' => MENU_DEFAULT_LOCAL_TASK,
  85    );
  86    $items['admin/settings/language/add'] = array(
  87      'title' => 'Add language',
  88      'page callback' => 'locale_inc_callback',
  89      'page arguments' => array('locale_languages_add_screen'), // two forms concatenated
  90      'access arguments' => array('administer languages'),
  91      'weight' => 5,
  92      'type' => MENU_LOCAL_TASK,
  93    );
  94    $items['admin/settings/language/configure'] = array(
  95      'title' => 'Configure',
  96      'page callback' => 'locale_inc_callback',
  97      'page arguments' => array('drupal_get_form', 'locale_languages_configure_form'),
  98      'access arguments' => array('administer languages'),
  99      'weight' => 10,
 100      'type' => MENU_LOCAL_TASK,
 101    );
 102    $items['admin/settings/language/edit/%'] = array(
 103      'title' => 'Edit language',
 104      'page callback' => 'locale_inc_callback',
 105      'page arguments' => array('drupal_get_form', 'locale_languages_edit_form', 4),
 106      'access arguments' => array('administer languages'),
 107      'type' => MENU_CALLBACK,
 108    );
 109    $items['admin/settings/language/delete/%'] = array(
 110      'title' => 'Confirm',
 111      'page callback' => 'locale_inc_callback',
 112      'page arguments' => array('drupal_get_form', 'locale_languages_delete_form', 4),
 113      'access arguments' => array('administer languages'),
 114      'type' => MENU_CALLBACK,
 115    );
 116  
 117    // Translation functionality
 118    $items['admin/build/translate'] = array(
 119      'title' => 'Translate interface',
 120      'description' => 'Translate the built in interface and optionally other text.',
 121      'page callback' => 'locale_inc_callback',
 122      'page arguments' => array('locale_translate_overview_screen'), // not a form, just a table
 123      'access arguments' => array('translate interface'),
 124    );
 125    $items['admin/build/translate/overview'] = array(
 126      'title' => 'Overview',
 127      'weight' => 0,
 128      'type' => MENU_DEFAULT_LOCAL_TASK,
 129    );
 130    $items['admin/build/translate/search'] = array(
 131      'title' => 'Search',
 132      'weight' => 10,
 133      'type' => MENU_LOCAL_TASK,
 134      'page callback' => 'locale_inc_callback',
 135      'page arguments' => array('locale_translate_seek_screen'), // search results and form concatenated
 136      'access arguments' => array('translate interface'),
 137    );
 138    $items['admin/build/translate/import'] = array(
 139      'title' => 'Import',
 140      'page callback' => 'locale_inc_callback',
 141      'page arguments' => array('drupal_get_form', 'locale_translate_import_form'),
 142      'access arguments' => array('translate interface'),
 143      'weight' => 20,
 144      'type' => MENU_LOCAL_TASK,
 145    );
 146    $items['admin/build/translate/export'] = array(
 147      'title' => 'Export',
 148      'page callback' => 'locale_inc_callback',
 149      'page arguments' => array('locale_translate_export_screen'), // possibly multiple forms concatenated
 150      'access arguments' => array('translate interface'),
 151      'weight' => 30,
 152      'type' => MENU_LOCAL_TASK,
 153    );
 154    $items['admin/build/translate/edit/%'] = array(
 155      'title' => 'Edit string',
 156      'page callback' => 'locale_inc_callback',
 157      'page arguments' => array('drupal_get_form', 'locale_translate_edit_form', 4),
 158      'access arguments' => array('translate interface'),
 159      'type' => MENU_CALLBACK,
 160    );
 161    $items['admin/build/translate/delete/%'] = array(
 162      'title' => 'Delete string',
 163      'page callback' => 'locale_inc_callback',
 164      'page arguments' => array('locale_translate_delete_page', 4),
 165      'access arguments' => array('translate interface'),
 166      'type' => MENU_CALLBACK,
 167    );
 168  
 169    return $items;
 170  }
 171  
 172  /**
 173   * Wrapper function to be able to set callbacks in locale.inc
 174   */
 175  function locale_inc_callback() {
 176    $args = func_get_args();
 177    $function = array_shift($args);
 178    include_once  './includes/locale.inc';
 179    return call_user_func_array($function, $args);
 180  }
 181  
 182  /**
 183   * Implementation of hook_perm().
 184   */
 185  function locale_perm() {
 186    return array('administer languages', 'translate interface');
 187  }
 188  
 189  /**
 190   * Implementation of hook_locale().
 191   */
 192  function locale_locale($op = 'groups') {
 193    switch ($op) {
 194      case 'groups':
 195        return array('default' => t('Built-in interface'));
 196    }
 197  }
 198  
 199  /**
 200   * Implementation of hook_user().
 201   */
 202  function locale_user($type, $edit, &$user, $category = NULL) {
 203    global $language;
 204  
 205    // If we have more then one language and either creating a user on the
 206    // admin interface or edit the user, show the language selector.
 207    if (variable_get('language_count', 1) > 1 && ($type == 'register' && user_access('administer users') || $type == 'form' && $category == 'account' )) {
 208      $languages = language_list('enabled');
 209      $languages = $languages[1];
 210  
 211      // If the user is being created, we set the user language to the page language.
 212      $user_preferred_language = $user ? user_preferred_language($user) : $language;
 213  
 214      $names = array();
 215      foreach ($languages as $langcode => $item) {
 216        $name = t($item->name);
 217        $names[check_plain($langcode)] = check_plain($name . ($item->native != $name ? ' ('. $item->native .')' : ''));
 218      }
 219      $form['locale'] = array(
 220        '#type' => 'fieldset',
 221        '#title' => t('Language settings'),
 222        '#weight' => 1,
 223      );
 224  
 225      // Get language negotiation settings.
 226      $mode = variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE);
 227      $form['locale']['language'] = array(
 228        '#type' => (count($names) <= 5 ? 'radios' : 'select'),
 229        '#title' => t('Language'),
 230        '#default_value' => check_plain($user_preferred_language->language),
 231        '#options' => $names,
 232        '#description' => ($mode == LANGUAGE_NEGOTIATION_PATH) ? t("This account's default language for e-mails, and preferred language for site presentation.") : t("This account's default language for e-mails."),
 233      );
 234      return $form;
 235    }
 236  }
 237  
 238  /**
 239   * Implementation of hook_form_alter(). Adds language fields to forms.
 240   */
 241  function locale_form_alter(&$form, $form_state, $form_id) {
 242    switch ($form_id) {
 243  
 244      // Language field for paths
 245      case 'path_admin_form':
 246        $form['language'] = array(
 247          '#type' => 'select',
 248          '#title' => t('Language'),
 249          '#options' => array('' => t('All languages')) + locale_language_list('name'),
 250          '#default_value' => $form['language']['#value'],
 251          '#weight' => -10,
 252          '#description' => t('A path alias set for a specific language will always be used when displaying this page in that language, and takes precedence over path aliases set for <em>All languages</em>.'),
 253        );
 254        break;
 255  
 256      // Language setting for content types
 257      case 'node_type_form':
 258        if (isset($form['identity']['type'])) {
 259          $form['workflow']['language_content_type'] = array(
 260            '#type' => 'radios',
 261            '#title' => t('Multilingual support'),
 262            '#default_value' => variable_get('language_content_type_'. $form['#node_type']->type, 0),
 263            '#options' => array(t('Disabled'), t('Enabled')),
 264            '#description' => t('Enable multilingual support for this content type. If enabled, a language selection field will be added to the editing form, allowing you to select from one of the <a href="!languages">enabled languages</a>. If disabled, new posts are saved with the default language. Existing content will not be affected by changing this option.', array('!languages' => url('admin/settings/language'))),
 265          );
 266        }
 267        break;
 268  
 269      // Language field for nodes
 270      default:
 271        if (isset($form['#id']) && $form['#id'] == 'node-form') {
 272          if (isset($form['#node']->type) && variable_get('language_content_type_'. $form['#node']->type, 0)) {
 273            $form['language'] = array(
 274              '#type' => 'select',
 275              '#title' => t('Language'),
 276              '#default_value' => (isset($form['#node']->language) ? $form['#node']->language : ''),
 277              '#options' => array('' => t('Language neutral')) + locale_language_list('name'),
 278            );
 279          }
 280          // Node type without language selector: assign the default for new nodes
 281          elseif (!isset($form['#node']->nid)) {
 282            $default = language_default();
 283            $form['language'] = array(
 284              '#type' => 'value',
 285              '#value' => $default->language
 286            );
 287          }
 288        }
 289    }
 290  }
 291  
 292  /**
 293   * Implementation of hook_theme()
 294   */
 295  function locale_theme() {
 296    return array(
 297      'locale_languages_overview_form' => array(
 298        'arguments' => array('form' => array()),
 299      ),
 300    );
 301  }
 302  
 303  /**
 304   * Implementation of hook_node_type().
 305   */
 306  function locale_node_type($op, $info) {
 307    if ($op == 'delete') {
 308      variable_del('language_content_type_'. $info->type);
 309    }
 310  }
 311  
 312  // ---------------------------------------------------------------------------------
 313  // Locale core functionality
 314  
 315  /**
 316   * Provides interface translation services.
 317   *
 318   * This function is called from t() to translate a string if needed.
 319   *
 320   * @param $string
 321   *   A string to look up translation for. If omitted, all the
 322   *   cached strings will be returned in all languages already
 323   *   used on the page.
 324   * @param $langcode
 325   *   Language code to use for the lookup.
 326   * @param $reset
 327   *   Set to TRUE to reset the in-memory cache.
 328   */
 329  function locale($string = NULL, $langcode = NULL, $reset = FALSE) {
 330    global $language;
 331    static $locale_t;
 332  
 333    if ($reset) {
 334      // Reset in-memory cache.
 335      $locale_t = NULL;
 336    }
 337  
 338    if (!isset($string)) {
 339      // Return all cached strings if no string was specified
 340      return $locale_t;
 341    }
 342  
 343    $langcode = isset($langcode) ? $langcode : $language->language;
 344  
 345    // Store database cached translations in a static var.
 346    if (!isset($locale_t[$langcode])) {
 347      $locale_t[$langcode] = array();
 348      // Disabling the usage of string caching allows a module to watch for
 349      // the exact list of strings used on a page. From a performance
 350      // perspective that is a really bad idea, so we have no user
 351      // interface for this. Be careful when turning this option off!
 352      if (variable_get('locale_cache_strings', 1) == 1) {
 353        if ($cache = cache_get('locale:'. $langcode, 'cache')) {
 354          $locale_t[$langcode] = $cache->data;
 355        }
 356        elseif (lock_acquire('locale_cache_' . $langcode)) {
 357          // Refresh database stored cache of translations for given language.
 358          // We only store short strings used in current version, to improve
 359          // performance and consume less memory.
 360          $result = db_query("SELECT s.source, t.translation, t.language FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = '%s' WHERE s.textgroup = 'default' AND s.version = '%s' AND LENGTH(s.source) < 75", $langcode, VERSION);
 361          while ($data = db_fetch_object($result)) {
 362            $locale_t[$langcode][$data->source] = (empty($data->translation) ? TRUE : $data->translation);
 363          }
 364          cache_set('locale:'. $langcode, $locale_t[$langcode]);
 365          lock_release('locale_cache_' . $langcode);
 366        }
 367      }
 368    }
 369  
 370    // If we have the translation cached, skip checking the database
 371    if (!isset($locale_t[$langcode][$string])) {
 372  
 373      // We do not have this translation cached, so get it from the DB.
 374      $translation = db_fetch_object(db_query("SELECT s.lid, t.translation, s.version FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = '%s' WHERE s.source = '%s' AND s.textgroup = 'default'", $langcode, $string));
 375      if ($translation) {
 376        // We have the source string at least.
 377        // Cache translation string or TRUE if no translation exists.
 378        $locale_t[$langcode][$string] = (empty($translation->translation) ? TRUE : $translation->translation);
 379  
 380        if ($translation->version != VERSION) {
 381          // This is the first use of this string under current Drupal version. Save version
 382          // and clear cache, to include the string into caching next time. Saved version is
 383          // also a string-history information for later pruning of the tables.
 384          db_query("UPDATE {locales_source} SET version = '%s' WHERE lid = %d", VERSION, $translation->lid);
 385          cache_clear_all('locale:', 'cache', TRUE);
 386        }
 387      }
 388      else {
 389        // We don't have the source string, cache this as untranslated.
 390        db_query("INSERT INTO {locales_source} (location, source, textgroup, version) VALUES ('%s', '%s', 'default', '%s')", request_uri(), $string, VERSION);
 391        $locale_t[$langcode][$string] = TRUE;
 392        // Clear locale cache so this string can be added in a later request.
 393        cache_clear_all('locale:', 'cache', TRUE);
 394      }
 395    }
 396  
 397    return ($locale_t[$langcode][$string] === TRUE ? $string : $locale_t[$langcode][$string]);
 398  }
 399  
 400  /**
 401   * Returns plural form index for a specific number.
 402   *
 403   * The index is computed from the formula of this language.
 404   *
 405   * @param $count
 406   *   Number to return plural for.
 407   * @param $langcode
 408   *   Optional language code to translate to a language other than
 409   *   what is used to display the page.
 410   */
 411  function locale_get_plural($count, $langcode = NULL) {
 412    global $language;
 413    static $locale_formula, $plurals = array();
 414  
 415    $langcode = $langcode ? $langcode : $language->language;
 416  
 417    if (!isset($plurals[$langcode][$count])) {
 418      if (!isset($locale_formula)) {
 419        $language_list = language_list();
 420        $locale_formula[$langcode] = $language_list[$langcode]->formula;
 421      }
 422      if ($locale_formula[$langcode]) {
 423        $n = $count;
 424        $plurals[$langcode][$count] = @eval('return intval('. $locale_formula[$langcode] .');');
 425        return $plurals[$langcode][$count];
 426      }
 427      else {
 428        $plurals[$langcode][$count] = -1;
 429        return -1;
 430      }
 431    }
 432    return $plurals[$langcode][$count];
 433  }
 434  
 435  
 436  /**
 437   * Returns a language name
 438   */
 439  function locale_language_name($lang) {
 440    static $list = NULL;
 441    if (!isset($list)) {
 442      $list = locale_language_list();
 443    }
 444    return ($lang && isset($list[$lang])) ? $list[$lang] : t('All');
 445  }
 446  
 447  /**
 448   * Returns array of language names
 449   *
 450   * @param $field
 451   *   'name' => names in current language, localized
 452   *   'native' => native names
 453   * @param $all
 454   *   Boolean to return all languages or only enabled ones
 455   */
 456  function locale_language_list($field = 'name', $all = FALSE) {
 457    if ($all) {
 458      $languages = language_list();
 459    }
 460    else {
 461      $languages = language_list('enabled');
 462      $languages = $languages[1];
 463    }
 464    $list = array();
 465    foreach ($languages as $language) {
 466      $list[$language->language] = ($field == 'name') ? t($language->name) : $language->$field;
 467    }
 468    return $list;
 469  }
 470  
 471  /**
 472   * Imports translations when new modules or themes are installed or enabled.
 473   *
 474   * This function will either import translation for the component change
 475   * right away, or start a batch if more files need to be imported.
 476   *
 477   * @param $components
 478   *   An array of component (theme and/or module) names to import
 479   *   translations for.
 480   */
 481  function locale_system_update($components) {
 482    include_once  'includes/locale.inc';
 483    if ($batch = locale_batch_by_component($components)) {
 484      batch_set($batch);
 485    }
 486  }
 487  
 488  /**
 489   * Update JavaScript translation file, if required, and add it to the page.
 490   *
 491   * This function checks all JavaScript files currently added via drupal_add_js()
 492   * and invokes parsing if they have not yet been parsed for Drupal.t()
 493   * and Drupal.formatPlural() calls. Also refreshes the JavaScript translation
 494   * file if necessary, and adds it to the page.
 495   */
 496  function locale_update_js_files() {
 497    global $language;
 498  
 499    $dir = file_create_path(variable_get('locale_js_directory', 'languages'));
 500    $parsed = variable_get('javascript_parsed', array());
 501  
 502    // The first three parameters are NULL in order to get an array with all
 503    // scopes. This is necessary to prevent recreation of JS translation files
 504    // when new files are added for example in the footer.
 505    $javascript = drupal_add_js(NULL, NULL, NULL);
 506    $files = $new_files = FALSE;
 507  
 508    foreach ($javascript as $scope) {
 509      foreach ($scope as $type => $data) {
 510        if ($type != 'setting' && $type != 'inline') {
 511          foreach ($data as $filepath => $info) {
 512            $files = TRUE;
 513            if (!in_array($filepath, $parsed)) {
 514              // Don't parse our own translations files.
 515              if (substr($filepath, 0, strlen($dir)) != $dir) {
 516                locale_inc_callback('_locale_parse_js_file', $filepath);
 517                $parsed[] = $filepath;
 518                $new_files = TRUE;
 519              }
 520            }
 521          }
 522        }
 523      }
 524    }
 525  
 526    // If there are any new source files we parsed, invalidate existing
 527    // JavaScript translation files for all languages, adding the refresh
 528    // flags into the existing array.
 529    if ($new_files) {
 530      $parsed += locale_inc_callback('_locale_invalidate_js');
 531    }
 532  
 533    // If necessary, rebuild the translation file for the current language.
 534    if (!empty($parsed['refresh:'. $language->language])) {
 535      // Don't clear the refresh flag on failure, so that another try will
 536      // be performed later.
 537      if (locale_inc_callback('_locale_rebuild_js')) {
 538        unset($parsed['refresh:'. $language->language]);
 539      }
 540      // Store any changes after refresh was attempted.
 541      variable_set('javascript_parsed', $parsed);
 542    }
 543    // If no refresh was attempted, but we have new source files, we need
 544    // to store them too. This occurs if current page is in English.
 545    else if ($new_files) {
 546      variable_set('javascript_parsed', $parsed);
 547    }
 548  
 549    // Add the translation JavaScript file to the page.
 550    if ($files && !empty($language->javascript)) {
 551      drupal_add_js($dir .'/'. $language->language .'_'. $language->javascript .'.js', 'core');
 552    }
 553  }
 554  
 555  // ---------------------------------------------------------------------------------
 556  // Language switcher block
 557  
 558  /**
 559   * Implementation of hook_block().
 560   * Displays a language switcher. Translation links may be provided by other modules.
 561   */
 562  function locale_block($op = 'list', $delta = 0) {
 563    if ($op == 'list') {
 564      $block[0]['info'] = t('Language switcher');
 565      // Not worth caching.
 566      $block[0]['cache'] = BLOCK_NO_CACHE;
 567      return $block;
 568    }
 569  
 570    // Only show if we have at least two languages and language dependent
 571    // web addresses, so we can actually link to other language versions.
 572    elseif ($op == 'view' && variable_get('language_count', 1) > 1 && variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE) != LANGUAGE_NEGOTIATION_NONE) {
 573      $path = drupal_is_front_page() ? '<front>' : $_GET['q'];
 574      $languages = language_list('enabled');
 575      $links = array();
 576      foreach ($languages[1] as $language) {
 577        $links[$language->language] = array(
 578          'href'       => $path,
 579          'title'      => $language->native,
 580          'language'   => $language,
 581          'attributes' => array('class' => 'language-link'),
 582        );
 583      }
 584  
 585      // Allow modules to provide translations for specific links.
 586      // A translation link may need to point to a different path or use
 587      // a translated link text before going through l(), which will just
 588      // handle the path aliases.
 589      drupal_alter('translation_link', $links, $path);
 590  
 591      $block['subject'] = t('Languages');
 592      $block['content'] = theme('links', $links, array());
 593      return $block;
 594    }
 595  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7