Jun 11, 2014

Which t() function to call


In Drupal 8 calling the global t() function within a class context seems strongly deprecated, although it still works. Instead, use
$this->t()
My understanding of the rationale is that calling the class method provides better encapsulation and testability, and it also allows for the possibility of invoking a different version of t() other than whatever global default there is.

In non-class contexts, such as within functions defined in the .install script, use the global t() as in D7.

The following article seems to imply that the use of  $this->t() is either required or a really good idea if you want to enable unit testing. The article is mainly about how D8 core should implement  t() as a class method and has an extremely long thread of comments by core maintainers.

Provide a trait for $this->t() and $this->formatPlural()
https://drupal.org/node/2079797

No comments:

Post a Comment