The load_script_translations() WordPress PHP function loads the translation data for a given script handle and text domain.
Usage
load_script_translations( $file, $handle, $domain );
Custom example:
load_script_translations( '/path/to/translations.po', 'my-script', 'my-text-domain' );
Parameters
$file (string|false)
: Path to the translation file to load. False if there isn’t one.$handle (string)
: Name of the script to register a translation domain to.$domain (string)
: The text domain.
More information
See WordPress Developer Resources: load_script_translations()
Examples
Load translations for a custom script
This example demonstrates how to load translations for a custom script called ‘my-script’ with the text domain ‘my-text-domain’.
function my_enqueue_scripts() { wp_enqueue_script( 'my-script', '/path/to/my-script.js', array(), '1.0.0', true ); $translations_path = '/path/to/translations.po'; load_script_translations( $translations_path, 'my-script', 'my-text-domain' ); } add_action( 'wp_enqueue_scripts', 'my_enqueue_scripts' );
Load translations for a plugin script
This example shows how to load translations for a script within a plugin directory.
function my_plugin_enqueue_scripts() { $plugin_url = plugin_dir_url( __FILE__ ); wp_enqueue_script( 'my-plugin-script', $plugin_url . 'js/my-plugin-script.js', array(), '1.0.0', true ); $translations_path = plugin_dir_path( __FILE__ ) . 'languages/translations.po'; load_script_translations( $translations_path, 'my-plugin-script', 'my-plugin-text-domain' ); } add_action( 'wp_enqueue_scripts', 'my_plugin_enqueue_scripts' );
Load translations for a theme script
This example demonstrates loading translations for a script within a theme directory.
function my_theme_enqueue_scripts() { $theme_url = get_template_directory_uri(); wp_enqueue_script( 'my-theme-script', $theme_url . '/js/my-theme-script.js', array(), '1.0.0', true ); $translations_path = get_template_directory() . '/languages/translations.po'; load_script_translations( $translations_path, 'my-theme-script', 'my-theme-text-domain' ); } add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_scripts' );
Load translations for a child theme script
This example shows how to load translations for a script within a child theme directory.
function my_child_theme_enqueue_scripts() { $child_theme_url = get_stylesheet_directory_uri(); wp_enqueue_script( 'my-child-theme-script', $child_theme_url . '/js/my-child-theme-script.js', array(), '1.0.0', true ); $translations_path = get_stylesheet_directory() . '/languages/translations.po'; load_script_translations( $translations_path, 'my-child-theme-script', 'my-child-theme-text-domain' ); } add_action( 'wp_enqueue_scripts', 'my_child_theme_enqueue_scripts' );
Load translations for an external script
This example demonstrates loading translations for an external script, assuming the translations are hosted on your server.
function my_external_script_enqueue_scripts() { wp_enqueue_script( 'my-external-script', 'https://example.com/external-script.js