Correctly load JS translations from WC core (II) (https://github.com/woocommerce/woocommerce-blocks/pull/6036)

* Correctly load JS translations from WC core (II)

* typo

* empty instead of count

Co-authored-by: Mike Jolley <mike.jolley@me.com>
This commit is contained in:
Albert Juhé Lluveras 2022-03-11 15:18:45 +01:00 committed by GitHub
parent 8e057a03f5
commit 0819ab2797
1 changed files with 16 additions and 7 deletions

View File

@ -204,13 +204,22 @@ function woocommerce_blocks_get_i18n_data_json( $translations, $file, $handle, $
} )( "{$domain}", {$json_translations} );
JS;
// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NoExplicitVersion
wp_register_script( $handle_filename, '', array( 'wp-i18n' ), false, true );
wp_enqueue_script( $handle_filename );
wp_add_inline_script(
$handle_filename,
$output
);
if ( empty( $wp_scripts->done ) ) {
// If we hadn't printed any script into the page, let's enqueue the translations.
// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NoExplicitVersion
wp_register_script( $handle_filename, '', array( 'wp-i18n' ), false, true );
wp_enqueue_script( $handle_filename );
wp_add_inline_script(
$handle_filename,
$output
);
} else {
// If we have already printed scripts into the page, there is a chance that
// scripts have finished being printed. That means that if we enqueued them here,
// they would never be printed. Instead of enqueuing, then, let's print directly
// the script tag.
printf( "<script type='text/javascript'>\n%s\n</script>\n", $output ); // phpcs:ignore
}
// Finally, short circuit the pre_load_script_translations hook by returning
// the translation JSON from the feature plugin, if it exists so this hook