Make WordPress Core


Ignore:
Timestamp:
10/25/2018 01:59:51 PM (6 years ago)
Author:
herregroen
Message:

I18N: Add JavaScript translation support.

Adds the wp_set_script_translations function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.

Props atimmer, omarreiss, nerrad, swissspidy, ocean90.
Fixes #45103.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0/src/wp-includes/class.wp-scripts.php

    r43788 r43825  
    479479
    480480    /**
     481     * Register a translation textdomain.
     482     *
     483     * @since 5.0.0
     484     *
     485     * @param string $handle Name of the script to register a translation domain to.
     486     * @param string $domain The textdomain.
     487     * @param string $path   Optional. The full file path to the directory containing translation files.
     488     *
     489     * @return bool True if the textdomain was registered, false if not.
     490     */
     491    public function set_translations( $handle, $domain, $path = null ) {
     492        if ( ! isset( $this->registered[ $handle ] ) ) {
     493            return false;
     494        }
     495
     496        $json_translations = load_script_textdomain( $handle, $domain, $path );
     497
     498        if ( ! $json_translations ) {
     499            return false;
     500        }
     501
     502        /** @var \_WP_Dependency $obj */
     503        $obj = $this->registered[ $handle ];
     504        $obj->deps[] = 'wp-i18n';
     505
     506        return $this->add_inline_script(
     507            $handle,
     508            '(function( translations ){' .
     509                'wp.i18n.setLocaleData( translations.locale_data, "' . $domain . '" );' .
     510            '})(' . $json_translations . ');',
     511            'before'
     512        );
     513    }
     514
     515    /**
    481516     * Determines script dependencies.
    482517     *
Note: See TracChangeset for help on using the changeset viewer.