[wp-trac] [WordPress Trac] #45103: Automatically load JavaScript translations when scripts are enqueued if these exist.

WordPress Trac noreply at wordpress.org
Thu Oct 18 12:03:02 UTC 2018


#45103: Automatically load JavaScript translations when scripts are enqueued if
these exist.
-------------------------+-------------------------
 Reporter:  herregroen   |       Owner:  (none)
     Type:  enhancement  |      Status:  new
 Priority:  high         |   Milestone:  5.0
Component:  I18N         |     Version:  trunk
 Severity:  critical     |  Resolution:
 Keywords:  needs-patch  |     Focuses:  javascript
-------------------------+-------------------------

Comment (by nerrad):

 I'm wondering if we should introduce an api for scripts to indicate they
 have translation strings.  It be nice if we could put extra arguments on
 `wp_register_script` but that already has a lot of arguments and retooling
 could be painful.

 Instead what about something like:

 {{{
 function wp_script_has_translation( $handle, $domain ) {
     $wp_scripts = wp_scripts();
     if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
         return false;
     }
     return $wp_scripts->has_translation( $handle, $domain );
 }
 }}}

 Then in `WP_Scripts` there could be tracking of what handles have been
 registered as having translation strings for when time comes to write
 `wp.i18n.setLocaleData` inline.  The beauty of this is is saves having to
 auto-detect whether js files have translations or not (which could get
 expensive) and also ensures that the translation is only loaded when the
 script itself is enqueued.

 So the new api bits would be the function `wp_script_has_translation` and
 the logic etc for `WP_Scripts::has_translation`

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/45103#comment:6>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list