Skip to content

Commit fdf5181

Browse files
author
sdnunca
committed
Add destination file support to MakePoCommand
1 parent e84691f commit fdf5181

File tree

3 files changed

+109
-6
lines changed

3 files changed

+109
-6
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ wp i18n make-mo <source> [<destination>]
212212
Path to an existing PO file or a directory containing multiple PO files.
213213

214214
[<destination>]
215-
Path to the destination directory for the resulting MO files. Defaults to the source directory.
215+
Path to the destination file or directory for the resulting MO files. Defaults to the source directory.
216216

217217
**EXAMPLES**
218218

@@ -222,6 +222,9 @@ wp i18n make-mo <source> [<destination>]
222222
# Create a MO file from a single PO file in a specific directory.
223223
$ wp i18n make-mo example-plugin-de_DE.po languages
224224

225+
# Create a MO file from a single PO file to a specific file destination
226+
$ wp i18n make-mo example-plugin-de_DE.po languages/bar.mo
227+
225228

226229

227230
### wp i18n update-po

src/MakeMoCommand.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class MakeMoCommand extends WP_CLI_Command {
2020
* : Path to an existing PO file or a directory containing multiple PO files.
2121
*
2222
* [<destination>]
23-
* : Path to the destination directory for the resulting MO files. Defaults to the source directory.
23+
* : Path to the destination file or directory for the resulting MO files. Defaults to the source directory.
2424
*
2525
* ## EXAMPLES
2626
*
@@ -30,6 +30,9 @@ class MakeMoCommand extends WP_CLI_Command {
3030
* # Create a MO file from a single PO file in a specific directory.
3131
* $ wp i18n make-mo example-plugin-de_DE.po languages
3232
*
33+
* # Create a MO file from a single PO file to a specific file destination
34+
* $ wp i18n make-mo example-plugin-de_DE.po languages/bar.mo
35+
*
3336
* @when before_wp_load
3437
*
3538
* @throws WP_CLI\ExitException
@@ -40,9 +43,19 @@ public function __invoke( $args, $assoc_args ) {
4043
WP_CLI::error( 'Source file or directory does not exist!' );
4144
}
4245

43-
$destination = is_file( $source ) ? dirname( $source ) : $source;
46+
$destination = is_file( $source ) ? dirname( $source ) : $source;
47+
$custom_file_name = null;
4448
if ( isset( $args[1] ) ) {
45-
$destination = $args[1];
49+
$destination = $args[1];
50+
$destionation_pathinfo = pathinfo( $destination );
51+
// Destination is a file, make sure source is also a file
52+
if ( ! empty( $destionation_pathinfo['filename'] ) ) {
53+
if ( ! is_file( $source ) ) {
54+
WP_CLI::error( 'Destination file not supported when source is a directory!' );
55+
}
56+
$destination = $destionation_pathinfo['dirname'];
57+
$custom_file_name = $destionation_pathinfo['filename'] . '.' . $destionation_pathinfo['extension'];
58+
}
4659
}
4760

4861
// Two is_dir() checks in case of a race condition.
@@ -71,8 +84,12 @@ public function __invoke( $args, $assoc_args ) {
7184
continue;
7285
}
7386

74-
$file_basename = basename( $file->getFilename(), '.po' );
75-
$destination_file = "{$destination}/{$file_basename}.mo";
87+
$file_basename = basename( $file->getFilename(), '.po' );
88+
$file_name = $file_basename . '.mo';
89+
if ( $custom_file_name ) {
90+
$file_name = $custom_file_name;
91+
}
92+
$destination_file = "{$destination}/{$file_name}";
7693

7794
$translations = Translations::fromPoFile( $file->getPathname() );
7895
if ( ! $translations->toMoFile( $destination_file ) ) {

tests/MakeMoTest.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace WP_CLI\I18n\Tests;
4+
5+
use Gettext\Translation;
6+
use WP_CLI\I18n\PotGenerator;
7+
use Gettext\Translations;
8+
use WP_CLI\Tests\TestCase;
9+
use WP_CLI\I18n\MakeMoCommand;
10+
use WP_CLI\Utils;
11+
12+
class MakeMoTest extends TestCase {
13+
public function set_up() {
14+
parent::set_up();
15+
$this->temp_dir = Utils\get_temp_dir() . uniqid( 'wp-cli-test-make-po-', true ) . '/';
16+
mkdir( $this->temp_dir );
17+
}
18+
19+
public function tear_down() {
20+
rmdir( $this->temp_dir );
21+
22+
parent::tear_down();
23+
}
24+
25+
/**
26+
* Test wp i18n make-mo foo.po
27+
*/
28+
public function test_single_file_generation() {
29+
$file_path = $this->temp_dir . '/test.po';
30+
touch( $file_path );
31+
$make_mo = new MakeMoCommand();
32+
$make_mo(
33+
array(
34+
$file_path,
35+
),
36+
array()
37+
);
38+
$this->assertTrue( is_file( $this->temp_dir . '/test.mo' ), 'MO file not generated' );
39+
unlink( $file_path );
40+
unlink( $this->temp_dir . '/test.mo' );
41+
}
42+
43+
/**
44+
* Test wp i18n make-mo foo.po bar.mo
45+
*/
46+
public function test_single_file_generation_named() {
47+
$file_path = $this->temp_dir . '/test.po';
48+
$mo_file_path = $this->temp_dir . '/bar.mo';
49+
touch( $file_path );
50+
$make_mo = new MakeMoCommand();
51+
$make_mo(
52+
array(
53+
$file_path,
54+
$mo_file_path,
55+
),
56+
array()
57+
);
58+
$this->assertTrue( is_file( $mo_file_path ), 'MO file not generated' );
59+
unlink( $file_path );
60+
unlink( $mo_file_path );
61+
}
62+
63+
/**
64+
* Test wp i18n make-mo foo/
65+
*/
66+
public function test_whole_directory_generation() {
67+
$base_directory = $this->temp_dir . '/foo';
68+
mkdir( $base_directory, 0777, true );
69+
$file_path = $base_directory . '/test.po';
70+
touch( $file_path );
71+
$make_mo = new MakeMoCommand();
72+
$make_mo(
73+
array(
74+
$base_directory,
75+
),
76+
array()
77+
);
78+
$this->assertTrue( is_file( $base_directory . '/test.mo' ), 'MO file not generated' );
79+
unlink( $file_path );
80+
unlink( $base_directory . '/test.mo' );
81+
rmdir( $base_directory );
82+
}
83+
}

0 commit comments

Comments
 (0)