Skip to content

Commit f61ab88

Browse files
authored
Fixed bug with merging existing translations
- Allows generating a `.pot` file now - Don't need to generate default translation file
1 parent 7dfabc3 commit f61ab88

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

inc/class-command.php

+29-18
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Command extends WP_CLI_Command {
2020
* [--type=<string>]
2121
* : The output file type.
2222
* ---
23-
* default: po
23+
* default: pot
2424
* options:
2525
* - csv
2626
* - csvdict
@@ -29,6 +29,7 @@ class Command extends WP_CLI_Command {
2929
* - mo
3030
* - php
3131
* - po
32+
* - pot
3233
* - jed
3334
* - xliff
3435
* - yaml
@@ -44,9 +45,6 @@ class Command extends WP_CLI_Command {
4445
* [--locales=<array>]
4546
* : A list of comma separated locale codes to generate translation ready files for.
4647
* Alternatively can be a text file containing locales on separate lines.
47-
* ---
48-
* default: en_US
49-
* ---
5048
*
5149
* [--domain=<string>]
5250
* : The text domain to extract strings for. Prepended to translation files.
@@ -78,9 +76,9 @@ public function generate( $args, $assoc_args = [] ) {
7876
$extract_to = WP_CONTENT_DIR . '/languages/plugins';
7977

8078
$assoc_args = array_merge( [
81-
'type' => 'po',
79+
'type' => 'pot',
8280
'locale' => 'en_US',
83-
'locales' => 'en_US',
81+
'locales' => '',
8482
'domain' => 'default',
8583
'exclude' => 'vendor,node_modules',
8684
'extract-from' => $extract_from,
@@ -121,25 +119,29 @@ public function generate( $args, $assoc_args = [] ) {
121119
mkdir( $assoc_args['extract-to'], 0755, true );
122120
}
123121

124-
// Add default language to locales.
122+
// Sanitise locales.
125123
$locales = array_map( 'trim', explode( ',', $assoc_args['locales'] ) );
126-
$locales = array_merge( $locales, (array) $assoc_args['locale'] );
127124
$locales = array_unique( $locales );
125+
$locales = array_filter( $locales );
128126

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+
}
130132

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 );
133139

134140
// Get the file name.
135141
$path = rtrim( $assoc_args['extract-to'], DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR . $assoc_args['domain'] . '-' . $locale;
136142

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 );
143145
}
144146
}
145147

@@ -162,6 +164,7 @@ public function generate( $args, $assoc_args = [] ) {
162164
* - mo
163165
* - php
164166
* - po
167+
* - pot
165168
* - jed
166169
* - xliff
167170
* - yaml
@@ -177,6 +180,8 @@ public function generate( $args, $assoc_args = [] ) {
177180
*
178181
* @param array $args
179182
* @param array $assoc_args
183+
*
184+
* @when after_wp_config_load
180185
*/
181186
public function convert( $args, $assoc_args = [] ) {
182187

@@ -255,6 +260,8 @@ public function convert( $args, $assoc_args = [] ) {
255260
*
256261
* @param array $args
257262
* @param array $assoc_args
263+
*
264+
* @when after_wp_config_load
258265
*/
259266
public function po2mo( $args, $assoc_args ) {
260267
$args[] = 'mo';
@@ -279,6 +286,7 @@ protected function from( string $file, string $type = '' ) {
279286

280287
switch ( $type ) {
281288
case 'po':
289+
case 'pot':
282290
$translations = Translations::fromPoFile( $file );
283291
break;
284292
case 'mo':
@@ -331,8 +339,11 @@ protected function to( Translations $translations, string $type, string $file, $
331339
] ) {
332340
// Remove extension.
333341
$file = str_replace( '.' . pathinfo( $file, PATHINFO_EXTENSION ), '', $file );
334-
335342
switch ( $type ) {
343+
case 'pot':
344+
// @TODO empty out any translated strings.
345+
$translations->toPoFile( "{$file}.pot", $file_args );
346+
break;
336347
case 'po':
337348
$this->merge( $translations, $type, "{$file}.po" );
338349
$translations->toPoFile( "{$file}.po", $file_args );

0 commit comments

Comments
 (0)