-
Notifications
You must be signed in to change notification settings - Fork 52
/
UpdatePoCommand.php
100 lines (86 loc) · 2.68 KB
/
UpdatePoCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace WP_CLI\I18n;
use DirectoryIterator;
use Gettext\Extractors\Po;
use Gettext\Merge;
use Gettext\Translations;
use IteratorIterator;
use SplFileInfo;
use WP_CLI;
use WP_CLI\Utils;
use WP_CLI_Command;
class UpdatePoCommand extends WP_CLI_Command {
/**
* Update PO files from a POT file.
*
* This behaves similarly to the [msgmerge](https://www.gnu.org/software/gettext/manual/html_node/msgmerge-Invocation.html) command.
*
* ## OPTIONS
*
* <source>
* : Path to an existing POT file to use for updating.
*
* [<destination>]
* : PO file to update or a directory containing multiple PO files.
* Defaults to all PO files in the source directory.
*
* ## EXAMPLES
*
* # Update all PO files from a POT file in the current directory.
* $ wp i18n update-po example-plugin.pot
* Success: Updated 3 files.
*
* # Update a PO file from a POT file.
* $ wp i18n update-po example-plugin.pot example-plugin-de_DE.po
* Success: Updated 1 file.
*
* # Update all PO files in a given directory from a POT file.
* $ wp i18n update-po example-plugin.pot languages
* Success: Updated 2 files.
*
* @when before_wp_load
*
* @throws WP_CLI\ExitException
*/
public function __invoke( $args, $assoc_args ) {
$source = realpath( $args[0] );
if ( ! $source || ! is_file( $source ) ) {
WP_CLI::error( 'Source file does not exist.' );
}
$destination = dirname( $source );
if ( isset( $args[1] ) ) {
$destination = $args[1];
}
if ( ! file_exists( $destination ) ) {
WP_CLI::error( 'Destination file/folder does not exist.' );
}
if ( is_file( $destination ) ) {
$files = [ new SplFileInfo( $destination ) ];
} else {
$files = new IteratorIterator( new DirectoryIterator( $destination ) );
}
$pot_translations = Translations::fromPoFile( $source );
$result_count = 0;
/** @var DirectoryIterator $file */
foreach ( $files as $file ) {
if ( 'po' !== $file->getExtension() ) {
continue;
}
if ( ! $file->isFile() || ! $file->isReadable() ) {
WP_CLI::warning( sprintf( 'Could not read file %s', $file->getFilename() ) );
continue;
}
$po_translations = Translations::fromPoFile( $file->getPathname() );
$po_translations->mergeWith(
$pot_translations,
Merge::ADD | Merge::REMOVE | Merge::COMMENTS_THEIRS | Merge::EXTRACTED_COMMENTS_THEIRS | Merge::REFERENCES_THEIRS
);
if ( ! $po_translations->toPoFile( $file->getPathname() ) ) {
WP_CLI::warning( sprintf( 'Could not update file %s', $file->getPathname() ) );
continue;
}
++$result_count;
}
WP_CLI::success( sprintf( 'Updated %d %s.', $result_count, Utils\pluralize( 'file', $result_count ) ) );
}
}