locale.install 11.4 KB
Newer Older
1 2
<?php

3 4
/**
 * @file
5
 * Install, update, and uninstall functions for the Locale module.
6 7
 */

8
use Drupal\Core\Language\Language;
9
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSelected;
10

11 12 13 14 15
/**
 * Implements hook_install().
 */
function locale_install() {
  // Create the interface translations directory and ensure it's writable.
16
  if (!$directory = \Drupal::config('locale.settings')->get('translation.path')) {
17
    $directory = conf_path() . '/files/translations';
18
    \Drupal::config('locale.settings')->set('translation.path', $directory)->save();
19
  }
20 21 22
  file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
}

23
/**
24
 * Implements hook_uninstall().
25 26
 */
function locale_uninstall() {
27
  $config = \Drupal::config('locale.settings');
28
  // Delete all JavaScript translation files.
29
  $locale_js_directory = 'public://' . $config->get('javascript.directory');
30 31

  if (is_dir($locale_js_directory)) {
32
    $locale_javascripts = \Drupal::state()->get('locale.translation.javascript') ?: array();
33 34 35
    foreach ($locale_javascripts as $langcode => $file_suffix) {
      if (!empty($file_suffix)) {
        file_unmanaged_delete($locale_js_directory . '/' . $langcode . '_' . $file_suffix . '.js');
36 37 38 39
      }
    }
    // Delete the JavaScript translations directory if empty.
    if (!file_scan_directory($locale_js_directory, '/.*/')) {
40
      drupal_rmdir($locale_js_directory);
41
    }
42
  }
43

44
  // Clear variables.
45 46 47
  \Drupal::state()->delete('system.javascript_parsed');
  \Drupal::state()->delete('locale.translation.plurals');
  \Drupal::state()->delete('locale.translation.javascript');
48
}
49 50

/**
51
 * Implements hook_schema().
52 53 54
 */
function locale_schema() {
  $schema['locales_source'] = array(
55
    'description' => 'List of English source strings.',
56
    'fields' => array(
57 58 59
      'lid' => array(
        'type' => 'serial',
        'not null' => TRUE,
60
        'description' => 'Unique identifier of this string.',
61 62 63
      ),
      'source' => array(
        'type' => 'text',
64
        'mysql_type' => 'blob',
65
        'not null' => TRUE,
66
        'description' => 'The original string in English.',
67
      ),
68 69 70 71 72 73 74
      'context' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The context this string applies to.',
      ),
75 76 77 78 79
      'version' => array(
        'type' => 'varchar',
        'length' => 20,
        'not null' => TRUE,
        'default' => 'none',
80
        'description' => 'Version of Drupal where the string was last used (for locales optimization).',
81
      ),
82 83
    ),
    'primary key' => array('lid'),
84
    'indexes' => array(
85
      'source_context' => array(array('source', 30), 'context'),
86
    ),
87 88 89
  );

  $schema['locales_target'] = array(
90
    'description' => 'Stores translated versions of strings.',
91
    'fields' => array(
92 93 94 95
      'lid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
96
        'description' => 'Source string ID. References {locales_source}.lid.',
97 98 99
      ),
      'translation' => array(
        'type' => 'text',
100
        'mysql_type' => 'blob',
101
        'not null' => TRUE,
102
        'description' => 'Translation string value in this language.',
103 104 105 106 107 108
      ),
      'language' => array(
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
109
        'description' => 'Language code. References {language}.langcode.',
110
      ),
111 112 113 114 115 116
      'customized' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0, // LOCALE_NOT_CUSTOMIZED
        'description' => 'Boolean indicating whether the translation is custom to this site.',
      ),
117
    ),
118
    'primary key' => array('language', 'lid'),
119
    'foreign keys' => array(
120 121 122 123
      'locales_source' => array(
        'table' => 'locales_source',
        'columns' => array('lid' => 'lid'),
      ),
124
    ),
125
    'indexes' => array(
126
      'lid'      => array('lid'),
127 128 129
    ),
  );

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
  $schema['locales_location'] = array(
    'description' => 'Location information for source strings.',
    'fields' => array(
      'lid' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Unique identifier of this location.',
      ),
      'sid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'description' => 'Unique identifier of this string.',
      ),
      'type' => array(
        'type' => 'varchar',
        'length' => 50,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The location type (file, config, path, etc).',
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Type dependent location information (file name, path, etc).',
      ),
      'version' => array(
        'type' => 'varchar',
        'length' => 20,
        'not null' => TRUE,
        'default' => 'none',
        'description' => 'Version of Drupal where the location was found.',
      ),
    ),
    'primary key' => array('lid'),
    'foreign keys' => array(
      'locales_source' => array(
        'table' => 'locales_source',
        'columns' => array('sid' => 'lid'),
      ),
    ),
    'indexes' => array(
       'string_id' => array('sid'),
       'string_type' => array('sid', 'type'),
    ),
  );

