locale.install

Same filename and directory in other branches
  1. 10 core/modules/locale/locale.install
  2. 11.x core/modules/locale/locale.install
  3. 9 core/modules/locale/locale.install
  4. 8.9.x core/modules/locale/locale.install
  5. 7.x modules/locale/locale.install

Install, update, and uninstall functions for the Locale module.

File

core/modules/locale/locale.install

View source
<?php


/**
 * @file
 * Install, update, and uninstall functions for the Locale module.
 */

use Drupal\Core\Database\SchemaDefinition\Column;
use Drupal\Core\Database\SchemaDefinition\ColumnSize;
use Drupal\Core\Database\SchemaDefinition\ColumnType;
use Drupal\Core\Database\SchemaDefinition\ForeignKey;
use Drupal\Core\Database\SchemaDefinition\Index;
use Drupal\Core\Database\SchemaDefinition\IntValue;
use Drupal\Core\Database\SchemaDefinition\PrimaryKey;
use Drupal\Core\Database\SchemaDefinition\Schema;
use Drupal\Core\Database\SchemaDefinition\SchemaDefinitionType;
use Drupal\Core\Database\SchemaDefinition\StringValue;
use Drupal\Core\Database\SchemaDefinition\Table;
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\FileSystemInterface;
use Drupal\locale\StreamWrapper\TranslationsStream;

/**
 * Implements hook_install().
 */
function locale_install() : void {
  // Create the interface translations directory and ensure it's writable.
  $directory = TranslationsStream::basePath();
  \Drupal::service('file_system')->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
}

/**
 * Implements hook_uninstall().
 */
function locale_uninstall() : void {
  $config = \Drupal::config('locale.settings');
  // Delete all JavaScript translation files.
  $locale_js_directory = 'assets://' . $config->get('javascript.directory');
  if (is_dir($locale_js_directory)) {
    $locale_javascripts = \Drupal::state()->get('locale.translation.javascript', []);
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    foreach ($locale_javascripts as $langcode => $file_suffix) {
      if (!empty($file_suffix)) {
        try {
          $file_system->delete($locale_js_directory . '/' . $langcode . '_' . $file_suffix . '.js');
        } catch (FileException) {
          // Ignore and continue.
        }
      }
    }
    // Delete the JavaScript translations directory if empty.
    if (is_dir($locale_js_directory)) {
      if (!$file_system->scanDirectory($locale_js_directory, '/.*/')) {
        $file_system->rmdir($locale_js_directory);
      }
    }
  }
  // Clear variables.
  \Drupal::state()->delete('system.javascript_parsed');
  \Drupal::state()->delete('locale.translation.plurals');
  \Drupal::state()->delete('locale.translation.javascript');
}

/**
 * Implements hook_schema().
 */
