Repository navigation
Migrate to gettext/gettext v5 - #522
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
Walkthrough
Merge Risk:
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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.
Narrow the return types of the `create()` factories, read comments through `toArray()`, and make numeric ranges explicit.
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.
ece9bd0 to
4cdfebc
Compare
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.
Entries that are no longer in the POT file were kept as active entries, although the option is documented to keep them marked with `#~`.
* 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.
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.
Updates
gettext/gettextfrom^4.8to^5.7.4and usesgettext/php-scanner(^2.1) to find translatable strings in PHP code.Previously: #443, #465. See #303.
Why this is more than a version bump
v5 is a different library in a few ways that matter here:
gettext/gettext. The PHP scanner now lives ingettext/php-scanner, see "PHP scanning" below."0"as an empty string, droppingmsgid "0"entries andmsgstr "0"translations. WordPress core has strings like this (_x( '1', 'start of week' ), which translators answer with0). Fixed upstream in Treat "0" as a valid string php-gettext/Gettext#311, released in 5.7.4, which is why that is the minimum version.What changed
PoLoaderviaTranslations::fromPoFiles(). Entries that appear more than once in a file are still merged, as before.make-mouses the library'sMoGenerator, andupdate-pouses itsPoGenerator.PotGeneratorkeeps its own output format, because POT files need emptymsgstr[0]andmsgstr[1]lines for plurals, which the library's generator only writes when there is aPlural-Formsheader.Translationsextends the v5 class and sets the default headers v4 used to set.gettext/php-scanner, which usesnikic/php-parser.PhpFunctionsScanneris a small subclass that recovers from syntax errors, so that a file with an error is still scanned instead of skipped.PhpCodeExtractormaps the calls to the WordPress function signatures, as before.fromString()/fromFile()/fromDirectory()API is unchanged,fromStringMultiple()is gone.JedGeneratorandPhpArrayGeneratorno longer have a library parent.update-poandmake-pot --merge/--subtract-and-mergeimplement their merge rules on top of the v5 API, since v5 replaced v4's merge flags.stubs/Gettext.stubis removed, it only described v4 classes.This is a breaking change for anyone using these classes as a library: type hints now refer to
WP_CLI\I18n\TranslationsandGettext\Translation, v4 methods such asTranslations::fromPoFile()no longer exist,PhpFunctionsScannerhas the API of thegettext/php-scannerclass it extends and returnsGettext\Scanner\ParsedFunctionobjects, andWP_CLI\I18n\ParsedFunctionis gone. The unusedconstantsextractor option is gone.PHP scanning
Upstream changes, released in
gettext/php-scanner2.1.0, made it usable here:Two things are kept from the old scanner: only the translator comment closest to a call is extracted, and the lines of a multiline comment are joined with spaces.
Compared with the old token-based scanner on WordPress core (trunk), Twenty* themes and Gutenberg (
make-pot, PHP only), all strings and references are the same. Some translator comments differ:_x( 'F Y', … )insideprintf( __( 'Monthly Archives: %s' ), … )gottranslators: %s: Date., and__( 'Latest' )got the comment meant forUpdate to %s. Six such comments are gone in core.?or:in a ternary are not found, because the parser does not attach them to a node (class-wp-object-cache.php,blocks/gallery.php). WordPress/wordpress-develop and WordPress/gutenberg changes that move these comments are proposed separately.array( _x( 'Most Used', 'tags' ), _x( 'Most Used', 'categories' ) )the second call has no comment, and neither has%sX-Small/%sX-Largeinclass-wp-theme-json.php.Behavior changes
Ordering. This follows what v5 does by default. It does not change the meaning of a file, but existing files change once when they are regenerated:
Project-Id-Versionis no longer first. This applies to POT and PO files and to the header entry inside MO files..js.mapfiles.php-format,js-formatbecomesjs-format,php-format.JSON and PHP (
.l10n.php) output is not affected.Translator comments in PHP files, see "PHP scanning".
update-po:#~) entries whose string is back in the POT file are reactivated, asmsgmergedoes. Before, they stayed obsolete and their translation unused.--no-purge, entries that are no longer in the POT file are now marked as obsolete (#~), as the option's description says. Before, they were kept as active entries.#, fuzzyflag of the header entry and#|previous strings are kept. Before, they were dropped.--no-purge, file-level comments are followed by an empty#line, as the library writes them. By default they are still removed.Fixes:
\u{...}escapes no longer trigger ahexdec()deprecation.Verification
"0"strings and translations, the new ordering, duplicate entries, loading several files, file-level comments and flags, obsolete and multiline obsolete entries inupdate-po, an unreadable POT file, MO output, and PHP scanning including syntax errors and multiline comments.gettext/php-scanner, I compared this branch againstmain(gettext v4) by running both on the same input and diffing the results byte for byte, with dates normalized. Everything was identical then on:make-potandauditon Akismet, Contact Form 7, Yoast SEO, WooCommerce, Jetpack, Gutenberg, Twenty Twenty-One, Twenty Twenty-Five, Sage (Blade) and WordPress core (with--subtract,--subtract-and-merge,--merge,--headers,--file-comment);make-mo,make-php,make-jsonon WordPress and WooCommerce language packs; andupdate-poon Yoast SEO language packs. Output now differs by the changes listed above.🤖 Generated with Claude Code
https://claude.ai/code/session_01UWTd5qmztfFv37ybV4xsjV
Summary by CodeRabbit
"0"across MO, PHP, and POT generation, and PO updates.