[wp-trac] [WordPress Trac] #45488: wp_set_script_translations does not load md5-based PO files

WordPress Trac noreply at wordpress.org
Wed Dec 5 13:02:12 UTC 2018


#45488: wp_set_script_translations does not load md5-based PO files
--------------------------+-----------------------------
 Reporter:  ettoredn      |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  I18N          |    Version:  5.0
 Severity:  blocker       |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 Given js/myscript.js used in a WordPress theme:
 {{{
 const { __, _x, _n, _nx } = wp.i18n;
 console.debug(__('Translatable string', 'tctextdomain'));
 }}}

 use the following steps to reproduce the issue:

 1. register a script
 {{{#!php
 <?php
 wp_register_script('myscript', get_theme_file_path('js/myscript.js'));
 }}}



 2. generate POT and PO files via wp-cli, in particular let **wp-cli i18n
 make-json** generate a PO file having a **md5-based filename** e.g. ;

 3. in my case the generated JSON file with the localized strings extracted
 from the PO is to be found in <my theme>/languages/it_IT-<md5 of
 myscript.js contents>.json with the following content
 {{{
 {"translation-revision-date":"2018-12-05 11:51+0000","generator":"WP-
 CLI\/2.1.0-alpha","domain":"messages","locale_data":{"messages":{"":{"domain":"messages","lang":"it_IT
 ","plural-forms":"nplurals=2; plural=n != 1;"},"Translatable
 string":["Stringa traducibile da JavaScript!"]}}}
 }}}

 3. set the script as translatable (which automatically adds wp-i18n
 dependency)
 {{{#!php
 <?php
 wp_set_script_translations('myscript', 'tctextdomain',
 get_theme_file_path('languages'));
 }}}

 4. enqueue the script wp_enqueue_script('myscript')

 The string won't be localized because WP does not output translations for
 the script in wp.i18n. This is because wp-
 includes/l10n.php::load_script_textdomain() returns false in class.wp-
 scripts.php::print_translations():528. This happend because for some
 counterintuitive reason the md5-based filename check is executed after the
 check for relative source which returns false.

 Solution: move the md5-based filename check
 {{{#!php
 <?php
         $md5_filename = $file_base . '-' . md5( $relative ) . '.json';
         if ( $path && file_exists( $path . '/' . $md5_filename ) ) {
                 return file_get_contents( $path . '/' . $md5_filename );
         }
         if ( file_exists( $languages_path . '/' . $md5_filename ) ) {
                 return file_get_contents( $languages_path . '/' .
 $md5_filename );
         }
 }}}
 before load_script_textdomain():913, possible right after the check for
 handle-based filename existence at load_script_textdomain():897.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/45488>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list