forked from kgaut/drupal-potx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpotx.drush.inc
170 lines (154 loc) · 5.93 KB
/
potx.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* @file
* Translation template extractor module drush integration.
*/
/**
* Implements hook_drush_command().
*
* @see drush_parse_command() for a list of recognized keys.
*
* @return
* An associative array describing our command.
*/
function potx_drush_command() {
$items['potx'] = array(
'callback' => 'potx_drush_extract',
'description' => 'Extract translatable strings from Drupal source code.',
'arguments' => array(
'mode' => 'potx output mode e.g. single multiple core',
),
'options' => array(
'modules' => 'Comma delimited list of modules to extract translatable strings from.',
'files' => 'Comma delimited list of files to extract translatable strings from.',
'folder' => 'Folder to begin translation extraction in. When no other option is set this defaults to current directory.',
'api' => 'Drupal core version to use for extraction settings.',
'language' => 'language to include in the po file',
'destination' => 'The file name of the exported file.',
),
'examples' => array(
'potx single' => 'Extract translatable strings from applicable files in current directory and write to single output file',
'potx multiple --modules=example' => 'Extract translatable strings from applicable files of example module and write to module-specific output file.',
'potx --files=sites/all/modules/example/example.module' => 'Extract translatable strings from example.module and write to single output file.',
'potx single --api=8 --folder=projects/drupal/8' => 'Extract strings from folder projects/drupal/8 using API version 8.',
),
'bootstrap' => DRUSH_BOOTSTRAP_NONE,
);
return $items;
}
/**
* Implementation of hook_drush_help().
*
* This function is called whenever a drush user calls
* 'drush help potx'.
*
* @param
* A string with the help section (prepended with 'drush:').
*
* @return
* A string with the help text for our command.
*/
function potx_drush_help($section) {
if ($section == 'drush:potx') {
$help = dt('Generates translation templates from the given Drupal source code in the current working directory.');
$help .= "\n\n". dt('Possible potx modes are:');
$help .= "\n". dt(' single Single file output mode, every file folded into the single output file (default).');
$help .= "\n". dt(' multiple Multiple file output mode, .info files folded into module .pot files.');
$help .= "\n". dt(' core Drupal core output mode, .info files folded into general.pot.');
$help .= "\n\n". dt('If no files are specified, potx will autodiscover files from the current working directory. You can specify concrete files to look at to limit the scope of the operation.');
return $help;
}
}
/**
* Drush command callback.
*/
function potx_drush_extract($mode = NULL) {
if (\Drupal::hasService('potx.commands')) {
\Drupal::service('potx.commands')->potx($mode, [
'modules' => drush_get_option('modules'),
'files' => drush_get_option('files'),
'folder' => drush_get_option('folder'),
'api' => drush_get_option('api'),
'language' => drush_get_option('language'),
]);
}
else {
// Include library.
include_once __DIR__ . '/potx.inc';
include_once __DIR__ . '/potx.local.inc';
$files = [];
$build_mode = POTX_BUILD_SINGLE;
if (!is_null($mode) && in_array($mode, ['core', 'multiple', 'single'])) {
// First argument could be any of the mode names.
$build_mode = constant('POTX_BUILD_' . strtoupper($mode));
}
// Silence error message reporting. Messages will be reported by at the end.
potx_status('set', POTX_STATUS_SILENT);
// Get Drush options.
$modules_option = drush_get_option('modules');
$files_option = drush_get_option('files');
$folder_option = drush_get_option('folder');
$api_option = drush_get_option('api');
$language_option = drush_get_option('language');
$destination = drush_get_option('destination');
if (empty($api_option) || !in_array($api_option, [5, 6, 7, 8])) {
$api_option = POTX_API_CURRENT;
}
potx_local_init($folder_option);
if (!empty($modules_option)) {
$modules = explode(',', $modules_option);
foreach ($modules as $module) {
$files = array_merge($files, _potx_explore_dir(drupal_get_path('module', $module) . '/', '*', $api_option, TRUE));
}
}
elseif (!empty($files_option)) {
$files = explode(',', $files_option);
}
elseif (!empty($folder_option)) {
$files = _potx_explore_dir($folder_option, '*', $api_option, TRUE);
}
else {
// No file list provided so autodiscover files in current directory.
$files = _potx_explore_dir(drush_cwd() . '/', '*', $api_option, TRUE);
}
foreach ($files as $file) {
drush_print(dt("Processing $file..."));
_potx_process_file($file, 0, '_potx_save_string', '_potx_save_version', $api_option);
}
potx_finish_processing('_potx_save_string', $api_option);
_potx_build_files(
POTX_STRING_RUNTIME,
$build_mode,
$destination,
'_potx_save_string',
'_potx_save_version',
'_potx_get_header',
$language_option,
$language_option
);
_potx_build_files(POTX_STRING_INSTALLER, POTX_BUILD_SINGLE, 'installer');
_potx_write_files();
drush_print("");
drush_print(dt("Stats"));
$header = [
'files' => dt('Files'),
'strings' => dt('Strings'),
'warnings' => dt('Warnings'),
];
$rows = [array_values($header)];
// Get errors, if any.
$errors = potx_status('get');
// Get saved strings.
$strings = _potx_save_string(NULL, NULL, NULL, 0, POTX_STRING_RUNTIME);
$rows[] = [count($files), count($strings), count($errors)];
drush_print_table($rows, TRUE);
if (!empty($errors)) {
drush_print(dt("Errors"));
foreach ($errors as $error) {
drush_set_error($error);
}
}
drush_print("");
drush_print(dt("Done"));
}
}