Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wp i18n make-php command #363

Merged
merged 12 commits into from
Jan 30, 2024
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
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,32 @@ wp i18n make-mo <source> [<destination>]



### wp i18n make-php

Create PHP files from PO files.

~~~
wp i18n make-php <source> [<destination>]
~~~

**OPTIONS**

<source>
Path to an existing PO file or a directory containing multiple PO files.

[<destination>]
Path to the destination directory for the resulting PHP files. Defaults to the source directory.

**EXAMPLES**

# Create PHP files for all PO files in the current directory.
$ wp i18n make-php .

# Create a PHP file from a single PO file in a specific directory.
$ wp i18n make-php example-plugin-de_DE.po languages



### wp i18n update-po

Update PO files from a POT file.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"i18n make-pot",
"i18n make-json",
"i18n make-mo",
"i18n make-php",
"i18n update-po"
]
},
Expand Down
2 changes: 1 addition & 1 deletion features/makejson.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Feature: Split PO files into JSON files.
When I try `wp i18n make-json foo`
Then STDERR should contain:
"""
Error: Source file or directory does not exist!
Error: Source file or directory does not exist.
"""
And the return code should be 1

Expand Down
4 changes: 2 additions & 2 deletions features/makemo.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Feature: Generate MO files from PO files
When I try `wp i18n make-mo foo`
Then STDERR should contain:
"""
Error: Source file or directory does not exist!
Error: Source file or directory does not exist.
"""
And the return code should be 1
Scenario: Bail for destination being a file when source is a folder
Expand All @@ -18,7 +18,7 @@ Feature: Generate MO files from PO files
When I try `wp i18n make-mo foo test.mo `
Then STDERR should contain:
"""
Error: Destination file not supported when source is a directory!
Error: Destination file not supported when source is a directory.
"""
And the return code should be 1
Scenario: Uses source folder as destination by default
Expand Down
167 changes: 167 additions & 0 deletions features/makephp.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
Feature: Generate PHP files from PO files

Background:
Given an empty directory

Scenario: Bail for invalid source directories
When I try `wp i18n make-php foo`
Then STDERR should contain:
"""
Error: Source file or directory does not exist.
"""
And the return code should be 1

Scenario: Uses source folder as destination by default
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 <[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.js:15
msgid "Foo Plugin"
msgstr "Foo Plugin"
"""

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

Scenario: Allows setting custom destination directory
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 <[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.js:15
msgid "Foo Plugin"
msgstr "Foo Plugin"
"""

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

Scenario: Does include headers
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 <[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.js:15
msgid "Foo Plugin"
msgstr "Foo Plugin"
"""

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 STDERR should be empty
And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain:
"""
'language'=>'de_DE'
"""
And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain:
"""
'domain'=>'foo-plugin'
"""
And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain:
"""
'plural-forms'=>'nplurals=2; plural=(n != 1);'
"""
And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain:
"""
'messages'=>[''=>['Foo Plugin'=>['Foo Plugin']]]
"""

Scenario: Does include translations
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 <[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.js:15
msgid "Foo Plugin"
msgstr "Bar Plugin"
"""

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 contain:
"""
'messages'=>[''=>['Foo Plugin'=>['Bar Plugin']]]
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have a test that verifies a valid PHP translation file is produced?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid as in syntax or as in translation gets loaded properly in WordPress?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking the latter but we could check both, I suppose.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably best done in a future PR because we don't have similar assertions for the other file formats, and the PHP format especially is not yet merged into core.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good 👍

Loading