⚠️ Load later for i18n (Enhance i18n of attendees-email...)#752
⚠️ Load later for i18n (Enhance i18n of attendees-email...)#752carstingaxion wants to merge 16 commits intoGatherPress:mainfrom
Conversation
This is important for users how selected a different from the sites default language.
PR Summary
|
…e GatherPress filters for date- and time- format, as well as the users timezone, are recognized by the functions inside render_template().
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as resolved.
This comment was marked as resolved.
| * A call to load_plugin_textdomain() is ABSOLUTELY NEEDED to properly load all translation files | ||
| * for users who chosed another than the sites default language in their /profile.php. | ||
| */ | ||
| public function load_gatherpress_textdomain() { |
There was a problem hiding this comment.
You really don't need this. It's pointless and only has downsides. And saying "A call to load_plugin_textdomain() is ABSOLUTELY NEEDED" is purely WRONG.
The just-in-time translation loading in WordPress handles everything for you and only loads the translations when needed.
There was a problem hiding this comment.
Hmm... a little bit strange.
I absolutely trust you in translation stuff, because I know you have been advocating and coding a lot for that topic.
From my long doc-comment you can imagine that I read & tried a lot to come up with my solution and the UPPERCASE comment. But unfortunately I can not make this work, without that line of code. Or maybe I have the wrong expectations.
How to test
-
Set site language to English in
wp-admin/options-general.php -
Set user prefered language to German in
wp-admin/profile.php -
Comment out
load_plugin_textdomain( 'gatherpress', false, false ) -
Check that the Admin-UI is German now, except everything GatherPress
-
Re-Enable
load_plugin_textdomain( 'gatherpress', false, false ) -
Check that the Admin-UI is German now, including everything GatherPress
There was a problem hiding this comment.
The issue is that you are triggering just-in-time translation loading too early.
WordPress tries to load the translations automatically as soon as you call __(). In your case, this happens somewhere before the current user is loaded, thus the admin won't be in German.
With this manual load_plugin_textdomain() call you're just papering over this issue, causing WP to try to load the translations twice.
The proper fix is to initialize your plugin later. This in the main plugin file works:
add_action(
'plugins_loaded',
static function () {
// Initialize setups.
GatherPress\Core\Setup::get_instance();
}
);
// Or simply:
add_action(
'plugins_loaded',
array( '\GatherPress\Core\Setup', 'get_instance' )
);
There was a problem hiding this comment.
Ok, thank you very much Pascal, once again your explanation is better than any docs (I know) about that.
I followed your advise:
- Set
add_action( 'plugins_loaded', array( '\GatherPress\Core\Setup', 'get_instance' )); - Disabled my attempt
// add_action( 'init', array( $this, 'load_gatherpress_textdomain' ), 0 ); - And started looking for the anoying bug again
- and was able to track it down until
Assets->localize(), which calls
'timezoneChoices' => Utility::timezone_choices(),and seems to be the culprit. Disabling this line brings all translations back into shape; having this enabled messes things up. - Going on ...
There was a problem hiding this comment.
Hello @mauteri ,
do we really need all possible timezones
- in the frontend markup?
- in the backend markup?
- on every page load?
There was a problem hiding this comment.
Isn't Assets::localize() only called in Assets::add_global_object() which is hooked to wp_head? So that should be late enough.
Aside: just adding a script tag like this is usually not a good idea. You should at least use wp_print_inline_script_tag. Even better, attach it to a specific script with wp_add_inline_script.
There was a problem hiding this comment.
Bildschirmaufzeichnung.vom.05.09.2024.16.08.52.mp4
There was a problem hiding this comment.
Found it: 🎉
$timezones_raw = explode( PHP_EOL, wp_timezone_choice( 'UTC', get_user_locale() ) ); // i18n FIX!There was a problem hiding this comment.
As expected, a change like to the load order will break things. At least the unit tests.
There was a problem hiding this comment.
Thanks for taking the time again @swissspidy !
Isn't
Assets::localize()only called inAssets::add_global_object()which is hooked towp_head? So that should be late enough.
And on admin_print_scripts, which is the problematic trace.
Aside: just adding a script tag like this is usually not a good idea. You should at least use
wp_print_inline_script_tag. Even better, attach it to a specific script withwp_add_inline_script.
👍 I wanted to suggest the same, even that the relevant data - the timezones, in this case - are only used for the Event Date Block (#684)
Preview changes with PlaygroundYou can preview the least recent changes for PR#752 by following one of the links below: |
|
This PR was split into multiple different PRs to make testing, understanding etc. easier. |


Description of the Change
Originally planned to only close #651, some additional findings are solved with this PR, too:
This is important for users who selected a different from the sites default language.
render_template().How to test the Change
Testing this PR might be a little tricky and is not possible with WordPress Playground because of the necessary sending & receiving of emails.
Preparation to test:
profile.phpTesting this PR:
Changelog Entry
Credits
Props @carstingaxion
Checklist: