LLMS_Assets::set_script_translations( array $script )

Load JSON format localization files for a registered script


Description Description

This method mimics the behavior of PO/MO pot files loaded for PHP localization.

Language files can be found in the following locations (The first loaded file takes priority):

  1. wp-content/languages/{$textdomain}/{$textdomain}-{$locale}-{$file_md5_hash}.json

    This is recommended "safe" location where custom language files can be stored. A file stored in this directory will never be automatically overwritten.

  2. wp-content/languages/plugins/{$textdomain}-{$locale}-{$file_md5_hash}.json

    This is the default directory where WordPress will download language files from the WordPress GlotPress server during updates. If you store a custom language file in this directory it will be overwritten during updates.

  3. wp-content/plugins/{$textdomain}/languages/{$textdomain}-{$locale}-{$file_md5_hash}.json

    This is the the LifterLMS plugin directory. A language file stored in this directory will be removed from the server during a LifterLMS plugin update.


Top ↑

Parameters Parameters

$script

(array) (Required) An asset definition array from the return of LLMS_Assets::get().


Top ↑

Return Return

(void)


Top ↑

Source Source

File: includes/class-llms-assets.php

676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
* @return void
 */
protected function set_script_translations( $script ) {
 
    $plugin_data = get_plugin_data( $script['base_file'], false, false );
    $domain      = $plugin_data['TextDomain'];
 
    // Setup the script's filename based on the md5 of it's relative path.
    $relative_path = sprintf( '%1$s/%2$s%3$s', $script['path'], $script['file_name'], $script['extension'] );
    $file          = sprintf( '%1$s-%2$s-%3$s.json', $domain, llms_get_locale(), md5( $relative_path ) );
 
    // Possible directories where the language files may be found.
    $dirs = array(
        llms_l10n_get_safe_directory(),
        WP_LANG_DIR . '/plugins', // Default language directory.
        trailingslashit( plugin_dir_path( $script['base_file'] ) ) . untrailingslashit( ltrim( $plugin_data['DomainPath'], '/' ) ), // Language directory within the plugin.
    );
 
    foreach ( $dirs as $dir ) {
        // If the file exists, set the script translations.
        if ( file_exists( sprintf( '%1$s/%2$s', $dir, $file ) ) ) {
            wp_set_script_translations( $script['handle'], $domain, $dir );
            break;
        }
    }


Top ↑

Changelog Changelog

Changelog
Version Description
4.9.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.