Last active
December 20, 2015 00:18
-
-
Save DuaelFr/6040368 to your computer and use it in GitHub Desktop.
Drush i18ncheck
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Drush module to check if all translatable strings are safe to translate. | |
*/ | |
/** | |
* Implements hook_drush_help(). | |
*/ | |
function i18n_check_drush_help($section) { | |
switch ($section) { | |
case 'drush:i18n-check': | |
return dt('Check all translatable strings in your drupal installation to find invalid ones.'); | |
} | |
} | |
/** | |
* Implements hook_drush_command(). | |
*/ | |
function i18n_check_drush_command() { | |
$items = array(); | |
$items['i18n-check'] = array( | |
'description' => 'Check all translatable strings in your drupal installation to find invalid ones.', | |
'callback' => 'drush_i18n_check', | |
'aliases' => array('i24k'), | |
); | |
return $items; | |
} | |
/** | |
* Check translatable strings. | |
*/ | |
function drush_i18n_check() { | |
// Prepare temporary directory | |
$tmp_uri = 'temporary://i18ncheck'; | |
if (!file_prepare_directory($tmp_uri, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) { | |
drush_log('Unable to write in temporary directory.', 'error'); | |
} | |
$tmp_dir = drupal_realpath($tmp_uri); | |
// If directory is empty, clone Drupal | |
$dir_content = file_scan_directory($tmp_uri, '#^[^.]#'); | |
if (empty($dir_content)) { | |
$cmd = "git clone --branch 8.0.x http://git.drupal.org/project/drupal.git $tmp_dir"; | |
} | |
// Else update repository | |
else { | |
$cmd = "cd $tmp_dir && git pull origin 8.0.x"; | |
} | |
exec($cmd); | |
// Get PotX | |
$potx_dir = drupal_realpath(__DIR__ . '/../potx'); | |
if (!is_dir($potx_dir)) { | |
$cmd = "git clone --branch 7.x-3.x http://git.drupal.org/project/potx.git $potx_dir"; | |
} | |
else { | |
$cmd = "cd $potx_dir && git pull origin 7.x-3.x"; | |
} | |
exec($cmd); | |
exec("drush cc drush"); | |
// Run Potx | |
$cmd = "drush potx single --api=8 --folder=$tmp_dir/core"; | |
exec($cmd); | |
// Extract strings | |
$cmd = 'grep "#:" -A 1 general.pot > i24k_strings'; | |
exec($cmd); | |
// Ensure the locale_string_is_safe is usable even if the locale module is not | |
// enabled (it does not need to). | |
module_load_include('module', 'locale'); | |
// Search strings to find bad ones. | |
$f = fopen('i24k_strings', 'r'); | |
while ($file_ref = fgets($f)) { | |
$translatable = fgets($f); | |
$translatable = preg_replace('#^msgid "(.*?)"\s*$#', '$1', $translatable); | |
$translatable = str_replace('\\"', '"', $translatable); | |
fgets($f); // Skip separator line. | |
if (!empty($translatable) && !locale_string_is_safe($translatable)) { | |
echo $file_ref . "\n" . $translatable . "\n\n"; | |
} | |
} | |
// Clean files | |
$cmd = "rm -rf general.pot i24k_strings"; | |
exec($cmd); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment