Skip to content

Commit

Permalink
Fixed bug with merging existing translations
Browse files Browse the repository at this point in the history
- Allows generating a `.pot` file now
- Don't need to generate default translation file
  • Loading branch information
roborourke authored Jun 25, 2018
1 parent 7dfabc3 commit f61ab88
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions inc/class-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Command extends WP_CLI_Command {
* [--type=<string>]
* : The output file type.
* ---
* default: po
* default: pot
* options:
* - csv
* - csvdict
Expand All @@ -29,6 +29,7 @@ class Command extends WP_CLI_Command {
* - mo
* - php
* - po
* - pot
* - jed
* - xliff
* - yaml
Expand All @@ -44,9 +45,6 @@ class Command extends WP_CLI_Command {
* [--locales=<array>]
* : A list of comma separated locale codes to generate translation ready files for.
* Alternatively can be a text file containing locales on separate lines.
* ---
* default: en_US
* ---
*
* [--domain=<string>]
* : The text domain to extract strings for. Prepended to translation files.
Expand Down Expand Up @@ -78,9 +76,9 @@ public function generate( $args, $assoc_args = [] ) {
$extract_to = WP_CONTENT_DIR . '/languages/plugins';

$assoc_args = array_merge( [
'type' => 'po',
'type' => 'pot',
'locale' => 'en_US',
'locales' => 'en_US',
'locales' => '',
'domain' => 'default',
'exclude' => 'vendor,node_modules',
'extract-from' => $extract_from,
Expand Down Expand Up @@ -121,25 +119,29 @@ public function generate( $args, $assoc_args = [] ) {
mkdir( $assoc_args['extract-to'], 0755, true );
}

// Add default language to locales.
// Sanitise locales.
$locales = array_map( 'trim', explode( ',', $assoc_args['locales'] ) );
$locales = array_merge( $locales, (array) $assoc_args['locale'] );
$locales = array_unique( $locales );
$locales = array_filter( $locales );

foreach ( $locales as $locale ) {
// Output .pot file, no locales needed and translations not present in this file type.
if ( $assoc_args['type'] === 'pot' ) {
$this->to( $translations, 'pot', rtrim( $assoc_args['extract-to'], DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR . $assoc_args['domain'] );
return;
}

// Update language header.
$translations->setLanguage( $locale );
foreach ( $locales as $locale ) {
// New translations object per locale.
$locale_translations = new Translations();
$locale_translations->setLanguage( $locale );
$locale_translations->setDomain( $assoc_args['domain'] );
$locale_translations->mergeWith( $translations );

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

// Allow generating multiple types at a time.
$types = explode( ',', $assoc_args['type'] );

foreach ( $types as $type ) {
$this->to( $translations, $type, $path );
}
// Create file for locale and merge existing file.
$this->to( $locale_translations, $assoc_args['type'], $path );
}
}

Expand All @@ -162,6 +164,7 @@ public function generate( $args, $assoc_args = [] ) {
* - mo
* - php
* - po
* - pot
* - jed
* - xliff
* - yaml
Expand All @@ -177,6 +180,8 @@ public function generate( $args, $assoc_args = [] ) {
*
* @param array $args
* @param array $assoc_args
*
* @when after_wp_config_load
*/
public function convert( $args, $assoc_args = [] ) {

Expand Down Expand Up @@ -255,6 +260,8 @@ public function convert( $args, $assoc_args = [] ) {
*
* @param array $args
* @param array $assoc_args
*
* @when after_wp_config_load
*/
public function po2mo( $args, $assoc_args ) {
$args[] = 'mo';
Expand All @@ -279,6 +286,7 @@ protected function from( string $file, string $type = '' ) {

switch ( $type ) {
case 'po':
case 'pot':
$translations = Translations::fromPoFile( $file );
break;
case 'mo':
Expand Down Expand Up @@ -331,8 +339,11 @@ protected function to( Translations $translations, string $type, string $file, $
] ) {
// Remove extension.
$file = str_replace( '.' . pathinfo( $file, PATHINFO_EXTENSION ), '', $file );

switch ( $type ) {
case 'pot':
// @TODO empty out any translated strings.
$translations->toPoFile( "{$file}.pot", $file_args );
break;
case 'po':
$this->merge( $translations, $type, "{$file}.po" );
$translations->toPoFile( "{$file}.po", $file_args );
Expand Down

0 comments on commit f61ab88

Please sign in to comment.