Skip to content

Commit fff5e52

Browse files
authored
Migrate to gettext/gettext v5 (#522)
* Migrate to gettext/gettext v5 gettext/gettext v5 no longer ships extractors, the Jed and PHP array generators or the PHP functions scanner, and changes the data model (immutable merges, nullable getters, sorted headers and flags, references grouped by file). To keep the output of all commands unchanged: * Add `Translation` and `Translations` classes on top of the v5 ones that keep headers, flags and references in the order they were added and provide the default headers that v4 used to set. * Add a PO loader and PO/MO generators. The ones in v5 treat "0" as an empty string, which drops `msgid "0"` entries and `msgstr "0"` translations when reading, merging and writing files. * Port the PHP functions scanner from v4, as it is no longer part of the library. * Make the extractors and the Jed and PHP array generators standalone classes. * Implement the merge strategies used by `update-po` and `make-pot --merge/--subtract-and-merge` on top of the v5 API. Two intentional fixes on the way: headers of a PO file that only consists of a header entry without a trailing newline are no longer ignored, and strings with `\u{...}` escapes no longer trigger a `hexdec()` deprecation. * Fix PHPStan errors Narrow the return types of the `create()` factories, read comments through `toArray()`, and make numeric ranges explicit. * Fix type annotation for scanner constants * Keep data of plain gettext translations in insert() * Assert the translated value in the MO generator test * Use the header, flag and reference order of gettext/gettext v5 Drop the classes that kept headers, flags and references in the order they were added. Headers and flags are now sorted alphabetically and references are grouped by file, as the library does by default. The keys in generated PHP files keep their order. * Use the PO loader and generators of gettext/gettext 5.7.4 5.7.4 treats "0" as a valid string, so the local copies of the PO loader, the PO and MO generators and the `Translation` subclass are no longer needed. * PO and POT files are loaded with the library's loader. Entries that appear more than once in a file are still merged. * `make-mo` uses the library's MO generator. * `update-po` writes PO files with the library's generator. This keeps file-level comments with `--no-purge`, the `#, fuzzy` header flag and `#|` previous strings, and reactivates obsolete entries that are back in the POT file. * The POT generator keeps its own output format, as POT files need empty `msgstr[0]` and `msgstr[1]` lines for plurals. * Fix PHPStan error when merging duplicate entries * Mark removed strings as obsolete with `update-po --no-purge` Entries that are no longer in the POT file were kept as active entries, although the option is documented to keep them marked with `#~`. * Harden PO loading and writing in `update-po` * Stop when the source POT file cannot be read instead of treating it as empty, which would remove all entries from the PO files. * Prefix every line of a multiline obsolete entry with `#~`. gettext/gettext writes the continuation lines without it. * When merging duplicate entries, also treat a `#` line after an entry as its end, as the PO loader does. * Add Behat scenario for an unreadable POT file in `update-po` * Use gettext/php-scanner to find functions in PHP code Replaces the tokenizer-based scanner ported from gettext/gettext 4.x with the one from gettext/php-scanner, which uses nikic/php-parser. The subclass only adds error recovery, so that a file with a syntax error is still scanned instead of being skipped. As before, only the translator comment closest to a function call is extracted, and the lines of a multiline comment are joined with spaces. gettext/php-scanner is required as dev-master until 2.1.0 is released. * Fix PHPStan error for comment prefixes * Require gettext/php-scanner 2.1
1 parent 8eb50b5 commit fff5e52

37 files changed

Lines changed: 1783 additions & 623 deletions

‎.gitattributes‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@
1111
/phpstan.neon.dist export-ignore
1212
/phpunit.xml.dist export-ignore
1313
/tests export-ignore
14-
/stubs export-ignore
1514
/wp-cli.yml export-ignore

‎composer.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
],
1313
"require": {
1414
"eftec/bladeone": "3.52",
15-
"gettext/gettext": "^4.8",
15+
"gettext/gettext": "^5.7.4",
16+
"gettext/php-scanner": "^2.1",
1617
"mck89/peast": "^1.13.11",
1718
"wp-cli/wp-cli": "^3.0"
1819
},

