Skip to content

Commit 9cf9b40

Browse files
authored
Merge pull request #373 from sdnunca/feature/i18n-command-add-destination-file-support
2 parents e84691f + c5a4442 commit 9cf9b40

File tree

3 files changed

+70
-8
lines changed

3 files changed

+70
-8
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

features/makemo.feature

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,17 @@ Feature: Generate MO files from PO files
1010
Error: Source file or directory does not exist!
1111
"""
1212
And the return code should be 1
13-
13+
Scenario: Bail for destination being a file when source is a folder
14+
Given an empty foo directory
15+
And a foo/foo.po file:
16+
"""
17+
"""
18+
When I try `wp i18n make-mo foo test.mo `
19+
Then STDERR should contain:
20+
"""
21+
Error: Destination file not supported when source is a directory!
22+
"""
23+
And the return code should be 1
1424
Scenario: Uses source folder as destination by default
1525
Given an empty foo-plugin directory
1626
And a foo-plugin/foo-plugin-de_DE.po file:
@@ -44,7 +54,39 @@ Feature: Generate MO files from PO files
4454
"""
4555
And the return code should be 0
4656
And the foo-plugin/foo-plugin-de_DE.mo file should exist
47-
57+
Scenario: Uses the provided destination file name
58+
Given a foo.po file:
59+
"""
60+
"""
61+
When I run `wp i18n make-mo foo.po bar.mo`
62+
Then STDOUT should contain:
63+
"""
64+
Success: Created 1 file.
65+
"""
66+
And the return code should be 0
67+
And the bar.mo file should exist
68+
Scenario: Uses the provided destination file name with no extension
69+
Given a foo.po file:
70+
"""
71+
"""
72+
When I run `wp i18n make-mo foo.po bar`
73+
Then STDOUT should contain:
74+
"""
75+
Success: Created 1 file.
76+
"""
77+
And the return code should be 0
78+
And the bar file should exist
79+
Scenario: Preserves the provided source name with no destination
80+
Given a foo.po file:
81+
"""
82+
"""
83+
When I run `wp i18n make-mo foo.po`
84+
Then STDOUT should contain:
85+
"""
86+
Success: Created 1 file.
87+
"""
88+
And the return code should be 0
89+
And the foo.mo file should exist
4890
Scenario: Allows setting custom destination directory
4991
Given an empty foo-plugin directory
5092
And a foo-plugin/foo-plugin-de_DE.po file:

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+
$destination_pathinfo = pathinfo( $destination );
51+
// Destination is a file, make sure source is also a file
52+
if ( ! empty( $destination_pathinfo['filename'] ) && ! empty( $destination_pathinfo['extension'] ) ) {
53+
if ( ! is_file( $source ) ) {
54+
WP_CLI::error( 'Destination file not supported when source is a directory!' );
55+
}
56+
$destination = $destination_pathinfo['dirname'];
57+
$custom_file_name = $destination_pathinfo['filename'] . '.' . $destination_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 ) ) {

0 commit comments

Comments
 (0)