function locale_schema() : Schema {
  $locales_source = new Table(name: 'locales_source', description: 'List of English source strings.', columns: [
    Column::serial(name: 'lid', description: 'Unique identifier of this string.'),
    Column::create(type: ColumnType::Text, name: 'source', description: 'The original string in English.', notNull: TRUE, dbSpecificExtra: [
      'mysql' => [
        'mysql_type' => 'blob',
      ],
    ]),
    Column::varcharAscii(name: 'context', description: 'The context this string applies to.', length: 255, notNull: TRUE, default: new StringValue('')),
    Column::varcharAscii(name: 'version', description: 'Version of Drupal where the string was last used (for locales optimization).', length: 20, notNull: TRUE, default: new StringValue('none')),
  ], primaryKey: new PrimaryKey([
    'lid',
  ]), indexes: [
    new Index(name: 'source_context', columns: [
      [
        'source',
        30,
      ],
      'context',
    ]),
  ]);
  $locales_target = new Table(name: 'locales_target', description: 'Stores translated versions of strings.', columns: [
    Column::int(name: 'lid', description: 'Source string ID. References {locales_source}.lid.', notNull: TRUE, default: new IntValue(0)),
    Column::create(type: ColumnType::Text, name: 'translation', description: 'Translation string value in this language.', notNull: TRUE, dbSpecificExtra: [
      'mysql' => [
        'mysql_type' => 'blob',
      ],
    ]),
    Column::varcharAscii(name: 'language', description: 'Language code. References {language}.langcode.', length: 12, notNull: TRUE, default: new StringValue('')),
    Column::int(name: 'customized', description: 'Boolean indicating whether the translation is custom to this site.', notNull: TRUE, default: new IntValue(0)),
  ], primaryKey: new PrimaryKey([
    'language',
    'lid',
  ]), indexes: [
    new Index(name: 'lid', columns: [
      'lid',
    ]),
  ], foreignKeys: [
    new ForeignKey(name: 'locales_source', foreignTable: 'locales_source', columns: [
      'lid',
    ], foreignColumns: [
      'lid',
    ]),
  ]);
  $locales_location = new Table(name: 'locales_location', description: 'Location information for source strings.', columns: [
    Column::serial(name: 'lid', description: 'Unique identifier of this location.'),
    Column::int(name: 'sid', description: 'Unique identifier of this string.', notNull: TRUE),
    Column::varcharAscii(name: 'type', description: 'The location type (file, config, path, etc).', length: 50, notNull: TRUE, default: new StringValue('')),
    Column::varchar(name: 'name', description: 'Type dependent location information (file name, path, etc).', length: 255, notNull: TRUE, default: new StringValue('')),
    Column::varcharAscii(name: 'version', description: 'Version of Drupal where the location was found.', length: 20, notNull: TRUE, default: new StringValue('none')),
  ], primaryKey: new PrimaryKey([
    'lid',
  ]), indexes: [
    new Index(name: 'string_type', columns: [
      'sid',
      'type',
    ]),
    new Index(name: 'type_name', columns: [
      'type',
      'name',
    ]),
  ], foreignKeys: [
    new ForeignKey(name: 'locales_source', foreignTable: 'locales_source', columns: [
      'sid',
    ], foreignColumns: [
      'lid',
    ]),
  ]);
  $locale_file = new Table(name: 'locale_file', description: 'File import status information for interface translation files.', columns: [
    Column::varcharAscii(name: 'project', description: 'A unique short name to identify the project the file belongs to.', length: 255, notNull: TRUE, default: new StringValue('')),
    Column::varcharAscii(name: 'langcode', description: 'Language code of this translation. References {language}.langcode.', length: 12, notNull: TRUE, default: new StringValue('')),
    Column::varchar(name: 'filename', description: 'Filename of the imported file.', length: 255, notNull: TRUE, default: new StringValue('')),
    Column::varchar(name: 'version', description: 'Version tag of the imported file.', length: 128, notNull: TRUE, default: new StringValue('')),
    Column::varchar(name: 'uri', description: 'URI of the remote file, the resulting local file or the locally imported file.', length: 255, notNull: TRUE, default: new StringValue('')),
    Column::int(name: 'timestamp', description: 'Unix timestamp of the imported file.', size: ColumnSize::Big, notNull: FALSE, default: new IntValue(0)),
    Column::varcharAscii(name: 'hash', description: 'The hash of the imported file.', length: 32, notNull: TRUE, default: new StringValue('')),
    Column::int(name: 'last_checked', description: 'Unix timestamp of the last time this translation was confirmed to be the most recent release available.', size: ColumnSize::Big, notNull: FALSE, default: new IntValue(0)),
  ], primaryKey: new PrimaryKey([
    'project',
    'langcode',
  ]));
  return new Schema(type: SchemaDefinitionType::Module, name: 'locale', tables: [
    $locales_source,
    $locales_target,
    $locales_location,
    $locale_file,
  ]);
}

/**
 * Implements hook_update_last_removed().
 */
function locale_update_last_removed() : int {
  return 10300;
}

/**
 * Add a hash column to the {locale_file} table.
 */
function locale_update_11401() : void {
  $spec = [
    'type' => 'varchar_ascii',
    'length' => 32,
    'not null' => TRUE,
    'default' => '',
    'description' => 'The hash of the imported file.',
  ];
  $schema = \Drupal::database()->schema();
  if (!$schema->fieldExists('locale_file', 'hash')) {
    $schema->addField('locale_file', 'hash', $spec);
  }
}

Functions

Title Deprecated Summary
locale_install Implements hook_install().
locale_schema Implements hook_schema().
locale_uninstall Implements hook_uninstall().
locale_update_11401 Add a hash column to the {locale_file} table.
locale_update_last_removed Implements hook_update_last_removed().

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.