Skip to:
Content

BuddyPress.org

Changeset 11191


Ignore:
Timestamp:
10/12/2016 11:31:54 PM (8 years ago)
Author:
r-a-y
Message:

Core: Add moment.js locale files and load appropriate locale file if found.

This commit adds all the locale files for moment.js and tries to load
the appropriate locale file based off of the current WordPress locale.

This is so sites that do not use U.S. English will see the correct
localized version of moment.js.

Developers can filter 'bp_core_register_common_scripts' by checking the
'bp-moment-locale' array key to change the locale file if desired.

Props r-a-y, slaFFik.

Fixes #7275.

Location:
trunk/src/bp-core
Files:
105 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-cssjs.php

    r11190 r11191  
    2020    $url = buddypress()->plugin_url . 'bp-core/js/';
    2121
    22     /**
    23      * Filters the BuddyPress Core javascript files to register.
    24      *
    25      * @since 2.1.0
    26      *
    27      * @param array $value Array of javascript file information to register.
    28      */
    29     $scripts = apply_filters( 'bp_core_register_common_scripts', array(
    30 
     22    /*
     23     * Moment.js locale.
     24     *
     25     * Try to map current WordPress locale to a moment.js locale file for loading.
     26     *
     27     * eg. French (France) locale for WP is fr_FR. Here, we try to find fr-fr.js
     28     *     (this file doesn't exist).
     29     */
     30    $locale = sanitize_file_name( strtolower( get_locale() ) );
     31    $locale = str_replace( '_', '-', $locale );
     32    if ( file_exists( buddypress()->core->path . "bp-core/js/vendor/moment-js/locale/{$locale}{$min}.js" ) ) {
     33        $moment_locale_url = $url . "vendor/moment-js/locale/{$locale}{$min}.js";
     34
     35    /*
     36     * Try to find the short-form locale.
     37     *
     38     * eg. French (France) locale for WP is fr_FR. Here, we try to find fr.js
     39     *     (this exists).
     40     */
     41    } else {
     42        $locale = substr( $locale, 0, strpos( $locale, '-' ) );
     43        if ( file_exists( buddypress()->core->path . "bp-core/js/vendor/moment-js/locale/{$locale}{$min}.js" ) ) {
     44            $moment_locale_url = $url . "vendor/moment-js/locale/{$locale}{$min}.js";
     45        }
     46    }
     47
     48    // Set up default scripts to register.
     49    $scripts = array(
    3150        // Legacy.
    3251        'bp-confirm'        => array( 'file' => "{$url}confirm{$min}.js", 'dependencies' => array( 'jquery' ), 'footer' => false ),
     
    5170        'bp-moment'    => array( 'file' => "{$url}vendor/moment-js/moment{$min}.js", 'dependencies' => array(), 'footer' => true ),
    5271        'bp-livestamp' => array( 'file' => "{$url}vendor/livestamp{$min}.js", 'dependencies' => array( 'jquery', 'bp-moment' ), 'footer' => true ),
    53     ) );
     72    );
     73
     74    // Version 2.7 - Add Moment.js locale to our $scripts array if we found one.
     75    if ( isset( $moment_locale_url ) ) {
     76        $scripts['bp-moment-locale'] = array( 'file' => esc_url( $moment_locale_url ), 'dependencies' => array( 'bp-moment' ), 'footer' => true );
     77    }
     78
     79    /**
     80     * Filters the BuddyPress Core javascript files to register.
     81     *
     82     * Default handles include 'bp-confirm', 'bp-widget-members',
     83     * 'bp-jquery-query', 'bp-jquery-cookie', and 'bp-jquery-scroll-to'.
     84     *
     85     * @since 2.1.0 'jquery-caret', 'jquery-atwho' added.
     86     * @since 2.3.0 'bp-plupload', 'bp-avatar', 'bp-webcam' added.
     87     * @since 2.4.0 'bp-cover-image' added.
     88     * @since 2.7.0 'bp-moment', 'bp-livestamp' added.
     89     *              'bp-moment-locale' is added conditionally if a moment.js locale file is found.
     90     *
     91     * @param array $value Array of javascript file information to register.
     92     */
     93    $scripts = apply_filters( 'bp_core_register_common_scripts', $scripts );
     94
    5495
    5596    $version = bp_get_version();
     
    490531    }
    491532
     533    /*
     534     * Only enqueue Moment.js locale if we registered it in
     535     * bp_core_register_common_scripts().
     536     */
     537    if ( wp_script_is( 'bp-moment-locale', 'registered' ) ) {
     538        wp_enqueue_script( 'bp-moment-locale' );
     539
     540        if ( function_exists( 'wp_add_inline_script' ) ) {
     541            wp_add_inline_script ( 'bp-livestamp', bp_core_moment_js_config() );
     542        } else {
     543            add_action( 'wp_footer', '_bp_core_moment_js_config_footer', 20 );
     544        }
     545    }
     546
    492547    wp_enqueue_script( 'bp-livestamp' );
    493 
    494     // We're only localizing the relative time strings for moment.js since that's all we need for now.
    495     wp_localize_script( 'bp-livestamp', 'BP_Moment_i18n', array(
    496         'future' => __( 'in %s',         'buddypress' ),
    497         'past'   => __( '%s ago',        'buddypress' ),
    498         's'      => __( 'a few seconds', 'buddypress' ),
    499         'm'      => __( 'a minute',      'buddypress' ),
    500         'mm'     => __( '%d minutes',    'buddypress' ),
    501         'h'      => __( 'an hour',       'buddypress' ),
    502         'hh'     => __( '%d hours',      'buddypress' ),
    503         'd'      => __( 'a day',         'buddypress' ),
    504         'dd'     => __( '%d days',       'buddypress' ),
    505         'M'      => __( 'a month',       'buddypress' ),
    506         'MM'     => __( '%d months',     'buddypress' ),
    507         'y'      => __( 'a year',        'buddypress' ),
    508         'yy'     => __( '%d years',      'buddypress' ),
    509     ) );
    510 
    511     if ( function_exists( 'wp_add_inline_script' ) ) {
    512         wp_add_inline_script ( 'bp-livestamp', bp_core_moment_js_config() );
    513     } else {
    514         add_action( 'wp_footer', '_bp_core_moment_js_config_footer', 20 );
    515     }
    516548}
    517549
     
    524556 */
    525557function bp_core_moment_js_config() {
     558    // Grab the locale from the enqueued JS.
     559    $moment_locale = wp_scripts()->query( 'bp-moment-locale' );
     560    $moment_locale = substr( $moment_locale->src, strpos( $moment_locale->src, '/moment-js/locale/' ) + 18 );
     561    $moment_locale = str_replace( '.js', '', $moment_locale );
     562
    526563    $inline_js = <<<EOD
    527564jQuery(function() {
    528     moment.locale( 'bp', {
    529         relativeTime : BP_Moment_i18n
    530     });
     565    moment.locale( '{$moment_locale}' );
    531566});
    532567EOD;
     
    545580 */
    546581function _bp_core_moment_js_config_footer() {
    547     if ( ! wp_script_is( 'bp-livestamp' ) ) {
     582    if ( ! wp_script_is( 'bp-moment-locale' ) ) {
    548583        return;
    549584    }
Note: See TracChangeset for help on using the changeset viewer.