Make WordPress Core

Opened 3 hours ago

Last modified 3 hours ago

#66721 new defect (bug)

wptexturize() keeps the first locale's quotes after switch_to_locale()

Reported by: dpknauss Owned by:
Priority: normal Milestone: Awaiting Review
Component: Formatting Version: 4.7
Severity: normal Keywords: has-unit-tests has-patch
Cc: Focuses:

Description (last modified by dpknauss)

wptexturize() builds its quote, apostrophe, and dash replacements once per request and keeps them in static variables. If the locale changes during the request, every later call still uses the first locale's punctuation.

This happens whenever a request renders text in more than one locale. For example, a multilingual plugin calls switch_to_locale( 'de_DE' ) to render a German post or email from an English site, and the German text gets English curly quotes (“Hallo”) instead of German ones („Hallo“). restore_previous_locale() has the same problem in reverse.

Steps to reproduce, on a site in English with de_DE installed:

<?php
php
echo wptexturize( '"Hello"' );   // “Hello”
switch_to_locale( 'de_DE' );
echo wptexturize( '"Hallo"' );   // “Hallo”, expected „Hallo“
restore_previous_locale();

The only way to rebuild the tables today is the $reset argument, which is documented as "for unit testing".

Proposed fix: rebuild the tables when the change_locale action has fired since they were built. switch_to_locale(), restore_previous_locale(), and restore_current_locale() all fire it, after the new translations are loaded. The check compares did_action( 'change_locale' ) with the count recorded at the last build, so it costs about 0.08 µs per call on PHP 8.4, and output for a single-locale request is unchanged. The run_wptexturize filter still runs once, not on every switch.

This was split out of the inline-quote work on #18549 (PR 12249), at the reviewers' request.

PR: ​https://github.com/WordPress/wordpress-develop/pull/16076

Change History (2)

This ticket was mentioned in ​PR #16076 on ​WordPress/wordpress-develop by ​@dpknauss.


3 hours ago
#1

  • Keywords has-patch added

wptexturize() builds its quote, apostrophe, and dash replacements once per request and keeps them in static variables. After switch_to_locale(), every later call still uses the first locale's punctuation, so German text rendered from an English site gets “Hallo” instead of „Hallo“.

This rebuilds the tables when the change_locale action has fired since they were built. switch_to_locale(), restore_previous_locale(), and restore_current_locale() all fire it after the new translations load. The run_wptexturize filter still runs only once, as its documentation says.

Split out of #12249 at the reviewers' request; it does not depend on that PR.

Cost

The check compares did_action( 'change_locale' ) with the count recorded at the last build, instead of calling get_locale() on every call.

Measured with the harness in #16075 against trunk at 567ce64051 (Mac mini, PHP 8.4.27, OPcache on, three runs):

Inputs Trunk median This PR Ratio
whole-posts.php (3) 167 µs 167 µs 1.00
Real posts and pages (32) 58 µs 58 µs 1.00
Post titles (32) 1.8 µs 1.9 µs 1.05

The fixed cost is about 0.08 µs per call (did_action() alone measures 41 ns), which shows only on very short strings. Calling get_locale() per call instead would cost about 0.83 µs. Output matches trunk on every input.

Testing

  • test_quote_tables_follow_locale_switch() switches to de_DE and back. It fails on trunk and passes here.
  • test_locale_switch_does_not_rerun_run_wptexturize_filter() checks that the filter runs once across switches.
  • The formatting group passes on single site and multisite (2,268 tests), as do the l10n and i18n groups. PHPCS and PHPStan report no errors on formatting.php.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Claude wrote the patch, tests, and measurements following the approach discussed on #12249. I reviewed the code and the results.

---
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See ​GitHub Pull Requests for Code Review in the Core Handbook for more details.

🤖 Generated with ​Claude Code

#2 @dpknauss
3 hours ago

  • Description modified (diff)
Note: See TracTickets for help on using tickets.