#57123 closed enhancement (fixed)
Make it easier to switch to a user's locale
Reported by: | swissspidy | Owned by: | swissspidy |
---|---|---|---|
Milestone: | 6.2 | Priority: | normal |
Severity: | normal | Version: | 4.7 |
Component: | I18N | Keywords: | has-patch commit has-unit-tests has-dev-note |
Focuses: | Cc: |
Description
Locale switching was introduced in #26511 to allow doing things like sending emails in the recipient's locale instead of the site's locale.
That's why we see a lot of code like switch_to_locale( get_user_locale( $user ) )
in core and in plugins.
Not only is this very repetitive, it also causes limitations when used in combination with things like the Preferred Languages feature plugin, where one would like to fall back to another locale if the desired one is not available.
That's because the locale switcher lacks context about which user the locale is for.
To improve this, we could introduce a new switch_to_user_locale( $user )
function that a) grabs the user's locale and b) stores the user ID in the locale switcher stack so that at each moment in time we know whose locale we're supposed to be using.
Change History (18)
This ticket was mentioned in PR #3637 on WordPress/wordpress-develop by @swissspidy.
23 months ago
#1
- Keywords has-patch added; needs-patch removed
This ticket was mentioned in Slack in #core-i18n by swissspidy. View the logs.
21 months ago
#4
@
20 months ago
- Keywords changes-requested added
Just updating the ticket's keywords to reflect the current status of the PR.
@swissspidy commented on PR #3637:
20 months ago
#7
Committed in https://core.trac.wordpress.org/changeset/55161
#8
@
20 months ago
This is excellent!
I have a thought about the new method names (get_current_locale
, get_current_user_id
). Apologies for it being after-commit 😔 and for maybe digging up an old conversation…
Does _current_
convey “global state”, even when used within the confines of a class or Namespace?
Seems like existing global functions that start with get_current_
or restore_current_
established a pattern that “current” could be either “switched” or “originating” depending on the caller.
Is this, perhaps, a decent opportunity to establish a more intentional/explicit/strict naming pattern to differentiate things that are “currently switched” from “reset back to what we started with” ?
#9
@
20 months ago
Thanks for raising that. I see how it is confusing that e.g. ::restore_current_locale()
refers to the "original" locale and ::get_current_locale()
refers the the currently switched to locale.
Suggestion:
Rename ::get_current_locale()
to ::get_locale()
and ::get_current_user_id()
to ::get_user_id()
.
This way, the existing pattern in core ("current" referring to "originating") is unchanged.
This ticket was mentioned in PR #3952 on WordPress/wordpress-develop by @swissspidy.
20 months ago
#10
- Keywords has-unit-tests added
This is a follow-up to #3637 / https://core.trac.wordpress.org/changeset/55161 with the following changes:
- Fix docblock for
switch_locale
filter. The User ID isfalse
if missing, notnull
. Props @ocean90. - Rename
::get_current_locale()
to::get_locale()
and::get_current_user_id()
to::get_user_id()
. Props @JJJ. - Add additional test involving
restore_previous_locale()
as suggested by @ocean90.
Trac ticket: https://core.trac.wordpress.org/ticket/57123
This ticket was mentioned in Slack in #core by sergey. View the logs.
20 months ago
@swissspidy commented on PR #3952:
20 months ago
#13
This ticket was mentioned in PR #3999 on WordPress/wordpress-develop by @swissspidy.
20 months ago
#14
WP_Textdomain_Registry
: Use rtrim
instead of untrailingslashit
and trailingslashit
directly.
Avoids formatting.php
dependency and thus prevents errors when using wp_load_translations_early()
, for example when in maintenance mode.
Trac ticket: https://core.trac.wordpress.org/ticket/57123
@swissspidy commented on PR #3999:
20 months ago
#15
Let's add a short comment to the file header why both functions cannot be used here. Similar to
Fixed 👍
Trac ticket: https://core.trac.wordpress.org/ticket/57123