491 lines
18 KiB
Plaintext
491 lines
18 KiB
Plaintext
<?php
|
|
|
|
/**
|
|
* @file
|
|
* Install, update and uninstall functions for the locale module.
|
|
*/
|
|
|
|
/**
|
|
* Implements hook_install().
|
|
*
|
|
* Enable URL language negotiation by default in order to have a basic working
|
|
* system on multilingual sites without needing any preliminary configuration.
|
|
*/
|
|
function locale_install() {
|
|
require_once DRUPAL_ROOT . '/core/includes/language.inc';
|
|
|
|
// We cannot rely on language negotiation hooks here, because locale module is
|
|
// not enabled yet. Therefore language_negotiation_set() cannot be used.
|
|
$info = locale_language_negotiation_info();
|
|
$method = $info[LANGUAGE_NEGOTIATION_URL];
|
|
$method_fields = array('callbacks', 'file', 'cache');
|
|
$negotiation = array();
|
|
|
|
// Store only the needed data.
|
|
foreach ($method_fields as $field) {
|
|
if (isset($method[$field])) {
|
|
$negotiation[LANGUAGE_NEGOTIATION_URL][$field] = $method[$field];
|
|
}
|
|
}
|
|
|
|
// Enable URL language detection for each (core) configurable language type.
|
|
foreach (language_types_get_configurable() as $type) {
|
|
variable_set("language_negotiation_$type", $negotiation);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Fill in the path prefixes and domains when enabled.
|
|
*
|
|
* Language module might change the list of languages, so we need to sync our
|
|
* configuration for domains and paths with the current language list. This
|
|
* should run every time the module is enabled.
|
|
*/
|
|
function locale_enable() {
|
|
require_once DRUPAL_ROOT . '/core/includes/locale.inc';
|
|
|
|
$languages = language_list();
|
|
$prefixes_old = locale_language_negotiation_url_prefixes();
|
|
$domains_old = locale_language_negotiation_url_domains();
|
|
|
|
$prefixes = array();
|
|
$domains = array();
|
|
foreach ($languages as $langcode => $language) {
|
|
// Keep the old prefix or fill in based on whether the language is default.
|
|
$prefixes[$langcode] = empty($prefixes_old[$langcode]) ? (empty($language->default) ? $langcode : '') : $prefixes_old[$langcode];
|
|
// Keep the old domain or fill in empty value.
|
|
$domains[$langcode] = empty($domains_old[$langcode]) ? '' : $domains_old[$langcode];
|
|
}
|
|
|
|
locale_language_negotiation_url_prefixes_save($prefixes);
|
|
locale_language_negotiation_url_domains_save($domains);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_uninstall().
|
|
*/
|
|
function locale_uninstall() {
|
|
// Delete all JavaScript translation files.
|
|
$locale_js_directory = 'public://' . variable_get('locale_js_directory', 'languages');
|
|
|
|
if (is_dir($locale_js_directory)) {
|
|
$locale_javascripts = variable_get('locale_translation_javascript', array());
|
|
foreach ($locale_javascripts as $langcode => $file_suffix) {
|
|
if (!empty($file_suffix)) {
|
|
file_unmanaged_delete($locale_js_directory . '/' . $langcode . '_' . $file_suffix . '.js');
|
|
}
|
|
}
|
|
// Delete the JavaScript translations directory if empty.
|
|
if (!file_scan_directory($locale_js_directory, '/.*/')) {
|
|
drupal_rmdir($locale_js_directory);
|
|
}
|
|
}
|
|
|
|
// Clear variables.
|
|
variable_del('language_types');
|
|
variable_del('locale_language_negotiation_url_part');
|
|
variable_del('locale_language_negotiation_url_prefixes');
|
|
variable_del('locale_language_negotiation_url_domains');
|
|
variable_del('locale_language_negotiation_session_param');
|
|
variable_del('language_content_type_default');
|
|
variable_del('language_content_type_negotiation');
|
|
variable_del('locale_cache_strings');
|
|
variable_del('locale_js_directory');
|
|
variable_del('javascript_parsed');
|
|
variable_del('locale_field_language_fallback');
|
|
variable_del('locale_cache_length');
|
|
variable_del('locale_translation_plurals');
|
|
variable_del('locale_translation_javascript');
|
|
|
|
foreach (language_types_get_all() as $type) {
|
|
variable_del("language_negotiation_$type");
|
|
variable_del("locale_language_negotiation_methods_weight_$type");
|
|
}
|
|
|
|
// Remove all node type language variables. Node module might have been
|
|
// enabled, but may be disabled, so use a wildcard delete.
|
|
db_delete('variable')
|
|
->condition('name', db_like('language_content_type_') . '%', 'LIKE')
|
|
->execute();
|
|
}
|
|
|
|
/**
|
|
* Implements hook_schema().
|
|
*/
|
|
function locale_schema() {
|
|
$schema['locales_source'] = array(
|
|
'description' => 'List of English source strings.',
|
|
'fields' => array(
|
|
'lid' => array(
|
|
'type' => 'serial',
|
|
'not null' => TRUE,
|
|
'description' => 'Unique identifier of this string.',
|
|
),
|
|
'location' => array(
|
|
'type' => 'text',
|
|
'not null' => FALSE,
|
|
'size' => 'big',
|
|
'description' => 'Drupal path in case of online discovered translations or file path in case of imported strings.',
|
|
),
|
|
'source' => array(
|
|
'type' => 'text',
|
|
'mysql_type' => 'blob',
|
|
'not null' => TRUE,
|
|
'description' => 'The original string in English.',
|
|
),
|
|
'context' => array(
|
|
'type' => 'varchar',
|
|
'length' => 255,
|
|
'not null' => TRUE,
|
|
'default' => '',
|
|
'description' => 'The context this string applies to.',
|
|
),
|
|
'version' => array(
|
|
'type' => 'varchar',
|
|
'length' => 20,
|
|
'not null' => TRUE,
|
|
'default' => 'none',
|
|
'description' => 'Version of Drupal, where the string was last used (for locales optimization).',
|
|
),
|
|
),
|
|
'primary key' => array('lid'),
|
|
'indexes' => array(
|
|
'source_context' => array(array('source', 30), 'context'),
|
|
),
|
|
);
|
|
|
|
$schema['locales_target'] = array(
|
|
'description' => 'Stores translated versions of strings.',
|
|
'fields' => array(
|
|
'lid' => array(
|
|
'type' => 'int',
|
|
'not null' => TRUE,
|
|
'default' => 0,
|
|
'description' => 'Source string ID. References {locales_source}.lid.',
|
|
),
|
|
'translation' => array(
|
|
'type' => 'text',
|
|
'mysql_type' => 'blob',
|
|
'not null' => TRUE,
|
|
'description' => 'Translation string value in this language.',
|
|
),
|
|
'language' => array(
|
|
'type' => 'varchar',
|
|
'length' => 12,
|
|
'not null' => TRUE,
|
|
'default' => '',
|
|
'description' => 'Language code. References {language}.langcode.',
|
|
),
|
|
),
|
|
'primary key' => array('language', 'lid'),
|
|
'foreign keys' => array(
|
|
'locales_source' => array(
|
|
'table' => 'locales_source',
|
|
'columns' => array('lid' => 'lid'),
|
|
),
|
|
),
|
|
'indexes' => array(
|
|
'lid' => array('lid'),
|
|
),
|
|
);
|
|
|
|
return $schema;
|
|
}
|
|
|
|
/**
|
|
* @addtogroup updates-7.x-to-8.x
|
|
* @{
|
|
*/
|
|
|
|
/**
|
|
* Drop textgroup support.
|
|
*
|
|
* Update assumes i18n migrated this data before the update happened. Core
|
|
* never used textgroups for anything, so it is not our job to find place
|
|
* for the data elsewhere.
|
|
*/
|
|
function locale_update_8000() {
|
|
$subquery = db_select('locales_source', 'ls')
|
|
->fields('ls', array('lid'))
|
|
->condition('ls.textgroup', 'default', '<>');
|
|
db_delete('locales_target')
|
|
->condition('lid', $subquery, 'IN')
|
|
->execute();
|
|
db_delete('locales_source')
|
|
->condition('textgroup', 'default', '<>')
|
|
->execute();
|
|
db_drop_field('locales_source', 'textgroup');
|
|
}
|
|
|
|
/**
|
|
* Language type 'language' renamed to 'language_interface'.
|
|
*/
|
|
function locale_update_8001() {
|
|
// Only change language_types if we had this setting saved. Keep order
|
|
// of types because that is significant for value dependency.
|
|
$types = variable_get('language_types', NULL);
|
|
if (!empty($types) && isset($types['language'])) {
|
|
$new_types = array();
|
|
foreach ($types as $key => $type) {
|
|
$new_types[$key == 'language' ? 'language_interface' : $key] = $type;
|
|
}
|
|
variable_set('language_types', $new_types);
|
|
}
|
|
|
|
// Rename language_negotiation_language setting if exists.
|
|
$setting = variable_get('language_negotiation_language', NULL);
|
|
if ($setting !== NULL) {
|
|
variable_set('language_negotiation_language_interface', $setting);
|
|
variable_del('language_negotiation_language');
|
|
}
|
|
|
|
// Rename locale_language_providers_weight_language setting if exists.
|
|
$weight = variable_get('locale_language_providers_weight_language', NULL);
|
|
if ($weight !== NULL) {
|
|
variable_set('locale_language_providers_weight_language_interface', $weight);
|
|
variable_del('locale_language_providers_weight_language');
|
|
}
|
|
|
|
// Update block data in all core block related tables. Contributed modules
|
|
// storing data for blocks will need to update for themselves.
|
|
$block_tables = array('block', 'block_node_type', 'block_role');
|
|
foreach ($block_tables as $table) {
|
|
// Perform the update only if the language switcher block data is available.
|
|
$block_data = db_query_range('SELECT 1 FROM {' . $table . '} WHERE delta = :delta AND module = :module', 0, 1, array(':delta' => 'language', ':module' => 'locale'))
|
|
->fetchField();
|
|
if ($block_data) {
|
|
// If block information is rebuilt before performing the update, we might
|
|
// already have data for the new delta. In this case we need to remove it
|
|
// to avoid integrity constraint violation errors.
|
|
db_delete($table)
|
|
->condition('delta', 'language_interface')
|
|
->condition('module', 'locale')
|
|
->execute();
|
|
db_update($table)
|
|
->fields(array(
|
|
'delta' => 'language_interface',
|
|
))
|
|
->condition('delta', 'language')
|
|
->condition('module', 'locale')
|
|
->execute();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Removes duplicates in {locales_source}.
|
|
*
|
|
* Aggressively removes duplicates that has not already been removed by
|
|
* locale_update_7004() in Drupal 7.x.
|
|
*/
|
|
function locale_update_8002() {
|
|
// Look up all duplicates.
|
|
$results = db_query("SELECT source, context FROM {locales_source} GROUP BY source, context HAVING COUNT(*) > 1");
|
|
|
|
// For each set of duplicates, select one row that should survive, preferably
|
|
// one that has been translated, and delete the rest.
|
|
foreach ($results as $row) {
|
|
$lid = db_query("SELECT s.lid FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid WHERE s.source = :source AND s.context = :context ORDER BY translation IS NULL", array(
|
|
':source' => $row->source,
|
|
':context' => $row->context,
|
|
))->fetchField();
|
|
db_delete('locales_source')
|
|
->condition('source', $row->source)
|
|
->condition('context', $row->context)
|
|
->condition('lid', $lid, '<>')
|
|
->execute();
|
|
}
|
|
|
|
// Finally remove any rows from {locales_target} that refer to non-existing
|
|
// lids.
|
|
$subquery = db_select('locales_source', 't')->fields('t', array('lid'));
|
|
db_delete('locales_target')->condition('lid', $subquery, 'NOT IN')->execute();
|
|
}
|
|
|
|
/**
|
|
* Converts language domains to new format.
|
|
*/
|
|
function locale_update_8003() {
|
|
$message = '';
|
|
$domains = variable_get('locale_language_negotiation_url_domains', array());
|
|
// $used_domains keeps track of the domain names in use.
|
|
$used_domains = array();
|
|
foreach ($domains as $langcode => $domain) {
|
|
// Domain names can not contain protocol and/or ports.
|
|
if (!empty($domain)) {
|
|
$host = 'http://' . str_replace(array('http://', 'https://'), '', $domain);
|
|
if (parse_url($host, PHP_URL_HOST) != $domain) {
|
|
$domains[$langcode] = parse_url($host, PHP_URL_HOST);
|
|
}
|
|
if (array_key_exists($domain, $used_domains)) {
|
|
if (empty($message)) {
|
|
$message = 'Some languages are using the same domain name, you should change these domain names at ' . l('URL language detection configuration', 'admin/config/regional/language/configure/url' . '.');
|
|
}
|
|
}
|
|
else {
|
|
$used_domains[$domain] = $domain;
|
|
}
|
|
}
|
|
}
|
|
variable_set('locale_language_negotiation_url_domains', $domains);
|
|
|
|
if (!empty($message)) {
|
|
return $message;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Rename language providers to language negotiation methods.
|
|
*/
|
|
function locale_update_8004() {
|
|
$types = variable_get('language_types', NULL);
|
|
if (!empty($types)) {
|
|
foreach ($types as $type => $configurable) {
|
|
// Rename the negotiation and language switch callback keys.
|
|
$negotiation = variable_get('language_negotiation_' . $type, NULL);
|
|
if (!empty($negotiation)) {
|
|
foreach ($negotiation as $method_id => &$method) {
|
|
$method['callbacks']['negotiation'] = $method['callbacks']['language'];
|
|
unset($method['callbacks']['language']);
|
|
if (isset($method['callbacks']['switcher'])) {
|
|
$method['callbacks']['language_switch'] = $method['callbacks']['switcher'];
|
|
unset($method['callbacks']['switcher']);
|
|
}
|
|
}
|
|
variable_set('language_negotiation_' . $type, $negotiation);
|
|
}
|
|
|
|
// Rename the language negotiation methods weight variable.
|
|
$weight = variable_get('locale_language_providers_weight_' . $type , NULL);
|
|
if ($weight !== NULL) {
|
|
variable_set('locale_language_negotiation_methods_weight_' . $type , $weight);
|
|
variable_del('locale_language_providers_weight_' . $type);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update plural interface translations to new format.
|
|
*
|
|
* See http://drupal.org/node/532512#comment-5679184 for the details of the
|
|
* structures handled in this update.
|
|
*/
|
|
function locale_update_8005() {
|
|
// Collect all LIDs that are sources to plural variants.
|
|
$results = db_query("SELECT lid, plid FROM {locales_target} WHERE plural <> 0");
|
|
$plural_lids = array();
|
|
foreach ($results as $row) {
|
|
// Need to collect both LID and PLID. The LID for the first (singular)
|
|
// string can only be retrieved from the first plural's PLID given no
|
|
// other indication. The last plural variant is never referenced, so we
|
|
// need to store the LID directly for that. We never know whether we are
|
|
// on the last plural though, so we always remember LID too.
|
|
$plural_lids[] = $row->lid;
|
|
$plural_lids[] = $row->plid;
|
|
}
|
|
$plural_lids = array_unique($plural_lids);
|
|
|
|
if (!empty($plural_lids)) {
|
|
// Look up all translations for these source strings. Ordering by language
|
|
// will group the strings by language, the 'plid' order will get the
|
|
// strings in singular/plural order and 'plural' will get them in precise
|
|
// sequential order needed.
|
|
$results = db_query("SELECT s.lid, s.source, t.translation, t.plid, t.plural, t.language FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid WHERE s.lid IN (:lids) ORDER BY t.language, t.plid, t.plural", array(':lids' => $plural_lids));
|
|
|
|
// Collect the strings into an array and combine values as we go.
|
|
$strings = array();
|
|
$parents_to_sources = array();
|
|
$remove_lids = array();
|
|
foreach ($results as $child) {
|
|
$strings[$child->language][$child->lid] = array(
|
|
'source' => array($child->source),
|
|
'translation' => array($child->translation),
|
|
);
|
|
|
|
if (empty($child->plid)) {
|
|
// Non-children strings point to themselves as parents. This makes it
|
|
// easy to look up the utmost parents for any plurals.
|
|
$parents_to_sources[$child->lid] = $child->lid;
|
|
}
|
|
else {
|
|
// Children strings point to their utmost parents. Because we get data
|
|
// in PLID order, we can ensure that all previous parents have data now,
|
|
// so we can just copy the parent's data about their parent, etc.
|
|
$parents_to_sources[$child->lid] = $parents_to_sources[$child->plid];
|
|
|
|
// Append translation to the utmost parent's translation string.
|
|
$utmost_parent = &$strings[$child->language][$parents_to_sources[$child->plid]];
|
|
// Drop the Drupal-specific numbering scheme from the end of plural
|
|
// formulas.
|
|
$utmost_parent['translation'][] = str_replace('@count[' . $child->plural .']', '@count', $child->translation);
|
|
if (count($utmost_parent['source']) < 2) {
|
|
// Append source to the utmost parent's source string only if it is the
|
|
// plural variant. Further Drupal specific plural variants are not to be
|
|
// retained for source strings.
|
|
$utmost_parent['source'][] = $child->source;
|
|
}
|
|
|
|
// All plural variant LIDs are to be removed with their translations.
|
|
// Only the singular LIDs will be kept.
|
|
$remove_lids[] = $child->lid;
|
|
}
|
|
}
|
|
|
|
// Do updates for all source strings and all translations.
|
|
$updated_sources = array();
|
|
foreach ($strings as $langcode => $translations) {
|
|
foreach($translations as $lid => $translation) {
|
|
if (!in_array($lid, $updated_sources)) {
|
|
// Only update source string if not yet updated. We merged these within
|
|
// the translation lookups because plural information was only avilable
|
|
// with the translation, but we don't need to save it again for every
|
|
// language.
|
|
db_update('locales_source')
|
|
->fields(array(
|
|
'source' => implode(LOCALE_PLURAL_DELIMITER, $translation['source']),
|
|
))
|
|
->condition('lid', $lid)
|
|
->execute();
|
|
$updated_sources[] = $lid;
|
|
}
|
|
db_update('locales_target')
|
|
->fields(array(
|
|
'translation' => implode(LOCALE_PLURAL_DELIMITER, $translation['translation']),
|
|
))
|
|
->condition('lid', $lid)
|
|
->condition('language', $langcode)
|
|
->execute();
|
|
}
|
|
}
|
|
|
|
// Remove all plural LIDs from source and target. only keep those which were
|
|
// originally used for the singular strings (now updated to contain the
|
|
// serialized version of plurals).
|
|
$remove_lids = array_unique($remove_lids);
|
|
db_delete('locales_source')
|
|
->condition('lid', $remove_lids, 'IN')
|
|
->execute();
|
|
db_delete('locales_target')
|
|
->condition('lid', $remove_lids, 'IN')
|
|
->execute();
|
|
}
|
|
|
|
// Drop the primary key because it contains 'plural'.
|
|
db_drop_primary_key('locales_target');
|
|
|
|
// Remove the 'plid' and 'plural' columns and indexes.
|
|
db_drop_index('locales_target', 'plid');
|
|
db_drop_field('locales_target', 'plid');
|
|
db_drop_index('locales_target', 'plural');
|
|
db_drop_field('locales_target', 'plural');
|
|
|
|
// Add back a primary key without 'plural'.
|
|
db_add_primary_key('locales_target', array('language', 'lid'));
|
|
}
|
|
|
|
/**
|
|
* @} End of "addtogroup updates-7.x-to-8.x"
|
|
* The next series of updates should start at 9000.
|
|
*/
|