Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
106 changes: 4 additions & 102 deletions features/makejson.feature
Original file line number Diff line number Diff line change
Expand Up @@ -325,15 +325,15 @@ Feature: Split PO files into JSON files.
"""
"D"
"""
And the foo-plugin/foo-plugin-de_DE.po file should not contain:
And the foo-plugin/foo-plugin-de_DE.po file should contain:
"""
"A"
"""
And the foo-plugin/foo-plugin-de_DE.po file should not contain:
And the foo-plugin/foo-plugin-de_DE.po file should contain:
"""
"B"
"""
And the foo-plugin/foo-plugin-de_DE.po file should not contain:
And the foo-plugin/foo-plugin-de_DE.po file should contain:
"""
"C"
"""
Expand Down Expand Up @@ -377,7 +377,7 @@ Feature: Split PO files into JSON files.
msgstr "D"
"""

When I run `wp i18n make-json foo-plugin --no-purge`
When I run `wp i18n make-json foo-plugin`
Then STDOUT should contain:
"""
Success: Created 2 files.
Expand Down Expand Up @@ -437,104 +437,6 @@ Feature: Split PO files into JSON files.
"C"
"""

Scenario: Does generate or update MO files
Given an empty foo-plugin directory
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 <LL@li.org>\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"

#: a.js:10
msgid "A"
msgstr "A"

#: b.js:10
msgid "B"
msgstr "B"

#: a.js:10
#: b.js:10
msgid "C"
msgstr "C"

#: foo-plugin.php:10
msgid "D"
msgstr "D"
"""

When I run `wp i18n make-json foo-plugin`
Then STDOUT should contain:
"""
Success: Created 2 files.
"""
And the return code should be 0

And the foo-plugin/foo-plugin-de_DE-95f0a310f289230d56c3a4949c17963e.json file should exist
And the foo-plugin/foo-plugin-de_DE.mo file should exist

Scenario: Does not generate or update MO files
Given an empty foo-plugin directory
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 <LL@li.org>\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"

#: a.js:10
msgid "A"
msgstr "A"

#: b.js:10
msgid "B"
msgstr "B"

#: a.js:10
#: b.js:10
msgid "C"
msgstr "C"

#: foo-plugin.php:10
msgid "D"
msgstr "D"
"""

When I run `wp i18n make-json foo-plugin --no-update-mo-files`
Then STDOUT should contain:
"""
Success: Created 2 files.
"""
And the return code should be 0

And the foo-plugin/foo-plugin-de_DE-95f0a310f289230d56c3a4949c17963e.json file should exist
And the foo-plugin/foo-plugin-de_DE.mo file should not exist

Scenario: Correctly saves strings with context
Given an empty foo-plugin directory
And a foo-plugin/foo-plugin-de_DE.po file:
Expand Down
58 changes: 57 additions & 1 deletion features/makemo.feature
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ Feature: Generate MO files from PO files
"X-Domain: foo-plugin\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#: foo-plugin.js:15
#: foo-plugin.php:15
msgid "Foo Plugin"
msgstr "Bar Plugin"
"""
Expand All @@ -198,3 +198,59 @@ Feature: Generate MO files from PO files
"""
Bar Plugin
"""

Scenario: Excludes JS-only strings from MO files
Given an empty foo-plugin directory
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 <LL@li.org>\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.js:15
msgid "JS Only String"
msgstr "JS Only Translation"

#: foo-plugin.php:10
msgid "PHP String"
msgstr "PHP Translation"

#: foo-plugin.js:20
#: foo-plugin.php:15
msgid "Both JS and PHP"
msgstr "Both Translation"
"""

When I run `wp i18n make-mo foo-plugin`
Then STDOUT should contain:
"""
Success: Created 1 file.
"""
And the return code should be 0
And the foo-plugin/foo-plugin-de_DE.mo file should exist
And the foo-plugin/foo-plugin-de_DE.mo file should contain:
"""
PHP Translation
"""
And the foo-plugin/foo-plugin-de_DE.mo file should contain:
"""
Both Translation
"""
And the foo-plugin/foo-plugin-de_DE.mo file should not contain:
"""
JS Only Translation
"""

66 changes: 65 additions & 1 deletion features/makephp.feature
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,15 @@ Feature: Generate PHP files from PO files
And the return code should be 0
And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain:
"""
return ['domain'=>'foo-plugin','plural-forms'=>'nplurals=2; plural=(n != 1);','language'=>'de_DE','project-id-version'=>'Foo Plugin','pot-creation-date'=>'2018-05-02T22:06:24+00:00','po-revision-date'=>'2018-05-02T22:06:24+00:00','messages'=>['Plugin NameFoo Plugin (EN)'=>'Foo Plugin (DE)','Foo Plugin'=>'Bar Plugin','You have %d new message'=>'Sie haben %d neue Nachricht' . "\0" . 'Sie haben %d neue Nachrichten']];
You have %d new message
"""
And the foo-plugin/foo-plugin-de_DE.l10n.php file should not contain:
"""
Foo Plugin (DE)
"""
And the foo-plugin/foo-plugin-de_DE.l10n.php file should not contain:
"""
Bar Plugin
"""

Scenario: Excludes strings without translations
Expand Down Expand Up @@ -300,3 +308,59 @@ Feature: Generate PHP files from PO files
"""
'messages' => [
"""

Scenario: Excludes JS-only strings from PHP files
Given an empty foo-plugin directory
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 <LL@li.org>\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.js:15
msgid "JS Only String"
msgstr "JS Only Translation"

#: foo-plugin.php:10
msgid "PHP String"
msgstr "PHP Translation"

#: foo-plugin.js:20
#: foo-plugin.php:15
msgid "Both JS and PHP"
msgstr "Both Translation"
"""

When I run `wp i18n make-php foo-plugin`
Then STDOUT should contain:
"""
Success: Created 1 file.
"""
And the return code should be 0
And the foo-plugin/foo-plugin-de_DE.l10n.php file should exist
And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain:
"""
PHP Translation
"""
And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain:
"""
Both Translation
"""
And the foo-plugin/foo-plugin-de_DE.l10n.php file should not contain:
"""
JS Only Translation
"""

46 changes: 46 additions & 0 deletions src/JsStringFilterTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace WP_CLI\I18n;

use Gettext\Translation;
use Gettext\Translations;

trait JsStringFilterTrait {
/**
* JavaScript file extensions to check for.
*
* @var string[]
*/
protected static $js_extensions = [ '.js', '.jsx', '.ts', '.tsx', '.mjs', '.cjs' ];

/**
* Removes strings from translations that only occur in JavaScript files.
*
* @param Translations $translations The translations instance to filter.
*/
protected function remove_js_only_strings( Translations $translations ): void {
foreach ( $translations->getArrayCopy() as $translation ) {
/** @var Translation $translation */

if ( ! $translation->hasReferences() ) {
continue;
}

$has_non_js_reference = false;
foreach ( $translation->getReferences() as $reference ) {
$file = $reference[0];
$extension = '.' . strtolower( pathinfo( $file, PATHINFO_EXTENSION ) );

if ( ! in_array( $extension, static::$js_extensions, true ) ) {
$has_non_js_reference = true;
break;
}
}

// If all references are JS files, remove this translation.
if ( ! $has_non_js_reference ) {
unset( $translations[ $translation->getId() ] );
}
}
}
}
Loading