I am trying to change the Drupal's default language programmatically but haven't found the codes/function. Any suggestions?

  • 2
    At a guess... $langs = language_list('enabled'); variable_set('language_default', $langs[$some_langcode]);
    – Clive
    Jun 18, 2014 at 11:13
  • @Clive, close, but not quite. See my answer for the correct code. Sep 25, 2014 at 7:59

4 Answers 4


Here is a corrected and tested version of the code posted by @Clive in a comment to the OP:

$langs = language_list(); // Note: No argument
// debug($langs, '$langs');
$langcode = 'en';
variable_set('language_default', $langs[$langcode]);

The code snippet above assumes that the two-character code that you assign to $langcode is enabled. Uncomment the call to debug to see the languages that are enabled on your site and what array indices they use.

Note that the setting will not take effect until the next page load.


I have found the code and its pretty simple .

//Set Default language
    $default_language = array('language' => 'en-US', 
                            'name' => 'English', 
                            'native' => 'English', 
                            'direction' => LANGUAGE_LTR, 
                            'enabled' => (int) TRUE, 
                            'plurals' => 0, 
                            'formula' =>'',
                            'domain' => '', 
                            'weight' => 0, 
                            'javascript' => '');
variable_set('language_default', (object) $default_language) ;

By implementing the following hook: hook_language_negotiation_info() and here is a full example



Just setting the variable is not always enough. By the time you've done so, the Drupal language negotiation function may have already run, and it will not react to this variable change. The proper way to do this is as follows:

// Get the list of languages
$languages = language_list();

// Set up the new language code
$new_lang_code = 'fr';

// Make sure the required language object is actually set
if (isset($languages[$new_lang_code])) {
  // Overwrite the global language object
  global $language;
  $language = $languages[$new_lang_code];

This has been documented in other questions such as: function to change language object

An additional option is to write your own custom language negotiation hook: https://api.drupal.org/api/drupal/modules!system!language.api.php/function/hook_language_negotiation_info/7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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