‎features/makemo.feature‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,31 @@ Feature: Generate MO files from PO files
254254
JS Only Translation
255255
"""
256256

257+
258+
Scenario: Includes translations that are "0"
259+
Given an empty foo-plugin directory
260+
And a foo-plugin/foo-plugin-de_DE.po file:
261+
"""
262+
msgid ""
263+
msgstr ""
264+
"Project-Id-Version: Foo Plugin\n"
265+
"Language: de_DE\n"
266+
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
267+
"X-Domain: foo-plugin\n"
268+
269+
#: foo-plugin.php:10
270+
msgctxt "start of week"
271+
msgid "1"
272+
msgstr "0"
273+
"""
274+
275+
When I run `wp i18n make-mo foo-plugin`
276+
Then STDOUT should contain:
277+
"""
278+
Success: Created 1 file.
279+
"""
280+
And the return code should be 0
281+
And the foo-plugin/foo-plugin-de_DE.mo file should contain:
282+
"""
283+
start of week
284+
"""

‎features/makephp.feature‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,3 +400,39 @@ Feature: Generate PHP files from PO files
400400
[,
401401
"""
402402

403+
404+
Scenario: Includes translations that are "0"
405+
Given an empty foo-plugin directory
406+
And a foo-plugin/foo-plugin-de_DE.po file:
407+
"""
408+
msgid ""
409+
msgstr ""
410+
"Project-Id-Version: Foo Plugin\n"
411+
"Language: de_DE\n"
412+
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
413+
"X-Domain: foo-plugin\n"
414+
415+
#: foo-plugin.php:10
416+
msgctxt "start of week"
417+
msgid "1"
418+
msgstr "0"
419+
420+
#: foo-plugin.php:20
421+
msgid "0"
422+
msgstr "Null"
423+
"""
424+
425+
When I run `wp i18n make-php foo-plugin --pretty-print`
426+
Then STDOUT should contain:
427+
"""
428+
Success: Created 1 file.
429+
"""
430+
And the return code should be 0
431+
And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain:
432+
"""
433+
1' => '0',
434+
"""
435+
And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain:
436+
"""
437+
0 => 'Null',
438+
"""

‎features/makepot.feature‎

Lines changed: 123 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ Feature: Generate a POT file of a WordPress project
189189
And I run `wp i18n make-pot wp-content/plugins/hello-world wp-content/plugins/hello-world/languages/hello-world.pot`
190190
Then the wp-content/plugins/hello-world/languages/hello-world.pot file should contain:
191191
"""
192-
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
193192
"Language-Team: LANGUAGE <LL@li.org>\n"
193+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
194194
"""
195195

