language.module 7.75 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php

/**
 * @file
 * Add language handling functionality to Drupal.
 */

/**
 * Implements hook_help().
 */
function language_help($path, $arg) {
  switch ($path) {
    case 'admin/help#language':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Language module allows you to maintain a list of languages used on your Drupal site for providing language information for content and for interface translation (using the Locale module). For more information, see the online handbook entry for <a href="@language">Language module</a>.', array('@language' => 'http://drupal.org/handbook/modules/language/')) . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Configuring the list of languages') . '</dt>';
      $output .= '<dd>' . t('<a href="@configure-languages">Configure the list of languages</a> either using the built-in language list or providing any custom languages you wish.', array('@configure-languages' => url('admin/config/regional/language'))) . '</dd>';
      $output .= '</dl>';
      return $output;

    case 'admin/config/regional/language':
      return '<p>' . t('With multiple languages enabled, registered users may select their preferred language and authors can assign a specific language to content.') . '</p>';

    case 'admin/config/regional/language/add':
      return '<p>' . t('Add a language to be supported by your site. If your desired language is not available, pick <em>Custom language...</em> at the end and provide a language code and other details manually.') . '</p>';
  }
}

/**
 * Implements hook_menu().
 */
function language_menu() {
  $items['admin/config/regional/language'] = array(
    'title' => 'Languages',
    'description' => 'Configure languages for content and the user interface.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('language_admin_overview_form'),
    'access arguments' => array('administer languages'),
    'file' => 'language.admin.inc',
    'weight' => -10,
  );
  $items['admin/config/regional/language/list'] = array(
    'title' => 'List',
    'weight' => 0,
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/config/regional/language/add'] = array(
    'title' => 'Add language',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('language_admin_add_form'),
    'access arguments' => array('administer languages'),
    'weight' => 5,
    'type' => MENU_LOCAL_ACTION,
    'file' => 'language.admin.inc',
  );
  $items['admin/config/regional/language/edit/%language'] = array(
    'title' => 'Edit language',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('language_admin_edit_form', 5),
    'access arguments' => array('administer languages'),
    'file' => 'language.admin.inc',
  );
  $items['admin/config/regional/language/delete/%language'] = array(
    'title' => 'Confirm delete',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('language_admin_delete_form', 5),
    'access arguments' => array('administer languages'),
    'file' => 'language.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function language_permission() {
  return array(
    'administer languages' => array(
      'title' => t('Administer languages'),
    ),
  );
}

/**
 * Implements hook_theme().
 */
function language_theme() {
  return array(
    'language_admin_overview_form_table' => array(
      'render element' => 'form',
      'file' => 'language.admin.inc',
    ),
    'language_admin_operations' => array(
      'render element' => 'elements',
      'file' => 'language.admin.inc',
    ),
  );
}

/**
 * Loads a language object from the database.
 *
 * @param $langcode
 *   The language code.
 *
 * @return
 *   A fully-populated language object or FALSE.
 */
function language_load($langcode) {
  $languages = language_list();
  return isset($languages[$langcode]) ? $languages[$langcode] : FALSE;
}

/**
 * API function to add or update a language.
 *
 * @param $language
 *   Language object with properties corresponding to 'language' table columns.
 */
function language_save($language) {
124
  $language->is_new = !(bool) db_query_range('SELECT 1 FROM {language} WHERE langcode = :langcode', 0, 1, array(':langcode' => $language->langcode))->fetchField();
125
126
127
128
129

  // If name was not set, we add a predefined language.
  if (!isset($language->name)) {
    include_once DRUPAL_ROOT . '/core/includes/standard.inc';
    $predefined = standard_language_list();
130
131
    $language->name = $predefined[$language->langcode][0];
    $language->direction = isset($predefined[$language->langcode][2]) ? $predefined[$language->langcode][2] : LANGUAGE_LTR;
132
133
134
135
136
137
138
139
140
141
  }

  // Set to enabled for the default language and unless specified otherwise.
  if (!empty($language->default) || !isset($language->enabled)) {
    $language->enabled = TRUE;
  }
  // Let other modules modify $language before saved.
  module_invoke_all('language_presave', $language);

  // Save the record and inform others about the change.
142
  $t_args = array('%language' => $language->name, '%langcode' => $language->langcode);
143
144
145
146
147
148
  if ($language->is_new) {
    drupal_write_record('language', $language);
    module_invoke_all('language_insert', $language);
    watchdog('language', 'The %language (%langcode) language has been created.', $t_args);
  }
  else {
149
    drupal_write_record('language', $language, array('langcode'));
150
151
152
153
154
155
156
157
158
159
160
    module_invoke_all('language_update', $language);
    watchdog('language', 'The %language (%langcode) language has been updated.', $t_args);
  }

  if (!empty($language->default)) {
    // Set the new version of this language as default in a variable.
    $default_language = language_default();
    variable_set('language_default', $language);
  }

  // Update language count based on enabled language count.
161
  variable_set('language_count', db_query('SELECT COUNT(langcode) FROM {language} WHERE enabled = 1')->fetchField());
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

  // Kill the static cache in language_list().
  drupal_static_reset('language_list');

  return $language;
}

/**
 * Delete a language.
 *
 * @param $langcode
 *   Language code of the language to be deleted.
 * @return
 *   TRUE if language is successfully deleted. Otherwise FALSE.
 */
function language_delete($langcode) {
  $languages = language_list();
  if (isset($languages[$langcode])) {
    $language = $languages[$langcode];

    module_invoke_all('language_delete', $language);

    // Remove the language.
    db_delete('language')
186
      ->condition('langcode', $language->langcode)
187
188
189
190
191
192
193
194
      ->execute();

    if ($language->enabled) {
      variable_set('language_count', variable_get('language_count', 1) - 1);
    }

    drupal_static_reset('language_list');

195
    $t_args = array('%language' => $language->name, '%langcode' => $language->langcode);
196
197
198
199
200
    watchdog('language', 'The %language (%langcode) language has been removed.', $t_args);
    return TRUE;
  }
  return FALSE;
}
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227

/**
 * Implements hook_css_alter().
 *
 * This function checks all CSS files currently added via drupal_add_css() and
 * and checks to see if a related right to left CSS file should be included.
 */
function language_css_alter(&$css) {
  global $language;

  // If the current language is RTL, add the CSS file with the RTL overrides.
  if ($language->direction == LANGUAGE_RTL) {
    foreach ($css as $data => $item) {
      // Only provide RTL overrides for files.
      if ($item['type'] == 'file') {
        $rtl_path = str_replace('.css', '-rtl.css', $item['data']);
        if (file_exists($rtl_path) && !isset($css[$rtl_path])) {
          // Replicate the same item, but with the RTL path and a little larger
          // weight so that it appears directly after the original CSS file.
          $item['data'] = $rtl_path;
          $item['weight'] += 0.01;
          $css[$rtl_path] = $item;
        }
      }
    }
  }
}