-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpo_utils.drush.inc
98 lines (86 loc) · 2.78 KB
/
po_utils.drush.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
* Implements hook_drush_command().
*/
function po_utils_drush_command() {
$items['po-import'] = array(
'description' => dt('Import all translation files for a given language.'),
'arguments' => array(
'langcode' => dt('The langcode of the language in which the strings will be imported.'),
),
'options' => array(
'replace' => array(
'description' => dt('Replace existing translations.'),
'value' => 'optional',
),
'custom-only' => array(
'description' => dt('Import only custom modules translations.'),
'value' => 'optional',
),
),
'example' => array(
'drush po-import fr --replace' => 'Import all French translations and replace existing ones.'
),
);
return $items;
}
/**
* Implements drush_COMMANDFILE_COMMANDNAME().
*/
function drush_po_utils_po_import() {
$args = func_get_args();
if (count($args) < 1) {
drush_set_error(dt('Usage: drush po-import <langcode> [--replace]'));
return;
}
// Get arguments and options
$langcode = $args[0];
$replace = drush_get_option('replace');
$customOnly = drush_get_option('custom-only');
$mode = isset($replace) ? LOCALE_IMPORT_OVERWRITE : LOCALE_IMPORT_KEEP;
// Get all .po files
$profile = drupal_get_profile();
$path = $customOnly ? 'profiles/' . $profile . '/modules/custom' : 'profiles/' . $profile . '/modules';
if (file_exists($path)) {
$directory = new RecursiveDirectoryIterator($path);
$iterator = new RecursiveIteratorIterator($directory);
$file_paths = array();
foreach($iterator as $file) {
if (substr($file, strrpos($file, '/') + 1) == $langcode . '.po') {
$file_paths[] = $file->getPathName();
}
}
}
// Add profile directory
$profile_path = 'profiles/' . $profile . '/translations/' . $langcode . ".po" ;
if (file_exists($profile_path)) {
$file_paths[] = $profile_path;
}
// Add Drupal core translations
if(!$customOnly) {
$core_path = "translations/" . $langcode . ".po";
if (file_exists($core_path)) {
$file_paths[] = $core_path;
}
}
// Display an error if no .po file is found
if (empty($file_paths)) {
drush_log(dt('No available .po file for this language.'));
}
// Import language files
else {
foreach ($file_paths as $file_path) {
// Construct a fake file object
$file = new stdClass();
$file->uid = 1;
$file->status = 0;
$file->filename = trim(drupal_basename($file_path), '.');
$file->uri = $file_path;
// Now import strings
if ($return = _locale_import_po($file, $langcode, $mode, 'default') == FALSE) {
$variables = array('%filename' => $file->filename);
drush_log(dt('The translation import of %filename failed.', $variables), 'error');
}
}
}
}