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.
19 class SelectLanguageForm extends FormBase {
24 public function getFormId() {
25 return 'install_select_language_form';
31 public function buildForm(array $form, FormStateInterface $form_state, $install_state = NULL) {
32 if (count($install_state['translations']) > 1) {
33 $files = $install_state['translations'];
38 $standard_languages = LanguageManager::getStandardLanguageList();
40 $browser_options = [];
42 $form['#title'] = 'Choose language';
44 // Build a select list with language names in native language for the user
45 // to choose from. And build a list of available languages for the browser
46 // to select the language default from.
47 // Select lists based on all standard languages.
48 foreach ($standard_languages as $langcode => $language_names) {
49 $select_options[$langcode] = $language_names[1];
50 $browser_options[$langcode] = $langcode;
52 // Add languages based on language files in the translations directory.
54 foreach ($files as $langcode => $uri) {
55 $select_options[$langcode] = isset($standard_languages[$langcode]) ? $standard_languages[$langcode][1] : $langcode;
56 $browser_options[$langcode] = $langcode;
59 asort($select_options);
60 $request = Request::createFromGlobals();
61 $browser_langcode = UserAgent::getBestMatchingLangcode($request->server->get('HTTP_ACCEPT_LANGUAGE'), $browser_options);
64 '#title' => 'Choose language',
65 '#title_display' => 'invisible',
66 '#options' => $select_options,
67 // Use the browser detected language as default or English if nothing found.
68 '#default_value' => !empty($browser_langcode) ? $browser_langcode : 'en',
70 $link_to_english = install_full_redirect_url(['parameters' => ['langcode' => 'en']]);
73 // #markup is XSS admin filtered which ensures unsafe protocols will be
74 // removed from the url.
75 '#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>',
78 'select[name="langcode"]' => ['value' => 'en'],
82 $form['actions'] = ['#type' => 'actions'];
83 $form['actions']['submit'] = [
85 '#value' => 'Save and continue',
86 '#button_type' => 'primary',
94 public function submitForm(array &$form, FormStateInterface $form_state) {
95 $build_info = $form_state->getBuildInfo();
96 $build_info['args'][0]['parameters']['langcode'] = $form_state->getValue('langcode');
97 $form_state->setBuildInfo($build_info);