Skip to main content
1 of 4
Adrian Cid Almaguer
  • 18.5k
  • 14
  • 79
  • 133
You can do it with this function.

function getCode($find) {
  // Getting all the languages.
  $languages = \Drupal::service('language_manager')->getStandardLanguageList();
  $code = NULL;
  
  foreach ($languages as $code => $names) {
    for($i = 0; $i < count($names); $i++) {
            if ($names[$i] == $find) {
        return $code;
        }   
    }
  }
  return $code;
}  

// Language to search.
$find = 'French';
// Calling the function.
$code = getCode($find);
// If you have the devel module installed you can use the dpm function.
dpm($code);
Adrian Cid Almaguer
  • 18.5k
  • 14
  • 79
  • 133