Drupal installation profile with multiple languages

Multilanguage

By default Drupal gets installed with only one default language - English - and all additional languages need to be added afterwards, followed by downloading and importing all relevant interface translations. But why not to make our multilingual lives just a little bit easier?

An alternative might be a new installation profile, which would allow users to select all required languages during the installation process, and automatically download and import all relevant translation files for them.

Multilanguage installation profile

Let's create new installation profile called (creatively) multilanguage, based on the standard profile. We'll need 3 files in it: multilanguage.info, multilanguage.install and multilanguage.profile.

multilanguage.info

This is almost an exact copy of standard.info, with the only difference being added dependency on Localization update module, which we will need for downloading interface translations from localize.drupal.org:

name = Multilanguage
description = Install with multiple languages and commonly used features pre-configured.
version = VERSION
core = 7.x
dependencies[] = block
dependencies[] = color
dependencies[] = comment
dependencies[] = contextual
dependencies[] = dashboard
dependencies[] = help
dependencies[] = image
dependencies[] = list
dependencies[] = menu
dependencies[] = number
dependencies[] = options
dependencies[] = path
dependencies[] = taxonomy
dependencies[] = dblog
dependencies[] = search
dependencies[] = shortcut
dependencies[] = toolbar
dependencies[] = overlay
dependencies[] = field_ui
dependencies[] = file
dependencies[] = rdf

; Install localization dependencies.
dependencies[] = l10n_update
Don't forget to download the Localization update module into the modules/ directory within this installation profile!

multilanguage.install

This should have the same content as standard.install, no need to replicate it all here then, let's just use the standard functionality:

<?php
/**
 * Implements hook_install().
 */
function multilanguage_install() {
  include_once
DRUPAL_ROOT . '/profiles/standard/standard.install';
 
standard_install();
}
?>

multilanguage.profile

First we need to add two new items to the installation tasks - one for displaying the form to select additional languages to install, and second one to actually install those languages and download all required translation files for them:

<?php
/**
 * Implements hook_install_tasks().
 */
function multilanguage_install_tasks() {

 
// Add installation step asking for additional languages to install.
 
$tasks['multilanguage_configure_translations_form'] = array(
   
'display_name' => st('Configure languages'),
   
'type' => 'form',
  );

 
// Add batch process installing selected additional languages.
 
$tasks['multilanguage_import_translations'] = array(
   
'display_name' => st('Import translations'),
   
'type' => 'batch',
  );

  return
$tasks;
}
?>

Language selection form is pretty simple:

<?php
/**
 * Installation task callback: returns the form allowing the user to select
 * additional languages to install.
 */
function multilanguage_configure_translations_form() {
 
// Provides predefined country list.
 
include_once DRUPAL_ROOT . '/includes/iso.inc';

 
$form['translations'] = array(
   
'#type' => 'select',
   
'#title' => st('Additional languages'),
   
'#description' => st('Select additional languages to enable and download contributed interface translations.'),
   
'#options' => _country_get_predefined_list(),
   
'#multiple' => TRUE,
   
'#size' => 10,
  );

 
$form['submit'] = array(
   
'#type' => 'submit',
   
'#value' => st('Install selected languages'),
  );

  return
$form;
}
?>

Let's not forget about submission callback, as we need to pass posted value to the next step of the installation process:

<?php
/**
 * Submit callback: saves selected languages to be processed on the next step.
 */
function multilanguage_configure_translations_form_submit(&$form, &$form_state) {
 
variable_set('multilanguage_selected_translations', $form_state['values']['translations']);
}
?>

Now, let's enable selected languages and import relevant translations:

<?php
/**
 * Installation task callback: creates batch process to enable additional
 * languages and download relevant interface translations.
 */
function multilanguage_import_translations() {
  include_once
DRUPAL_ROOT . '/includes/locale.inc';
 
module_load_include('check.inc', 'l10n_update');
 
module_load_include('batch.inc', 'l10n_update');

  if (
$translations = variable_get('multilanguage_selected_translations', array())) {
   
// No need to keep this variable anymore.
   
variable_del('multilanguage_selected_translations');

   
// Prepare batch process to enable languages and download translations.
   
$operations = array();
    foreach (
$translations as $translation) {
     
locale_add_language(strtolower($translation));

     
// Build batch with l10n_update module.
     
$history = l10n_update_get_history();
     
$available = l10n_update_available_releases();
     
$updates = l10n_update_build_updates($history, $available);

     
$operations = array_merge($operations, _l10n_update_prepare_updates($updates, NULL, array()));
    }

   
$batch = l10n_update_batch_multiple($operations, LOCALE_IMPORT_KEEP);
    return
$batch;
  }
}
?>

Finally, let's assume that site's main language will always be English, and therefore remove Drupal's default Choose language installation task:

<?php
/**
 * Implement hook_install_tasks_alter().
 */
function multilanguage_install_tasks_alter(&$tasks, $install_state) {
 
// Set default site language to English.
 
global $install_state;
 
$install_state['parameters']['locale'] = 'en';
 
// Hide 'Choose language' installation task.
 
$tasks['install_select_locale']['display'] = FALSE;
 
$tasks['install_select_locale']['run'] = INSTALL_TASK_SKIP;
}
?>

Final result

New installation profile available:

Additional languages selection:

Importing translations:

All languages installed:

You can find more information on using Drupal in multiple languages in the Multilingual Guide.