[wp-trac] [WordPress Trac] #41305: Add lazily evaluated translations

WordPress Trac noreply at wordpress.org
Fri Jul 14 01:40:57 UTC 2017


#41305: Add lazily evaluated translations
-------------------------+------------------------------------
 Reporter:  schlessera   |       Owner:
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  I18N         |     Version:  4.8
 Severity:  normal       |  Resolution:
 Keywords:  has-patch    |     Focuses:  rest-api, performance
-------------------------+------------------------------------
Changes (by rmccue):

 * focuses:  performance => rest-api, performance


Comment:

 Rather than being too clever with objects, we could potentially just
 introduce `_noop()` and `translate_noop` functions instead, to match the
 existing `_n_noop` and `translate_nooped_plural`. The `__noop()` is just a
 static analysis hint, and the actual translation can be deferred until
 later; we can implement this for the API (the only place we really need
 it) pretty easily, it just requires us to add the functions to the static
 analysis.

 This would look something like:
 {{{
 public function get_item_schema() {
         // ...
         'translation_domain' => '',
         'properties' => array(
                 // ...
                 'description' => _noop( "The date the object was
 published, in the site's timezone." ),
         )
         // ...
 }
 }}}

 Later, we then call `translate_noop( $description, $translation_domain )`
 when needed.

 This is less surprising to consumers of the function, since it's still
 actually a string. One of the problems with PHP is that string-like
 objects aren't equivalent to first-class strings, so introducing an object
 with `__toString()` might introduce problems.

 In an ideal world, we would have implemented the API so that things like
 this could either be a string or a function, allowing us to defer the
 actual runtime of translation:
 {{{
 'description' => function () {
         return __( "The date the object was published, in the site's
 timezone." );
 }
 }}}

 However PHP doesn't fit well with this pattern generally, and we also
 don't have proper closures in PHP 5.2, so we can't really do this.

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


More information about the wp-trac mailing list