Skip to content

Commit

Permalink
Switch locale when generating logs, rather than filtering the default…
Browse files Browse the repository at this point in the history
… locale (that's already loaded).
  • Loading branch information
dd32 authored Nov 25, 2024
1 parent 357c2b2 commit ae4db83
Showing 1 changed file with 4 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,19 +256,6 @@ public function metabox_status( $post ) {
*/
abstract public static function get_edit_capability();

/**
* Filter: Set the locale to en_US.
*
* For some purposes, such as internal logging, strings that would normally be translated to the
* current user's locale should be in English, so that other users who may not share the same
* locale can read them.
*
* @return string
*/
public function set_locale_to_en_us() {
return 'en_US';
}

/**
* Log when the post status changes
*
Expand All @@ -286,7 +273,7 @@ public function log_status_changes( $new_status, $old_status, $post ) {
}

// Ensure status labels are in English.
add_filter( 'locale', array( $this, 'set_locale_to_en_us' ) );
$locale_switched = switch_to_locale( 'en_US' );

$old_status = get_post_status_object( $old_status );
$new_status = get_post_status_object( $new_status );
Expand All @@ -308,7 +295,9 @@ public function log_status_changes( $new_status, $old_status, $post ) {
}

// Remove the temporary locale change.
remove_filter( 'locale', array( $this, 'set_locale_to_en_us' ) );
if ( $locale_switched ) {
restore_previous_locale();
}
}

/**
Expand Down

0 comments on commit ae4db83

Please sign in to comment.