Content entities and the translation API
So far in this chapter, we’ve mostly talked about how to ensure that our modules only output text that can also be translated. The Drupal best practice is to always use these techniques regardless of whether the site is multilingual. You never know whether you’ll ever need to add a new language.
In this section, we are going to talk a bit about how we can interact with the language system programmatically and work with entity translations.
A potentially important thing you’ll often want to do is check the current language of the site. Depending on the language negotiation in place, this can either be determined by the browser language, a domain, a URL prefix, or something else. The LanguageManager
is the service we use to figure this out. We can inject it using the language_manager
key or use it via the static shorthand:
$manager = \Drupal::languageManager();
To get the current language, we do this:
$language...