Make WordPress Core


Ignore:
Timestamp:
12/14/2018 05:51:31 AM (6 years ago)
Author:
pento
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.

Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.

Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/wp-includes/class.wp-scripts.php

    r43583 r44169  
    331331        }
    332332
     333        $translations = $this->print_translations( $handle, false );
     334        if ( $translations ) {
     335            $translations = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $translations );
     336        }
     337
    333338        if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) {
    334339            $src = $this->base_url . $src;
     
    346351        }
    347352
    348         $tag = "{$cond_before}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}";
     353        $tag = "{$translations}{$cond_before}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}";
    349354
    350355        /**
     
    489494
    490495        return parent::set_group( $handle, $recursion, $grp );
     496    }
     497
     498    /**
     499     * Sets a translation textdomain.
     500     *
     501     * @since 5.0.0
     502     *
     503     * @param string $handle Name of the script to register a translation domain to.
     504     * @param string $domain The textdomain.
     505     * @param string $path   Optional. The full file path to the directory containing translation files.
     506     *
     507     * @return bool True if the textdomain was registered, false if not.
     508     */
     509    public function set_translations( $handle, $domain, $path = null ) {
     510        if ( ! isset( $this->registered[ $handle ] ) ) {
     511            return false;
     512        }
     513
     514        /** @var \_WP_Dependency $obj */
     515        $obj = $this->registered[ $handle ];
     516
     517        if ( ! in_array( 'wp-i18n', $obj->deps, true ) ) {
     518            $obj->deps[] = 'wp-i18n';
     519        }
     520        return $obj->set_translations( $domain, $path );
     521    }
     522
     523    /**
     524     * Prints translations set for a specific handle.
     525     *
     526     * @since 5.0.0
     527     *
     528     * @param string $handle Name of the script to add the inline script to. Must be lowercase.
     529     * @param bool   $echo   Optional. Whether to echo the script instead of just returning it.
     530     *                       Default true.
     531     * @return string|false Script on success, false otherwise.
     532     */
     533    public function print_translations( $handle, $echo = true ) {
     534        if ( ! isset( $this->registered[ $handle ] ) || empty( $this->registered[ $handle ]->textdomain ) ) {
     535            return false;
     536        }
     537
     538        $domain = $this->registered[ $handle ]->textdomain;
     539        $path   = $this->registered[ $handle ]->translations_path;
     540
     541        $json_translations = load_script_textdomain( $handle, $domain, $path );
     542
     543        if ( ! $json_translations ) {
     544            // Register empty locale data object to ensure the domain still exists.
     545            $json_translations = '{ "locale_data": { "messages": { "": {} } } }';
     546        }
     547
     548        $output = '(function( translations ){' .
     549                      'wp.i18n.setLocaleData( translations.locale_data, "' . $domain . '" );' .
     550                  '})(' . $json_translations . ');';
     551
     552        if ( $echo ) {
     553            printf( "<script type='text/javascript'>\n%s\n</script>\n", $output );
     554        }
     555
     556        return $output;
    491557    }
    492558
Note: See TracChangeset for help on using the changeset viewer.