WordPress.org

Make WordPress Core


Ignore:
Timestamp:
03/06/2007 12:39:46 AM (14 years ago)
Author:
ryan
Message:

JS localization from mdawaffe. fixes #3911

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/script-loader.php

    r4948 r4968  
    2222        $this->add( 'autosave', '/wp-includes/js/autosave-js.php', array('prototype', 'sack'), '20070116');
    2323        $this->add( 'wp-ajax', '/wp-includes/js/wp-ajax-js.php', array('prototype'), '20070118');
    24         $this->add( 'listman', '/wp-includes/js/list-manipulation-js.php', array('wp-ajax', 'fat'), '20070118');
     24        $this->add( 'listman', '/wp-includes/js/list-manipulation.js', array('wp-ajax', 'fat'), '20070305');
     25        $this->localize( 'listman', 'listManL10n', array(
     26            'jumpText' => __('Jump to new item'),
     27            'delText' => __('Are you sure you want to delete this %s?')
     28        ) );
    2529        $this->add( 'scriptaculous-root', '/wp-includes/js/scriptaculous/wp-scriptaculous.js', array('prototype'), '1.7.0');
    2630        $this->add( 'scriptaculous-builder', '/wp-includes/js/scriptaculous/builder.js', array('scriptaculous-root'), '1.7.0');
     
    8488                    $src = apply_filters( 'script_loader_src', $src );
    8589                    echo "<script type='text/javascript' src='$src'></script>\n";
     90                    $this->print_scripts_l10n( $handle );
    8691                }
    8792                $this->printed[] = $handle;
     
    9095    }
    9196
     97    function print_scripts_l10n( $handle ) {
     98        if ( empty($this->scripts[$handle]->l10n_object) || empty($this->scripts[$handle]->l10n) || !is_array($this->scripts[$handle]->l10n) )
     99            return;
     100
     101        $object_name = $this->scripts[$handle]->l10n_object;
     102
     103        echo "<script type='text/javascript'>\n";
     104        echo "/* <![CDATA[ */\n";
     105        echo "\t$object_name = {\n";
     106        $eol = '';
     107        foreach ( $this->scripts[$handle]->l10n as $var => $val ) {
     108            echo "$eol\t\t$var: \"" . js_escape( $val ) . '"';
     109            $eol = ",\n";
     110        }
     111        echo "\n\t}\n";
     112        echo "/* ]]> */\n";
     113        echo "</script>\n";
     114    }
    92115
    93116    /**
     
    141164    }
    142165
     166    /**
     167     * Localizes a script
     168     *
     169     * Localizes only if script has already been added
     170     *
     171     * @param string handle Script name
     172     * @param string object_name Name of JS object to hold l10n info
     173     * @param array l10n Array of JS var name => localized string
     174     * @return bool Successful localization
     175     */
     176    function localize( $handle, $object_name, $l10n ) {
     177        if ( !isset($this->scripts[$handle]) )
     178            return false;
     179        return $this->scripts[$handle]->localize( $object_name, $l10n );
     180    }
     181
    143182    function remove( $handles ) {
    144183        foreach ( (array) $handles as $handle )
     
    183222    var $deps = array();
    184223    var $ver = false;
    185     var $args = false;
     224    var $l10n_object = '';
     225    var $l10n = array();
    186226
    187227    function _WP_Script() {
     
    192232            $this->ver = false;
    193233    }
     234
     235    function localize( $object_name, $l10n ) {
     236        if ( !$object_name || !is_array($l10n) )
     237            return false;
     238        $this->l10n_object = $object_name;
     239        $this->l10n = $l10n;
     240        return true;
     241    }
    194242}
    195243
     
    228276}
    229277
     278/**
     279 * Localizes a script
     280 *
     281 * Localizes only if script has already been added
     282 *
     283 * @see WP_Script::localize()
     284 */
     285function wp_localize_script( $handle, $object_name, $l10n ) {
     286    global $wp_scripts;
     287    if ( !is_a($wp_scripts, 'WP_Scripts') )
     288        return false;
     289
     290    return $wp_scripts->localize( $handle, $object_name, $l10n );
     291}
     292
    230293function wp_deregister_script( $handle ) {
    231294    global $wp_scripts;
Note: See TracChangeset for help on using the changeset viewer.