Skip to content

Instantly share code, notes, and snippets.

@vectoroc
Created December 13, 2012 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vectoroc/4276431 to your computer and use it in GitHub Desktop.
Save vectoroc/4276431 to your computer and use it in GitHub Desktop.
potx-6.x drush integration / with help of this plugin you can keep in sync your codebase and language translations
<?php
function potx_drush_command() {
$items['po-import'] = array(
'aliases' => array('poi'),
'description' => "Update project translations from po-files.",
'examples' => array(
'drush po-import' => 'Re-import all po-files from enabled modules',
'drush po-import mymodule1 mymodule2' => 'Re-import po-files from specified modules',
),
'arguments' => array(
'module1 module2 ...' => 'List of modules where to search po-files to be imported.',
),
'drupal dependencies' => array('locale'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_DATABASE,
'core' => array('6'),
);
$items['po-update'] = array(
'aliases' => array('pou'),
'description' => "Update po-files using potx module.",
'examples' => array(
'drush po-update mymodule' => 'Update po-file of mymodule.',
'drush po-update --custom' => 'Update po-files of all custom modules.',
),
'drupal dependencies' => array('potx'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_DATABASE,
'core' => array('6'),
'options' => array(
'lang' => 'Default value - default site language.',
'custom' => 'If defined then this command will be applied to all modules within custom_path folder.',
'custom_path' => 'Folder to look for custom modules. Default - sites/all/modules/custom',
),
);
return $items;
}
function drush_potx_po_import() {
$drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT');
$module_list = func_num_args() ? func_get_args() : array_keys(module_list());
// Set a batch to process all pending tasks.
include_once $drupal_root . '/includes/locale.inc';
$batch = locale_batch_by_component($module_list, '_drush_potx_po_import_batch_finished');
if (!$batch) {
drush_set_error('DRUSH_POTX', 'Unable to prepare batch by given modules list.');
return;
}
foreach ($batch['operations'] as $i => $operation) {
$operation[0] = '_drush_potx_po_import_batch_op';
$batch['operations'][$i] = $operation;
}
batch_set($batch);
drush_backend_batch_process();
}
function _drush_potx_po_import_batch_op($filepath, &$context) {
// The filename is either {langcode}.po or {prefix}.{langcode}.po, so
// we can extract the language code to use for the import from the end.
if (preg_match('!(/|\.)([^\./]+)\.po$!', $filepath, $langcode)) {
$file = (object) array('filename' => basename($filepath), 'filepath' => $filepath);
_locale_import_read_po('db-store', $file, LOCALE_IMPORT_OVERWRITE, $langcode[2]);
$context['results'][] = $filepath;
$context['message'] = t('Importing @file', array('@file' => $filepath));
}
}
function _drush_potx_po_import_batch_finished($success, $results) {
drupal_set_message(t('Reimport has been finished'));
cache_clear_all('locale:', 'cache', TRUE);
}
function drush_potx_po_update() {
$langcode = drush_get_option('lang', language_default('language'));
$modules = func_get_args();
if (drush_get_option('custom', 0)) {
$modules = _drush_potx_po_update_custom_modules();
}
foreach ($modules as $module_name) {
if (_drush_potx_update_module_translations($module_name, $langcode)) {
drush_log(dt('@module_name translations has been updated.', array('@module_name' => $module_name)), 'ok');
}
}
}
function _drush_potx_po_update_custom_modules() {
$path = rtrim(drush_get_option('custom_path', 'sites/all/modules/custom'), '/') . '/';
$res = drush_db_select('system', array('name'), 'filename LIKE :path', array(':path' => $path . '%'));
$modules = array();
while ($module = drush_db_result($res)) {
$modules[] = $module;
}
return $modules;
}
function _drush_potx_update_module_translations($module_name, $langcode) {
_drush_potx_init();
if (_drush_potx_scan_module($module_name)) {
_drush_potx_build_module($module_name, $langcode);
return _drush_potx_update_module($module_name, $langcode);
}
else {
return FALSE;
}
}
function _drush_potx_init() {
module_load_include('inc', 'potx');
potx_status('set', POTX_STATUS_MESSAGE);
$potx_globals = array(
'_potx_store',
'_potx_tokens',
'_potx_lookup',
'_potx_versions',
'_potx_strings',
'_potx_install'
);
foreach ($potx_globals as $varname) {
unset($GLOBALS[$varname]);
}
}
function _drush_potx_module_path($module_name) {
$filename = drush_db_result(drush_db_select('system', array('filename'), 'name = :name', array(':name' => $module_name)));
if ($filename) {
return dirname($filename);
}
}
function _drush_potx_scan_module($module_name) {
if ($module_path = _drush_potx_module_path($module_name)) {
$files = _potx_explore_dir($module_path . '/', '*', POTX_API_CURRENT, TRUE);
foreach ($files as $filename) {
// TODO: exclude submodules
drush_log(dt('Processing @file.', array('@file' => $filename)));
_potx_process_file($filename);
}
return TRUE;
}
else {
drush_log(dt('Not found module @module_name.', array('@module_name' => $module_name)), 'warning');
return FALSE;
}
}
function _drush_potx_build_module($module_name, $langcode) {
$build_mode = POTX_BUILD_SINGLE;
$string_mode = POTX_STRING_RUNTIME;
$force_name = $module_name;
$template_export_langcode = $translation_export_langcode = $langcode;
_potx_build_files(
$string_mode,
$build_mode,
$force_name,
'_potx_save_string',
'_potx_save_version',
'_potx_get_header',
$template_export_langcode,
$translation_export_langcode
);
}
function _drush_potx_update_module($module_name, $langcode) {
global $_potx_store;
$contents = $_potx_store[$module_name];
$output = str_replace('--VERSIONS--', ' TODO--REVISION--', $contents['header'] . $contents['strings']);
$translations_path = _drush_potx_module_path($module_name) .'/translations';
if (file_check_directory($translations_path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
$filename = "$translations_path/$module_name.$langcode.po";
file_put_contents($filename, $output);
return TRUE;
}
else {
drush_log(dt("@dir is inaccessible", array('@dir' => $translations_path)), 'error');
return FALSE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment