Skip to content

I18N: Simplify WP_Textdomain_Registry::has() - #7735

Open
swissspidy wants to merge 5 commits into
WordPress:trunkfrom
swissspidy:fix/62348-textdomain
Open

I18N: Simplify WP_Textdomain_Registry::has()#7735
swissspidy wants to merge 5 commits into
WordPress:trunkfrom
swissspidy:fix/62348-textdomain

Conversation

@swissspidy

@swissspidy swissspidy commented Nov 6, 2024

Copy link
Copy Markdown
Member

Started as an attempt to fix prefix matching in get_path_from_lang_dir(). That turned out not to be a real bug, but removing the machinery around it is one, so this ends up somewhere different than it began.

Translation files were matched by prefix:

str_starts_with( str_replace( "$location/", '', $file_path ), "$domain-" )

With do-something and do-something-else installed and only the latter translated, do-something really does end up in $domains_with_translations. That part reproduces.

It can't change behaviour, though. $domains_with_translations is read in exactly one place — the third condition of has() — and that condition is only reached when current[$domain] is unset while all[$domain] is non-empty. Since set() always assigns current, the only route into that state is set_custom_path().

Replaying every operation sequence up to length 3 over _load_textdomain_just_in_time(), load_textdomain(), load_plugin_textdomain() and load_theme_textdomain(), across 11 language-file layouts: the third condition is reached in 508 states, and in every one of them the text domain has translations of its own, prefix sibling or not. Making the match stricter changes nothing observable.

What this PR does

  • Removes the per-file scan from get_path_from_lang_dir().
  • Deprecates $domains_with_translations.
  • has() checks isset( $this->custom_paths[ $domain ] ) in its place.

The third condition is only reachable once a custom path has been registered, so the check is equivalent by construction — and it takes per-file work out of a loop that runs on every lookup, which is what the scan was costing in the first place.

Trac ticket: https://core.trac.wordpress.org/ticket/62348


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.

@swissspidy swissspidy changed the title I18N: Use regex in get_path_from_lang_dir for more accurate results [TRY] Simplify WP_Textdomain_Registry Nov 6, 2024
@github-actions

github-actions Bot commented Nov 6, 2024

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

swissspidy and others added 3 commits August 21, 2026 10:32
…ered.

Removing the `$domains_with_translations` property also removed the only
condition in `WP_Textdomain_Registry::has()` that covers the state
`set_custom_path()` leaves behind: `current` unset because the last lookup
missed, while `all` still holds a resolved path for another locale.

In that state `has()` started returning false, and because
`load_plugin_textdomain()` and `load_theme_textdomain()` only register a path
and hand the loading off to `_load_textdomain_just_in_time()`, the already
known translations stopped being loaded entirely.

That state is only reachable once a custom path has been registered, so check
for the custom path directly instead. This keeps the scan (and the deprecated
property) out of `get_path_from_lang_dir()` while restoring the previous
behaviour.

Adds tests covering `has()` for every state it can be asked about, including
text domains that merely share a prefix with a translated one, and an
end-to-end test for the locale-switch-then-`load_plugin_textdomain()` case.

See #62348.
@swissspidy
swissspidy force-pushed the fix/62348-textdomain branch from 8024eb9 to bfeaf3a Compare August 21, 2026 11:29
Comment thread tests/phpunit/tests/l10n/loadTextdomainJustInTime.php Outdated
@swissspidy swissspidy changed the title [TRY] Simplify WP_Textdomain_Registry I18N: Simplify WP_Textdomain_Registry::has() Aug 21, 2026
@swissspidy
swissspidy marked this pull request as ready for review August 21, 2026 12:01
Copilot AI lite review requested due to automatic review settings August 21, 2026 12:01

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @claude.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

Core Committers: Use this line as a base for the props when committing in SVN:

Props swissspidy.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants