Dashboard: Properly localize data for events

Moves localization to script-loader and removes dependency for two strings.

Props dd32, iandunn.
See #40702.

Built from https://develop.svn.wordpress.org/trunk@40776


git-svn-id: http://core.svn.wordpress.org/trunk@40634 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Konstantin Obenland 2017-05-18 14:34:43 +00:00
parent bb1207cf5a
commit 9bbbc719d4
6 changed files with 49 additions and 53 deletions

View File

@ -129,46 +129,6 @@ function wp_dashboard_setup() {
do_action( 'do_meta_boxes', $screen->id, 'side', '' );
}
/**
* Gets the community events data that needs to be passed to dashboard.js.
*
* @since 4.8.0
*
* @return array The script data.
*/
function wp_get_community_events_script_data() {
require_once( ABSPATH . 'wp-admin/includes/class-wp-community-events.php' );
$user_id = get_current_user_id();
$user_location = get_user_option( 'community-events-location', $user_id );
$events_client = new WP_Community_Events( $user_id, $user_location );
$script_data = array(
'nonce' => wp_create_nonce( 'community_events' ),
'cache' => $events_client->get_cached_events(),
'l10n' => array(
'enter_closest_city' => __( 'Enter your closest city to find nearby events.' ),
'error_occurred_please_try_again' => __( 'An error occured. Please try again.' ),
/*
* These specific examples were chosen to highlight the fact that a
* state is not needed, even for cities whose name is not unique.
* It would be too cumbersome to include that in the instructions
* to the user, so it's left as an implication.
*/
/* translators: %s is the name of the city we couldn't locate. Replace the examples with cities in your locale, but test that they match the expected location before including them. Use endonyms (native locale names) whenever possible. */
'could_not_locate_city' => __( "We couldn't locate %s. Please try another nearby city. For example: Kansas City; Springfield; Portland." ),
// This one is only used with wp.a11y.speak(), so it can/should be more brief.
/* translators: %s is the name of a city. */
'city_updated' => __( 'City updated. Listing events near %s.' ),
)
);
return $script_data;
}
/**
* Adds a new dashboard widget.
*
@ -1170,24 +1130,22 @@ function wp_dashboard_events_news() {
* @since 4.8.0
*/
function wp_print_community_events_markup() {
$script_data = wp_get_community_events_script_data();
?>
<div class="community-events-errors notice notice-error inline hide-if-js">
<p class="hide-if-js">
<?php _e( 'This widget requires JavaScript.'); ?>
<?php _e( 'This widget requires JavaScript.' ); ?>
</p>
<p class="community-events-error-occurred" aria-hidden="true">
<?php echo $script_data['l10n']['error_occurred_please_try_again']; ?>
<?php _e( 'An error occurred. Please try again.' ); ?>
</p>
<p class="community-events-could-not-locate" aria-hidden="true"></p>
</div>
<div class="community-events-loading hide-if-no-js">
<?php _e( 'Loading&hellip;'); ?>
<?php _e( 'Loading&hellip;' ); ?>
</div>
<?php
@ -1201,7 +1159,7 @@ function wp_print_community_events_markup() {
<p>
<span id="community-events-location-message"></span>
<button class="button-link community-events-toggle-location" aria-label="<?php _e( 'Edit city'); ?>" aria-expanded="false">
<button class="button-link community-events-toggle-location" aria-label="<?php esc_attr_e( 'Edit city' ); ?>" aria-expanded="false">
<span class="dashicons dashicons-edit"></span>
</button>
</p>
@ -1211,7 +1169,7 @@ function wp_print_community_events_markup() {
<?php _e( 'City:' ); ?>
</label>
<?php /* translators: Replace with the name of a city in your locale that shows events. Use only the city name itself, without any region or country. Use the endonym instead of the English name. */ ?>
<input id="community-events-location" class="regular-text" type="text" name="community-events-location" placeholder="<?php _e( 'Cincinnati' ); ?>" />
<input id="community-events-location" class="regular-text" type="text" name="community-events-location" placeholder="<?php esc_attr_e( 'Cincinnati' ); ?>" />
<?php submit_button( __( 'Submit' ), 'secondary', 'community-events-submit', false ); ?>
@ -1235,8 +1193,6 @@ function wp_print_community_events_markup() {
* @since 4.8.0
*/
function wp_print_community_events_templates() {
$script_data = wp_get_community_events_script_data();
?>
<script id="tmpl-community-events-attend-event-near" type="text/template">
@ -1249,7 +1205,8 @@ function wp_print_community_events_templates() {
<script id="tmpl-community-events-could-not-locate" type="text/template">
<?php printf(
$script_data['l10n']['could_not_locate_city'],
/* translators: %s is the name of the city we couldn't locate. Replace the examples with cities in your locale, but test that they match the expected location before including them. Use endonyms (native locale names) whenever possible. */
__( 'We couldn&#8217;t locate %s. Please try another nearby city. For example: Kansas City; Springfield; Portland.' ),
'<em>{{data.unknownCity}}</em>'
); ?>
</script>

View File

@ -15,7 +15,6 @@ require_once(ABSPATH . 'wp-admin/includes/dashboard.php');
wp_dashboard_setup();
wp_enqueue_script( 'dashboard' );
wp_localize_script( 'dashboard', 'communityEventsData', wp_get_community_events_script_data() );
if ( current_user_can( 'edit_theme_options' ) )
wp_enqueue_script( 'customize-loader' );

View File

@ -54,7 +54,6 @@ get_current_screen()->set_help_sidebar(
wp_dashboard_setup();
wp_enqueue_script( 'dashboard' );
wp_localize_script( 'dashboard', 'communityEventsData', wp_get_community_events_script_data() );
wp_enqueue_script( 'plugin-install' );
add_thickbox();

View File

@ -434,6 +434,7 @@ add_action( 'set_current_user', 'kses_init' );
add_action( 'wp_default_scripts', 'wp_default_scripts' );
add_action( 'wp_enqueue_scripts', 'wp_localize_jquery_ui_datepicker', 1000 );
add_action( 'admin_enqueue_scripts', 'wp_localize_jquery_ui_datepicker', 1000 );
add_action( 'admin_print_scripts-index.php', 'wp_localize_community_events' );
add_filter( 'wp_print_scripts', 'wp_just_in_time_script_localization' );
add_filter( 'print_scripts_array', 'wp_prototype_before_jquery' );
add_filter( 'customize_controls_print_styles', 'wp_resource_hints', 1 );

View File

@ -1000,6 +1000,46 @@ function wp_localize_jquery_ui_datepicker() {
wp_add_inline_script( 'jquery-ui-datepicker', "jQuery(document).ready(function(jQuery){jQuery.datepicker.setDefaults({$datepicker_defaults});});" );
}
/**
* Localizes community events data that needs to be passed to dashboard.js.
*
* @since 4.8.0
*/
function wp_localize_community_events() {
if ( ! wp_script_is( 'dashboard' ) ) {
return;
}
require_once( ABSPATH . 'wp-admin/includes/class-wp-community-events.php' );
$user_id = get_current_user_id();
$user_location = get_user_option( 'community-events-location', $user_id );
$events_client = new WP_Community_Events( $user_id, $user_location );
wp_localize_script( 'dashboard', 'communityEventsData', array(
'nonce' => wp_create_nonce( 'community_events' ),
'cache' => $events_client->get_cached_events(),
'l10n' => array(
'enter_closest_city' => __( 'Enter your closest city to find nearby events.' ),
'error_occurred_please_try_again' => __( 'An error occurred. Please try again.' ),
/*
* These specific examples were chosen to highlight the fact that a
* state is not needed, even for cities whose name is not unique.
* It would be too cumbersome to include that in the instructions
* to the user, so it's left as an implication.
*/
/* translators: %s is the name of the city we couldn't locate. Replace the examples with cities in your locale, but test that they match the expected location before including them. Use endonyms (native locale names) whenever possible. */
'could_not_locate_city' => __( 'We couldn&#8217;t locate %s. Please try another nearby city. For example: Kansas City; Springfield; Portland.' ),
// This one is only used with wp.a11y.speak(), so it can/should be more brief.
/* translators: %s is the name of a city. */
'city_updated' => __( 'City updated. Listing events near %s.' ),
)
) );
}
/**
* Administration Screen CSS for changing the styles.
*

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.8-beta1-40775';
$wp_version = '4.8-beta1-40776';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.