Skip to content

Instantly share code, notes, and snippets.

@brockboland
Created May 16, 2013 15:25
Show Gist options
  • Save brockboland/5592542 to your computer and use it in GitHub Desktop.
Save brockboland/5592542 to your computer and use it in GitHub Desktop.
Drush command to load Views string translations into Built-in Interface strings that have the same source text, but lack translations.
<?php
/**
* @file
* Drush command to load the translations from Views translated strings into
* Built-in Interface counterparts
*/
/**
* Implements hook_drush_command().
*/
function views_translations_drush_command() {
$items = array();
$items['convert_views_translations'] = array(
'description' => "Load translated Views strings into Built-in Interface counterparts.",
'arguments' => array(
),
'examples' => array(
'drush convert_views_translations' => 'Load strings.',
),
'aliases' => array(),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_DATABASE,
'drush dependencies' => array('sql', 'core'),
);
return $items;
}
/**
* Actual function run by the views_translations command.
*/
function drush_views_translations_convert_views_translations() {
$strings = _drush_views_translations_get_views_strings();
if (empty($strings)) {
drush_print(dt('No translated Views strings to import.'));
}
else {
$query = db_insert('locales_target')
->fields(array('lid', 'translation', 'language'));
// Do a multi-insert by adding each string to the query
foreach ($strings AS $str) {
$query->values(array(
'lid' => $str->to_lid,
'translation' => $str->translation,
'language' => $str->language,
));
}
$query->execute();
drush_print(dt('Updated @count translation strings', array('@count' => count($strings))));
}
}
/**
* Get all data on Views strings that are translated, and correspond with
* Built-in Interface strings that are NOT translated.
*/
function _drush_views_translations_get_views_strings() {
$sql = "SELECT
views.lid from_lid,
defaults.lid to_lid,
t.translation,
t.language
FROM {locales_source} views
LEFT JOIN {locales_target} t ON views.lid = t.lid
LEFT JOIN {locales_source} defaults ON views.source = defaults.source AND views.lid != defaults.lid
LEFT JOIN {locales_target} default_t ON defaults.lid = default_t.lid AND t.language = default_t.language
WHERE
views.textgroup = 'views'
AND OCTET_LENGTH(t.translation) > 0
AND default_t.translation IS NULL;";
$result = db_query($sql);
$rows = $result->fetchAll();
return $rows;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment