-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathUpdatePoCommand.php
More file actions
221 lines (184 loc) · 6.55 KB
/
UpdatePoCommand.php
File metadata and controls
221 lines (184 loc) · 6.55 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?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.
*
* # Shows message when some files don't need updating.
* $ wp i18n update-po example-plugin.pot languages
* Success: Updated 2 files. 1 file unchanged.
*
* @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 );
$updated_count = 0;
$unchanged_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() );
$original_translations = clone $po_translations;
$po_translations->mergeWith(
$pot_translations,
Merge::ADD | Merge::REMOVE | Merge::COMMENTS_THEIRS | Merge::EXTRACTED_COMMENTS_THEIRS | Merge::REFERENCES_THEIRS | Merge::DOMAIN_OVERRIDE
);
// Check if the translations actually changed by comparing the objects.
$has_changes = $this->translations_differ( $original_translations, $po_translations );
// Update PO-Revision-Date to current date and time in UTC.
// Uses gmdate() for consistency across different server timezones.
$po_translations->setHeader( 'PO-Revision-Date', gmdate( 'Y-m-d\TH:i:sP' ) );
// Reorder translations to match POT file order.
$ordered_translations = $this->reorder_translations( $po_translations, $pot_translations );
if ( ! $ordered_translations->toPoFile( $file->getPathname() ) ) {
WP_CLI::warning( sprintf( 'Could not update file %s', $file->getPathname() ) );
continue;
}
if ( $has_changes ) {
++$updated_count;
} else {
++$unchanged_count;
}
}
// Build the success message.
$message_parts = array();
$message_parts[] = sprintf( 'Updated %d %s', $updated_count, Utils\pluralize( 'file', $updated_count ) );
if ( $unchanged_count > 0 ) {
$message_parts[] = sprintf( '%d %s unchanged', $unchanged_count, Utils\pluralize( 'file', $unchanged_count ) );
}
WP_CLI::success( implode( '. ', $message_parts ) . '.' );
}
/**
* Check if two Translations objects differ.
*
* @param Translations $original Original translations.
* @param Translations $updated Updated translations.
* @return bool True if translations differ, false otherwise.
*/
private function translations_differ( Translations $original, Translations $updated ) {
// Quick check: if counts differ, they're different.
if ( count( $original ) !== count( $updated ) ) {
return true;
}
// Compare each translation entry.
foreach ( $original as $translation ) {
$context = $translation->getContext();
$original_str = $translation->getOriginal();
// Find the corresponding translation in the updated set.
$updated_translation = $updated->find( $context, $original_str );
// If translation doesn't exist in updated set, they differ.
if ( ! $updated_translation ) {
return true;
}
// Compare translation strings.
if ( $translation->getTranslation() !== $updated_translation->getTranslation() ) {
return true;
}
// Compare plural translations if they exist.
$original_plurals = $translation->getPluralTranslations();
$updated_plurals = $updated_translation->getPluralTranslations();
if ( $original_plurals !== $updated_plurals ) {
return true;
}
// Compare references (source code locations).
$original_refs = $translation->getReferences();
$updated_refs = $updated_translation->getReferences();
$original_refs_sorted = $original_refs;
$updated_refs_sorted = $updated_refs;
sort( $original_refs_sorted );
sort( $updated_refs_sorted );
if ( $original_refs_sorted !== $updated_refs_sorted ) {
return true;
}
// Compare comments.
if ( $translation->getExtractedComments() !== $updated_translation->getExtractedComments() ) {
return true;
}
if ( $translation->getComments() !== $updated_translation->getComments() ) {
return true;
}
}
return false;
}
/**
* Reorder translations to match the POT file order.
*
* @param Translations $po_translations The merged PO translations.
* @param Translations $pot_translations The POT translations (source of truth for order).
*
* @return Translations Translations object with entries in POT file order.
*/
private function reorder_translations( Translations $po_translations, Translations $pot_translations ) {
$ordered = new Translations();
// Copy headers from the merged PO translations.
foreach ( $po_translations->getHeaders() as $name => $value ) {
$ordered->setHeader( $name, $value );
}
// Add translations in POT file order.
foreach ( $pot_translations as $pot_entry ) {
$po_entry = $po_translations->find( $pot_entry );
if ( $po_entry ) {
$ordered[] = $po_entry->getClone();
}
}
return $ordered;
}
}