You are here

public function ConfigNamesMapper::getLangcode in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/config_translation/src/ConfigNamesMapper.php \Drupal\config_translation\ConfigNamesMapper::getLangcode()

Returns the original language code of the configuration.

Throws

\RuntimeException Throws an exception if the language codes in the config files don't match.

Overrides ConfigMapperInterface::getLangcode

File

core/modules/config_translation/src/ConfigNamesMapper.php, line 387
Contains \Drupal\config_translation\ConfigNamesMapper.

Class

ConfigNamesMapper
Configuration mapper base implementation.

Namespace

Drupal\config_translation

Code

public function getLangcode() {
  $config_factory = $this->configFactory;
  $langcodes = array_map(function ($name) use ($config_factory) {

    // Default to English if no language code was provided in the file.
    // Although it is a best practice to include a language code, if the
    // developer did not think about a multilingual use-case, we fall back
    // on assuming the file is English.
    return $config_factory
      ->get($name)
      ->get('langcode') ?: 'en';
  }, $this
    ->getConfigNames());
  if (count(array_unique($langcodes)) > 1) {
    throw new \RuntimeException('A config mapper can only contain configuration for a single language.');
  }
  return reset($langcodes);
}