3 namespace Drupal\Core\Installer\Form;
5 use Drupal\Component\Utility\UserAgent;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Language\LanguageManager;
9 use Symfony\Component\HttpFoundation\Request;
12 * Provides the language selection form.
14 * Note that hardcoded text provided by this form is not translated. This is
15 * because translations are downloaded as a result of submitting this form.
17 class SelectLanguageForm extends FormBase {
22 public function getFormId() {
23 return 'install_select_language_form';
29 public function buildForm(array $form, FormStateInterface $form_state, $install_state = NULL) {
30 if (count($install_state['translations']) > 1) {
31 $files = $install_state['translations'];
36 $standard_languages = LanguageManager::getStandardLanguageList();
38 $browser_options = [];
40 $form['#title'] = 'Choose language';
42 // Build a select list with language names in native language for the user
43 // to choose from. And build a list of available languages for the browser
44 // to select the language default from.
45 // Select lists based on all standard languages.
46 foreach ($standard_languages as $langcode => $language_names) {
47 $select_options[$langcode] = $language_names[1];
48 $browser_options[$langcode] = $langcode;
50 // Add languages based on language files in the translations directory.
52 foreach ($files as $langcode => $uri) {
53 $select_options[$langcode] = isset($standard_languages[$langcode]) ? $standard_languages[$langcode][1] : $langcode;
54 $browser_options[$langcode] = $langcode;
57 asort($select_options);
58 $request = Request::createFromGlobals();
59 $browser_langcode = UserAgent::getBestMatchingLangcode($request->server->get('HTTP_ACCEPT_LANGUAGE'), $browser_options);
62 '#title' => 'Choose language',
63 '#title_display' => 'invisible',
64 '#options' => $select_options,
65 // Use the browser detected language as default or English if nothing found.
66 '#default_value' => !empty($browser_langcode) ? $browser_langcode : 'en',
68 $link_to_english = install_full_redirect_url(['parameters' => ['langcode' => 'en']]);
71 // #markup is XSS admin filtered which ensures unsafe protocols will be
72 // removed from the url.
73 '#markup' => '<p>Translations will be downloaded from the <a href="http://localize.drupal.org">Drupal Translation website</a>. If you do not want this, select <a href="' . $link_to_english . '">English</a>.</p>',
76 'select[name="langcode"]' => ['value' => 'en'],
80 $form['actions'] = ['#type' => 'actions'];
81 $form['actions']['submit'] = [
83 '#value' => 'Save and continue',
84 '#button_type' => 'primary',
92 public function submitForm(array &$form, FormStateInterface $form_state) {
93 $build_info = $form_state->getBuildInfo();
94 $build_info['args'][0]['parameters']['langcode'] = $form_state->getValue('langcode');
95 $form_state->setBuildInfo($build_info);