Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions features/updatepo.feature
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,142 @@ Feature: Update existing PO files from a POT file
"""
"X-Domain: foo-plugin\n"
"""

Scenario: Reports unchanged files when POT hasn't changed
Given an empty foo-plugin directory
And a foo-plugin/foo-plugin.pot file:
"""
# Copyright (C) 2018 Foo Plugin
# This file is distributed under the same license as the Foo Plugin package.
msgid ""
msgstr ""
"Project-Id-Version: Foo Plugin\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/foo-plugin\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2018-05-02T22:06:24+00:00\n"
"PO-Revision-Date: 2018-05-02T22:06:24+00:00\n"
"X-Domain: foo-plugin\n"

#: foo-plugin.php:1
msgid "Some string"
msgstr ""
"""
And a foo-plugin/foo-plugin-de_DE.po file:
"""
# Copyright (C) 2018 Foo Plugin
# This file is distributed under the same license as the Foo Plugin package.
msgid ""
msgstr ""
"Project-Id-Version: Foo Plugin\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/foo-plugin\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: de_DE\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2018-05-02T22:06:24+00:00\n"
"PO-Revision-Date: 2018-05-02T22:06:24+00:00\n"
"X-Domain: foo-plugin\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#: foo-plugin.php:1
msgid "Some string"
msgstr "Some translated string"
"""

When I run `wp i18n update-po foo-plugin/foo-plugin.pot`
Then STDOUT should be:
"""
Success: Updated 0 files. 1 file unchanged.
"""
And STDERR should be empty

Scenario: Reports both updated and unchanged files
Given an empty foo-plugin directory
And a foo-plugin/foo-plugin.pot file:
"""
# Copyright (C) 2018 Foo Plugin
# This file is distributed under the same license as the Foo Plugin package.
msgid ""
msgstr ""
"Project-Id-Version: Foo Plugin\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/foo-plugin\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2018-05-02T22:06:24+00:00\n"
"PO-Revision-Date: 2018-05-02T22:06:24+00:00\n"
"X-Domain: foo-plugin\n"

#: foo-plugin.php:1
msgid "Some string"
msgstr ""

#: foo-plugin.php:15
msgid "Another new string"
msgstr ""
"""
And a foo-plugin/foo-plugin-de_DE.po file:
"""
# Copyright (C) 2018 Foo Plugin
# This file is distributed under the same license as the Foo Plugin package.
msgid ""
msgstr ""
"Project-Id-Version: Foo Plugin\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/foo-plugin\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: de_DE\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2018-05-02T22:06:24+00:00\n"
"PO-Revision-Date: 2018-05-02T22:06:24+00:00\n"
"X-Domain: foo-plugin\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#: foo-plugin.php:10
msgid "Some string"
msgstr "Some translated string"
"""
And a foo-plugin/foo-plugin-es_ES.po file:
"""
# Copyright (C) 2018 Foo Plugin
# This file is distributed under the same license as the Foo Plugin package.
msgid ""
msgstr ""
"Project-Id-Version: Foo Plugin\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/foo-plugin\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: es_ES\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2018-05-02T22:06:24+00:00\n"
"PO-Revision-Date: 2018-05-02T22:06:24+00:00\n"
"X-Domain: foo-plugin\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#: foo-plugin.php:1
msgid "Some string"
msgstr "Some translated string"

#: foo-plugin.php:15
msgid "Another new string"
msgstr "Otra nueva cadena"
"""

When I run `wp i18n update-po foo-plugin/foo-plugin.pot`
Then STDOUT should be:
"""
Success: Updated 1 file. 1 file unchanged.
"""
And STDERR should be empty
29 changes: 26 additions & 3 deletions src/UpdatePoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
* $ 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
Expand Down Expand Up @@ -69,7 +73,8 @@

$pot_translations = Translations::fromPoFile( $source );

$result_count = 0;
$updated_count = 0;
$unchanged_count = 0;
/** @var DirectoryIterator $file */
foreach ( $files as $file ) {
if ( 'po' !== $file->getExtension() ) {
Expand All @@ -81,6 +86,9 @@
continue;
}

// Read the original file content before merging.
$original_content = file_get_contents( $file->getPathname() );

$po_translations = Translations::fromPoFile( $file->getPathname() );
$po_translations->mergeWith(
$pot_translations,
Expand All @@ -92,9 +100,24 @@
continue;
}

++$result_count;
// Read the new file content after merging.
$new_content = file_get_contents( $file->getPathname() );

// Check if the file actually changed.
if ( $original_content !== $new_content ) {
++$updated_count;
} else {
++$unchanged_count;
}
}

// Build the success message.
$message_parts = array();

Check warning on line 115 in src/UpdatePoCommand.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

Check warning on line 115 in src/UpdatePoCommand.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
$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( sprintf( 'Updated %d %s.', $result_count, Utils\pluralize( 'file', $result_count ) ) );
WP_CLI::success( implode( '. ', $message_parts ) . '.' );
}
}
Loading