When you have content in custom code, it needs to be output using specific functions and other methods to ensure it can be translated through the administrative UI. How this is exposed depends on the manner it is embedded in the code.
Like Drupal 7, there are still the t()
and format_plural()
functions. However, the functionality behind these relies on the TranslationManager
class. This means that it can be dependency injected into any Drupal 8 services, controllers, plugins, and so on. This is the preferred mechanism for translating text in PHP, and many base plugin classes in Drupal already implement functions that delegate to the TranslationManager
.
One thing to be aware of is that the mechanism that discovers translatable strings relies on the functions being called, named t()
and formatPlural()
. So, while the actual method to translate content is TranslationManager::translate()
, the following will not be picked up by localize.drupal.org
:
$this->stringTranslation...