3

I've encountered MANY methods substituting the translation function — forms, entities, plugins... What is the point when there is the global t() function that has exactly the same code

return \Drupal::translation()->translate($string, $args, $options);

as the methods do?

2 Answers 2

7

The point is unit testing. You can extend the class and override t() or just create a mock out of it. Global functions are not available during PHPUnit runs and in general just a pain.

Since this became so popular let me link the relevant handbook page on global functions and unit tests.

2

On the longrun we want to create a trait which contains exactly the t() method and getTranslationManager() to make it as easy as possible to use that.

This sadly requires php 5.4, before it is usable.

Just in case someone wonders why we not just call the translate method: Using t() allows tools like potx to extract the strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.