1

In Drupal 7 I used drupal_multilingual() to know if a site is multilingual, but this function was renamed in Drupal 8 to language_multilingual(), the problem is that it seems that language_multilingual() is not longer available.

I found that LanguageManager::isMultilingual should return:

bool TRUE if more than one language is added, FALSE otherwise.

But when you go to see the code always FALSE is returned.

public function isMultilingual() {
  return FALSE;
}

This is an error or I missing something?

1

The default LanguageManager always returns false when you check for multilingual, because you need to install the Language module to configure a multilingual site. This module provides a proper check for multilingual by replacing the class LanguageManager with ConfigurableLanguageManager:

Drupal\language\LanguageServiceProvider::alter

  public function alter(ContainerBuilder $container) {
    $definition = $container->getDefinition('language_manager');
    $definition->setClass('Drupal\language\ConfigurableLanguageManager')
    ...

Drupal\language\ConfigurableLanguageManager::isMultilingual()

public function isMultilingual() {
    return count($this->getLanguages(LanguageInterface::STATE_CONFIGURABLE)) > 1;
  }

So you can use the service language_manager, injected in a class or from the static service container wrapper Drupal:

Drupal::languageManager()

  /**
   * Returns the language manager service.
   *
   * @return \Drupal\Core\Language\LanguageManagerInterface
   *   The language manager.
   */
  public static function languageManager() {
    return static::getContainer()->get('language_manager');
  }

to use in all cases the correct class:

\Drupal::languageManager()->isMultilingual()

2

Searching I found in drupalcontrib a reference to the language_multilingual() function that show the following code:

function language_multilingual() {
  return Drupal::languageManager()->isMultilingual();
}

So I used Drupal::languageManager()->isMultilingual(); and it seems that returns the good value.

I'll do tomorrow a test in a not multilingual site to confirm.

  • Yes, this is correct. – Berdir May 4 '17 at 7:24
2

The LanguageManager class is the wrong class to use, since it is defined as follows.

Class responsible for providing language support on language-unaware sites.

Given that description, and the fact there is a service using that class, the correct code should instantiate a service, which is what the code in install.core.inc does. (See install_tasks().)

  // Determine whether a translation file must be imported during the
  // 'install_import_translations' task. Import when a non-English language is
  // available and selected. Also we will need translations even if the
  // installer language is English but there are other languages on the system.
  $needs_translations = (count($install_state['translations']) > 1 && !empty($install_state['parameters']['langcode']) && $install_state['parameters']['langcode'] != 'en') || \Drupal::languageManager()->isMultilingual();

So, the correct code to use in procedural code is the following.

$is_multi_lingual = \Drupal::languageManager()->isMultilingual();

When using a class, and when it is possible, the service should be injected, and it instance saved in a object property (e.g. $this->languageManager). In that case, the code is the following.

$is_multi_lingual = $this->languageManager->isMultilingual();

The service ID for the language manager is language_manager.

  • Both answers are correct. When installing the module Language the class is switched to ConfigurableLanguageManager in the service container. So the code from both answers always uses the correct class. – 4k4 May 4 '17 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.