196196
Scenario: Sets the generator header
@@ -1765,17 +1765,23 @@ Feature: Generate a POT file of a WordPress project
17651765
# This file is distributed under the same license as the Hello World plugin.
17661766
msgid ""
17671767
msgstr ""
1768-
"Project-Id-Version: Hello World 0.1.0\n"
1769-
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/hello-world\n"
1770-
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1768+
"Content-Transfer-Encoding: 8bit\n"
1769+
"Content-Type: text/plain; charset=UTF-8\n"
17711770
"Language-Team: LANGUAGE <LL@li.org>\n"
1771+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
17721772
"MIME-Version: 1.0\n"
1773-
"Content-Type: text/plain; charset=UTF-8\n"
1774-
"Content-Transfer-Encoding: 8bit\n"
1773+
"""
1774+
And the wp-content/plugins/hello-world/languages/hello-world.pot file should contain:
1775+
"""
1776+
"Project-Id-Version: Hello World 0.1.0\n"
1777+
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/hello-world\n"
17751778
"""
17761779
And the wp-content/plugins/hello-world/languages/hello-world.pot file should not contain:
17771780
"""
17781781
"POT-Creation-Date: 2018-05-02T22:06:24+00:00\n"
1782+
"""
1783+
And the wp-content/plugins/hello-world/languages/hello-world.pot file should not contain:
1784+
"""
17791785
"PO-Revision-Date: 2018-05-02T22:06:24+00:00\n"
17801786
"""
17811787
And the wp-content/plugins/hello-world/languages/hello-world.pot file should contain:
@@ -4401,3 +4407,114 @@ Feature: Generate a POT file of a WordPress project
44014407
#, js-format
44024408
msgid "Buongiorno %1$s"
44034409
"""
4410+
4411+
Scenario: Subtracts and merges strings that are "0"
4412+
Given an empty foo-plugin directory
4413+
And a foo-plugin/exception.pot file:
4414+
"""
4415+
msgid ""
4416+
msgstr ""
4417+
"Project-Id-Version: Foo Plugin\n"
4418+
4419+
#: other.php:1
4420+
msgid "0"
4421+
msgstr ""
4422+
"""
4423+
And a foo-plugin/existing.pot file:
4424+
"""
4425+
msgid ""
4426+
msgstr ""
4427+
"Project-Id-Version: Foo Plugin\n"
4428+
4429+
#: existing.php:1
4430+
msgctxt "number"
4431+
msgid "0"
4432+
msgstr ""
4433+
"""
4434+
And a foo-plugin/foo-plugin.php file:
4435+
"""
4436+
<?php
4437+
/**
4438+
* Plugin Name: Foo Plugin
4439+
*/
4440+
4441+
__( '0', 'foo-plugin' );
4442+
4443+
__( 'Hello World', 'foo-plugin' );
4444+
"""
4445+
4446+
When I run `wp i18n make-pot foo-plugin foo-plugin/foo-plugin.pot --subtract=foo-plugin/exception.pot --merge=foo-plugin/existing.pot`
4447+
Then STDOUT should be:
4448+
"""
4449+
Plugin file detected.
4450+
Success: POT file successfully generated.
4451+
"""
4452+
And STDERR should be empty
4453+
And the foo-plugin/foo-plugin.pot file should contain:
4454+
"""
4455+
#: existing.php:1
4456+
msgctxt "number"
4457+
msgid "0"
4458+
msgstr ""
4459+
"""
4460+
And the foo-plugin/foo-plugin.pot file should contain:
4461+
"""
4462+
msgid "Hello World"
4463+
"""
4464+
And the foo-plugin/foo-plugin.pot file should not contain:
4465+
"""
4466+
#: foo-plugin.php:6
4467+
"""
4468+
4469+
Scenario: Groups references by file and sorts flags
4470+
Given an empty foo-plugin directory
4471+
And a foo-plugin/foo-plugin.php file:
4472+
"""
4473+
<?php
4474+
/**
4475+
* Plugin Name: Foo Plugin
4476+
*/
4477+
4478+
/* translators: %s: Name */
4479+
__( 'Hello %s', 'foo-plugin' );
4480+
"""
4481+
And a foo-plugin/admin.php file:
4482+
"""
4483+
<?php
4484+
/* translators: %s: Name */
4485+
__( 'Hello %s', 'foo-plugin' );
4486+
"""
4487+
And a foo-plugin/build/index.js file:
4488+
"""
4489+
/* translators: %s: Name */
4490+
__( 'Hello %s', 'foo-plugin' );
4491+
"""
4492+
And a foo-plugin/build/index.js.map file:
4493+
"""
4494+
{"version":3,"sources":["index.js"],"sourcesContent":["\n\n/* translators: %s: Name */\n__( 'Hello %s', 'foo-plugin' );"]}
4495+
"""
4496+
And a foo-plugin/build/view.js file:
4497+
"""
4498+
/* translators: %s: Name */
4499+
__( 'Hello %s', 'foo-plugin' );
4500+
"""
4501+
4502+
When I run `wp i18n make-pot foo-plugin`
4503+
Then STDOUT should be:
4504+
"""
4505+
Plugin file detected.
4506+
Success: POT file successfully generated.
4507+
"""
4508+
And STDERR should be empty
4509+
And the foo-plugin/foo-plugin.pot file should contain:
4510+
"""
4511+
#. translators: %s: Name
4512+
#: admin.php:3
4513+
#: foo-plugin.php:7
4514+
#: build/index.js:2
4515+
#: build/index.js:4
4516+
#: build/view.js:2
4517+
#, js-format,php-format
4518+
msgid "Hello %s"
4519+
msgstr ""
4520+
"""

0 commit comments

Comments
 (0)