178 179 180
  $schema['locale_file'] = array(
    'description' => 'File import status information for interface translation files.',
    'fields' => array(
181 182 183 184 185 186 187
      'project' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
        'default' => '',
        'description' => 'A unique short name to identify the project the file belongs to.',
      ),
188 189 190 191
      'langcode' => array(
        'type' => 'varchar',
        'length' => '12',
        'not null' => TRUE,
192 193
        'default' => '',
        'description' => 'Language code of this translation. References {language}.langcode.',
194 195 196 197 198 199
      ),
      'filename' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
200 201 202 203 204 205 206 207
        'description' => 'Filename of the imported file.',
      ),
      'version' => array(
        'type' => 'varchar',
        'length' => '128',
        'not null' => TRUE,
        'default' => '',
        'description' => 'Version tag of the imported file.',
208 209 210 211 212 213
      ),
      'uri' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
214
        'description' => 'URI of the remote file, the resulting local file or the locally imported file.',
215 216 217 218 219
      ),
      'timestamp' => array(
        'type' => 'int',
        'not null' => FALSE,
        'default' => 0,
220 221 222 223 224 225 226
        'description' => 'Unix timestamp of the imported file.',
      ),
      'last_checked' => array(
        'type' => 'int',
        'not null' => FALSE,
        'default' => 0,
        'description' => 'Unix timestamp of the last time this translation was confirmed to be the most recent release available.',
227 228
      ),
    ),
229
    'primary key' => array('project', 'langcode'),
230 231
  );

232 233 234 235 236 237 238 239 240 241
  $schema['locale_project'] = array(
    'description' => 'Translation status information for projects and server data.',
    'fields' => array(
      'name' => array(
        'description' => 'A unique short name to identify the project.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'project_type' => array(
242
        'description' => 'Project type: core, module, or theme.',
243 244 245 246 247
        'type' => 'varchar',
        'length' => 15,
        'not null' => TRUE,
      ),
      'core' => array(
248 249
        // http://drupal.org/node/542202#core has an example.
        'description' => 'Core compatibility string for this project, for example: 8.x',
250 251 252 253 254 255
        'type' => 'varchar',
        'length' => 4,
        'not null' => TRUE,
        'default' => '',
      ),
      'version' => array(
256 257
        // http://drupal.org/node/467026 has examples.
        'description' => 'The version release of the project, for example: 8.x-2.1 or 8.x-1.0-dev',
258 259 260 261 262 263 264 265 266 267 268 269 270
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
      ),
      'server_pattern' => array(
        'description' => 'Pattern of path and name of the gettext file at the translation server.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'status' => array(
271 272
        // locale.compare.inc gives possible values for status.
        'description' => 'The status of the project. Possible values are: 1 = enabled, 0 = disabled.',
273 274 275 276 277 278 279
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
      ),
    ),
    'primary key' => array('name'),
  );
280 281
  return $schema;
}
282

283 284 285 286 287 288 289
/**
 * Implements hook_requirements().
 */
function locale_requirements($phase) {
  $requirements = array();
  if ($phase == 'runtime') {
    $available_updates = array();
290
    $untranslated = array();
291 292 293
    $languages = locale_translatable_language_list();

    if ($languages) {
294
      // Determine the status of the translation updates per language.
295
      $status = locale_translation_get_status();
296
      if ($status) {
297
        foreach ($status as $project) {
298
          foreach ($project as $langcode => $project_info) {
299 300
            if (empty($project_info->type)) {
              $untranslated[$langcode] = $languages[$langcode]->name;
301 302 303 304 305 306 307
            }
            elseif ($project_info->type == LOCALE_TRANSLATION_LOCAL || $project_info->type == LOCALE_TRANSLATION_REMOTE) {
              $available_updates[$langcode] = $languages[$langcode]->name;
            }
          }
        }

308
        if ($available_updates || $untranslated) {
309 310 311 312 313 314 315 316 317 318 319 320 321
          if ($available_updates) {
            $requirements['locale_translation'] = array(
              'title' => 'Translation update status',
              'value' => l(t('Updates available'), 'admin/reports/translations'),
              'severity' => REQUIREMENT_WARNING,
              'description' => t('Updates available for: @languages. See the <a href="@updates">Available translation updates</a> page for more information.', array('@languages' => implode(', ', $available_updates), '@updates' => url('admin/reports/translations'))),
            );
          }
          else {
            $requirements['locale_translation'] = array(
              'title' => 'Translation update status',
              'value' => t('Missing translations'),
              'severity' => REQUIREMENT_INFO,
322
              'description' => t('Missing translations for: @languages. See the <a href="@updates">Available translation updates</a> page for more information.', array('@languages' => implode(', ', $untranslated), '@updates' => url('admin/reports/translations'))),
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
            );
          }
        }
        else {
          $requirements['locale_translation'] = array(
            'title' => 'Translation update status',
            'value' => t('Up to date'),
            'severity' => REQUIREMENT_OK,
          );
        }
      }
      else {
        $requirements['locale_translation'] = array(
          'title' => 'Translation update status',
          'value' => l(t('Can not determine status'), 'admin/reports/translations'),
          'severity' => REQUIREMENT_WARNING,
          'description' => t('No translation status is available. See the <a href="@updates">Available translation updates</a> page for more information.', array('@updates' => url('admin/reports/translations'))),
        );
      }
    }
  }
  return $requirements;
}