How can I allow users to set their language upon registration? I know that by using the language switcher block, they can change the language of the site, but I would like to have a button on the form that clearly allows the user to select their language.

I found the thread below, but the code offered is for Drupal 6, and I don't know how to update it to Drupal 7:
http://drupal.org/node/773500

Does anyone know of a way to do this in Drupal 7?

Comments

kristen pol’s picture

This can be set by the admin when creating a user, but not by the user when registering (which seems strange to me).

One solution is posted here but I didn't try it out:

http://stackoverflow.com/questions/5556895/how-to-add-language-switcher-...

The code is for Drupal 6 but you can use the Coder module to convert it from Drupal 6 to Drupal 7.

[update] Ah, I am now seeing that you already linked to a post that linked to the article above... so... you can try converting it with Coder or maybe someone else has an idea. I really find it odd that this is not solved.

Kristen

-Kristen
Profile: https://www.linkedin.com/in/kristenpol
Drupal 7 Multilingual Sites: http://kristen.org/book

ptmkenny’s picture

Thanks for your reply. I was unable to update it with the coder module, as user_hook(), which the linked code relies on, is deprecated in D7 and I can't figure out how it's supposed to be now.

In the meantime, I have arrived at a "sort-of" solution by displaying an i18n block above the form for each language that says "You are completing this form in English (Spanish, etc.) To use the site in another language, first change the language using the links to the left before proceeding with registration."

Ugly but it works...

ptmkenny’s picture

With some help from the fine folks at Drupal answers, here's the code to do this:

Place inside a custom module:

function hook_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  $form['locale']['language'] = array(
    '#type' => (count($names) <= 5 ? 'radios' : 'select'),
    '#title' => t('Language'),
    '#default_value' => $user_preferred_language->language,
    '#options' => $names,
    '#description' => $mode ? t("This account's default language for e-mails, and preferred language for site presentation.") : t("This account's default language for e-mails."),
  );

function MYMODULE_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  // Use exactly the same access logic as the original, 
  // without checking for the 'administer users' permission
  $form['locale']['#access'] = ($form['#user_category'] == 'account' || ($form['#user_category'] == 'register'));
}

calsoftinc’s picture

This code really worked i am too facing the same problem. But not getting any solution lastly i got it. It your hard work but it help many of us.

Offshore software product development

david_garcia’s picture

This works out of the box, autoselects current language and provides list of system available languages:

function hook_form_user_register_form_alter(&$form, &$form_state, $form_id) {

  $languages = language_list();
  $names = array();
  foreach ($languages as $key => $language) {
	$names[$key] = t($language->name);
  }
  global $language;

  $form['account']['locale']['language'] = array(
    '#type' => (count($names) <= 5 ? 'radios' : 'select'),
    '#title' => t('Language'),
    '#default_value' => $language->language,
    '#options' => $names,
    '#description' => t("Prefered language for you account."),
  );
  
  // Use exactly the same access logic as the original,
  // without checking for the 'administer users' permission
  $form['account']['locale']['#access'] = ($form['#user_category'] == 'account' || ($form['#user_category'] == 'register'));
}
kopeboy’s picture

Which code to use? This one or the first one? What's the difference?

Both don't work if used with the LoginToboggan module (for user login and registration on the same page).

Does anyone know how to use this in conjunction with this module? (https://drupal.org/project/logintoboggan)

Thanks!

kopeboy’s picture

Closing brace before function MYMODULE was missing in the first code..

boshtian’s picture

I needed this lately and saw that when you hook to user_register_form, the form item is actually there. The only thing you have to do is change its #access value, which is set to false by default.

function YOUR_MODULE_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  $form['locale']['#access'] = true;
}