-
Notifications
You must be signed in to change notification settings - Fork 5
/
command.php
220 lines (198 loc) · 5.04 KB
/
command.php
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/**
* PO Merger
*
* PO Merger is a WP-CLI command that merges two PO files together to
* make translation of two similar languages faste (e.g. fr vs fr-ca)
*
* @version 1.1.0
* @author SatelliteWP <[email protected]>
*/
namespace satellitewp\po;
require_once('core/po_file_merger.php');
if ( !defined( 'WP_CLI' ) ) return;
/**
* WP-CLI PO command for satellitewp/po
*
* ## EXAMPLES
*
* # Merge for the the URL of a plugin or theme found on the WordPress repository.
* $ wp po merge fr-ca fr https://wordpress.org/plugins/wordpress-seo/
*
* # Merge for the translation URL of a project in the WordPress repository (including meta and apps).
* $ wp po merge fr-ca fr https://translate.wordpress.org/locale/fr-ca/default/wp-plugins/wordpress-seo/
*
* # Merge for the core version of WordPress.
* $ wp po merge fr-ca fr 5.0
*/
class Po_Command extends \WP_CLI_Command {
/**
* Merge of two PO files of similar languages.
*
*
* ## EXAMPLES
*
* # Merge for the the URL of a plugin or theme found on the WordPress repository.
* $ wp po merge fr-ca fr https://wordpress.org/plugins/wordpress-seo/
*
* # Merge for the translation URL of a project in the WordPress repository (including meta and apps).
* $ wp po merge fr-ca fr https://translate.wordpress.org/locale/fr-ca/default/wp-plugins/wordpress-seo/
*
* # Merge for the core version of WordPress.
* $ wp po merge fr-ca fr 5.0
*
* @when before_wp_load
*/
public function merge( $args = array(), $assoc_args = array(), $verbose = true )
{
$merger = new Merger( $args, $assoc_args );
if ( $merger->has_valid_parameters() )
{
if ( $merger->can_start() )
{
$merger->start();
}
else
{
\WP_CLI::error( $merger->get_error_message() );
}
if ( !is_null( $merger->get_warning_messages() ) )
{
foreach( $merger->get_warning_messages() as $warning_message )
{
\WP_CLI::warning( $warning_message );
}
}
}
else
{
\WP_CLI::error( $merger->get_error_message() );
}
}
/**
* Merges two PO files
*
* ## OPTIONS
*
*
* <base-file>
* : Base filename that will receive the translations.
*
* <copy-file>
* : Source where translations will be extracted.
*
*
* [--max-length=<length>]
* : Maximum string length to consider
* default: 75
*
* [--mark-copy-as-fuzzy]
* : Mark copy as fuzzy in the base file
* default: true
*
* @subcommand merge-file
* @when before_wp_load
*/
public function merge_file( $args = array(), $assoc_args = null )
{
if ( count( $args ) == 2 )
{
if ( ! is_readable( $args[0] ) )
{
\WP_CLI::error( 'The base PO file specified is not a readable file.' );
}
elseif ( ! is_readable( $args[1] ) )
{
\WP_CLI::error( 'The copy PO file specified is not a readable file.' );
}
else
{
$pfm = new Po_File_Merger( $args[0], $args[1] );
try
{
$pfm->load_files();
}
catch( InvalidArgumentException $ex )
{
\WP_CLI::error( 'PO files could not be loaded properly. Make sure they both are valid.' );
}
// Max length
if ( isset( $assoc_args['max-length'] ) )
{
$max = (int) $assoc_args['max-length'];
$pfm->set_max_length( $max );
\WP_CLI::debug( 'Length: ' . $max );
}
// Mark copy as fuzzy?
if ( isset( $assoc_args['mark-copy-as-fuzzy'] ) )
{
$value = ( $assoc_args['mark-copy-as-fuzzy'] === 'true' ? true : false );
$pfm->set_mark_copy_as_fuzzy( $value );
\WP_CLI::debug( 'Mark copy as fuzzy?: ' . ($value ? 'true' : 'false' ) );
}
$pfm->merge();
}
}
else {
\WP_CLI::error( 'You must specify both the base PO file path and the copy PO file path.' );
}
}
/**
* Get the difference two PO files
*
* ## OPTIONS
*
*
* <base-file>
* : Base filename to validate.
*
* <copy-file>
* : Source where translations will be extracted.
*
*
* [--max-length=<length>]
* : Maximum string length to consider
* default: 75
*
* @subcommand diff
* @when before_wp_load
*/
public function diff_file( $args = array(), $assoc_args = null )
{
if ( count( $args ) == 2 )
{
if ( ! is_readable( $args[0] ) )
{
\WP_CLI::error( 'The base PO file specified is not a readable file.' );
}
elseif ( ! is_readable( $args[1] ) )
{
\WP_CLI::error( 'The copy PO file specified is not a readable file.' );
}
else
{
$pfm = new Po_File_Merger( $args[0], $args[1] );
try
{
$pfm->load_files();
}
catch( InvalidArgumentException $ex )
{
\WP_CLI::error( 'PO files could not be loaded properly. Make sure they both are valid.' );
}
if ( isset( $assoc_args['max-length'] ) )
{
$max = (int) $assoc_args['max-length'];
$pfm->set_max_length( $max );
}
$pfm->diff();
}
}
else {
\WP_CLI::error( 'You must specify both the base PO file path and the copy PO file path.' );
}
}
}
// Temporary hack
function __( $text ) { return $text; }
\WP_CLI::add_command( 'po', __NAMESPACE__ . '\\Po_Command' );