@@ -20,7 +20,7 @@ class Command extends WP_CLI_Command {
20
20
* [--type=<string>]
21
21
* : The output file type.
22
22
* ---
23
- * default: po
23
+ * default: pot
24
24
* options:
25
25
* - csv
26
26
* - csvdict
@@ -29,6 +29,7 @@ class Command extends WP_CLI_Command {
29
29
* - mo
30
30
* - php
31
31
* - po
32
+ * - pot
32
33
* - jed
33
34
* - xliff
34
35
* - yaml
@@ -44,9 +45,6 @@ class Command extends WP_CLI_Command {
44
45
* [--locales=<array>]
45
46
* : A list of comma separated locale codes to generate translation ready files for.
46
47
* Alternatively can be a text file containing locales on separate lines.
47
- * ---
48
- * default: en_US
49
- * ---
50
48
*
51
49
* [--domain=<string>]
52
50
* : The text domain to extract strings for. Prepended to translation files.
@@ -78,9 +76,9 @@ public function generate( $args, $assoc_args = [] ) {
78
76
$ extract_to = WP_CONTENT_DIR . '/languages/plugins ' ;
79
77
80
78
$ assoc_args = array_merge ( [
81
- 'type ' => 'po ' ,
79
+ 'type ' => 'pot ' ,
82
80
'locale ' => 'en_US ' ,
83
- 'locales ' => 'en_US ' ,
81
+ 'locales ' => '' ,
84
82
'domain ' => 'default ' ,
85
83
'exclude ' => 'vendor,node_modules ' ,
86
84
'extract-from ' => $ extract_from ,
@@ -121,25 +119,29 @@ public function generate( $args, $assoc_args = [] ) {
121
119
mkdir ( $ assoc_args ['extract-to ' ], 0755 , true );
122
120
}
123
121
124
- // Add default language to locales.
122
+ // Sanitise locales.
125
123
$ locales = array_map ( 'trim ' , explode ( ', ' , $ assoc_args ['locales ' ] ) );
126
- $ locales = array_merge ( $ locales , (array ) $ assoc_args ['locale ' ] );
127
124
$ locales = array_unique ( $ locales );
125
+ $ locales = array_filter ( $ locales );
128
126
129
- foreach ( $ locales as $ locale ) {
127
+ // Output .pot file, no locales needed and translations not present in this file type.
128
+ if ( $ assoc_args ['type ' ] === 'pot ' ) {
129
+ $ this ->to ( $ translations , 'pot ' , rtrim ( $ assoc_args ['extract-to ' ], DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR . $ assoc_args ['domain ' ] );
130
+ return ;
131
+ }
130
132
131
- // Update language header.
132
- $ translations ->setLanguage ( $ locale );
133
+ foreach ( $ locales as $ locale ) {
134
+ // New translations object per locale.
135
+ $ locale_translations = new Translations ();
136
+ $ locale_translations ->setLanguage ( $ locale );
137
+ $ locale_translations ->setDomain ( $ assoc_args ['domain ' ] );
138
+ $ locale_translations ->mergeWith ( $ translations );
133
139
134
140
// Get the file name.
135
141
$ path = rtrim ( $ assoc_args ['extract-to ' ], DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR . $ assoc_args ['domain ' ] . '- ' . $ locale ;
136
142
137
- // Allow generating multiple types at a time.
138
- $ types = explode ( ', ' , $ assoc_args ['type ' ] );
139
-
140
- foreach ( $ types as $ type ) {
141
- $ this ->to ( $ translations , $ type , $ path );
142
- }
143
+ // Create file for locale and merge existing file.
144
+ $ this ->to ( $ locale_translations , $ assoc_args ['type ' ], $ path );
143
145
}
144
146
}
145
147
@@ -162,6 +164,7 @@ public function generate( $args, $assoc_args = [] ) {
162
164
* - mo
163
165
* - php
164
166
* - po
167
+ * - pot
165
168
* - jed
166
169
* - xliff
167
170
* - yaml
@@ -177,6 +180,8 @@ public function generate( $args, $assoc_args = [] ) {
177
180
*
178
181
* @param array $args
179
182
* @param array $assoc_args
183
+ *
184
+ * @when after_wp_config_load
180
185
*/
181
186
public function convert ( $ args , $ assoc_args = [] ) {
182
187
@@ -255,6 +260,8 @@ public function convert( $args, $assoc_args = [] ) {
255
260
*
256
261
* @param array $args
257
262
* @param array $assoc_args
263
+ *
264
+ * @when after_wp_config_load
258
265
*/
259
266
public function po2mo ( $ args , $ assoc_args ) {
260
267
$ args [] = 'mo ' ;
@@ -279,6 +286,7 @@ protected function from( string $file, string $type = '' ) {
279
286
280
287
switch ( $ type ) {
281
288
case 'po ' :
289
+ case 'pot ' :
282
290
$ translations = Translations::fromPoFile ( $ file );
283
291
break ;
284
292
case 'mo ' :
@@ -331,8 +339,11 @@ protected function to( Translations $translations, string $type, string $file, $
331
339
] ) {
332
340
// Remove extension.
333
341
$ file = str_replace ( '. ' . pathinfo ( $ file , PATHINFO_EXTENSION ), '' , $ file );
334
-
335
342
switch ( $ type ) {
343
+ case 'pot ' :
344
+ // @TODO empty out any translated strings.
345
+ $ translations ->toPoFile ( "{$ file }.pot " , $ file_args );
346
+ break ;
336
347
case 'po ' :
337
348
$ this ->merge ( $ translations , $ type , "{$ file }.po " );
338
349
$ translations ->toPoFile ( "{$ file }.po " , $ file_args );
0 commit comments