Skip to main content
Add a simplified version that works on modern PHP versions.
Source Link

You can do it with this code.

// Language to search.
$find = 'French';

// Getting all the available languages.
$languages = \Drupal::service('language_manager')->getStandardLanguageList();

$key = key(array_filter($languages, function ($array) use ($find) {
  if (array_search($find, $array) === FALSE) {
    return FALSE;
  }
  return TRUE;
}));

// If you have the devel module installed you can use the dpm function.
dpm($key);

Or, if you are on PHP 7.4+:

// Language to search.
$find = 'French';

$languages = \Drupal::service('language_manager')::getStandardLanguageList();
$langcode = key(array_filter($languages, fn ($lang) => in_array($find, $lang)));

You can do it with this code.

// Language to search.
$find = 'French';

// Getting all the available languages.
$languages = \Drupal::service('language_manager')->getStandardLanguageList();

$key = key(array_filter($languages, function ($array) use ($find) {
  if (array_search($find, $array) === FALSE) {
    return FALSE;
  }
  return TRUE;
}));

// If you have the devel module installed you can use the dpm function.
dpm($key);

You can do it with this code.

// Language to search.
$find = 'French';

// Getting all the available languages.
$languages = \Drupal::service('language_manager')->getStandardLanguageList();

$key = key(array_filter($languages, function ($array) use ($find) {
  if (array_search($find, $array) === FALSE) {
    return FALSE;
  }
  return TRUE;
}));

// If you have the devel module installed you can use the dpm function.
dpm($key);

Or, if you are on PHP 7.4+:

// Language to search.
$find = 'French';

$languages = \Drupal::service('language_manager')::getStandardLanguageList();
$langcode = key(array_filter($languages, fn ($lang) => in_array($find, $lang)));
deleted 5 characters in body
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

You can do it with this code.

You can do it with this chode.
    
// Language to search.
$find = 'French';

// Getting all the available languages.
$languages = \Drupal::service('language_manager')->getStandardLanguageList();

$key = key(array_filter($languages, function ($array) use ($find) {
  if (array_search($find, $array) === FALSE) {
    return FALSE;
  }
  return TRUE;
}));

// If you have the devel module installed you can use the dpm function.
dpm($key);
You can do it with this chode.
    
// Language to search.
$find = 'French';

// Getting all the available languages.
$languages = \Drupal::service('language_manager')->getStandardLanguageList();

$key = key(array_filter($languages, function ($array) use ($find) {
  if (array_search($find, $array) === FALSE) {
    return FALSE;
  }
  return TRUE;
}));

// If you have the devel module installed you can use the dpm function.
dpm($key);

You can do it with this code.

// Language to search.
$find = 'French';

// Getting all the available languages.
$languages = \Drupal::service('language_manager')->getStandardLanguageList();

$key = key(array_filter($languages, function ($array) use ($find) {
  if (array_search($find, $array) === FALSE) {
    return FALSE;
  }
  return TRUE;
}));

// If you have the devel module installed you can use the dpm function.
dpm($key);
deleted 170 characters in body
Source Link
Adrian Cid Almaguer
  • 18.5k
  • 14
  • 79
  • 133
You can do it with this functionchode.
    
function// getCode($find)Language {to search.
$find = 'French';

// Getting all the available languages.
  $languages = \Drupal::service('language_manager')->getStandardLanguageList();
  $code = NULL;
  
 $key foreach= key(array_filter($languages as $code => $names) {
  , function for($i = 0; $i$array) <use count($names); $i++$find) {
            if ($names[$i]array_search($find, ==$array) $find=== FALSE) {
        return $code;
        }   
    }FALSE;
  }
  return $code;TRUE;
}  

// 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$key);
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);
You can do it with this chode.
    
// Language to search.
$find = 'French';

// Getting all the available languages.
$languages = \Drupal::service('language_manager')->getStandardLanguageList();

$key = key(array_filter($languages, function ($array) use ($find) {
  if (array_search($find, $array) === FALSE) {
    return FALSE;
  }
  return TRUE;
})); 

// If you have the devel module installed you can use the dpm function.
dpm($key);
Source Link
Adrian Cid Almaguer
  • 18.5k
  • 14
  • 79
  • 133
Loading