/**
  * {@inheritdoc}
  */
 public function getLangcode(Request $request = NULL)
 {
     $langcode = NULL;
     if ($this->languageManager && $request && $request->server->get('HTTP_ACCEPT_LANGUAGE')) {
         $http_accept_language = $request->server->get('HTTP_ACCEPT_LANGUAGE');
         $langcodes = array_keys($this->languageManager->getLanguages());
         $mappings = $this->config->get('language.mappings')->get();
         $langcode = UserAgent::getBestMatchingLangcode($http_accept_language, $langcodes, $mappings);
     }
     return $langcode;
 }
 /**
  * Unit tests for the language_from_browser() function.
  *
  * @see language_from_browser().
  */
 function testLanguageFromBrowser()
 {
     // The order of the languages is only important if the browser language
     // codes are having the same qvalue, otherwise the one with the highest
     // qvalue is preferred. The automatically generated generic tags are always
     // having a lower qvalue.
     $languages = array('en' => new Language(array('id' => 'en')), 'en-US' => new Language(array('id' => 'en-US')), 'fr-CA' => new Language(array('id' => 'fr-CA')), 'fr' => new Language(array('id' => 'fr')), 'es-MX' => new Language(array('id' => 'es-MX')), 'pt' => new Language(array('id' => 'pt')), 'eh-oh-laa-laa' => new Language(array('id' => 'eh-oh-laa-laa')), 'zh-hans' => new Language(array('id' => 'zh-hans')), 'zh-hant' => new Language(array('id' => 'zh-hant')), 'zh-hant-tw' => new Language(array('id' => 'zh-hant')));
     $test_cases = array('en,en-US,fr-CA,fr,es-MX' => 'en', 'en-US,en,fr-CA,fr,es-MX' => 'en', 'fr,en' => 'en', 'en,fr' => 'en', 'en-US,fr' => 'en-US', 'fr,en-US' => 'en-US', 'fr,fr-CA' => 'fr-CA', 'fr-CA,fr' => 'fr-CA', 'fr' => 'fr-CA', 'fr;q=1' => 'fr-CA', 'fr,es-MX' => 'fr-CA', 'fr,es' => 'fr-CA', 'es,fr' => 'fr-CA', 'es-MX,de' => 'es-MX', 'de,es-MX' => 'es-MX', 'en' => 'en', 'En' => 'en', 'EN' => 'en', ' en' => 'en', 'en ' => 'en', 'en, fr' => 'en', 'es' => 'es-MX', 'es-MX' => 'es-MX', 'pt' => 'pt', 'pt-PT' => 'pt', 'pt-PT;q=0.5,pt-BR;q=1,en;q=0.7' => 'en', 'pt-PT;q=1,pt-BR;q=0.5,en;q=0.7' => 'en', 'pt-PT;q=0.4,pt-BR;q=0.1,en;q=0.7' => 'en', 'pt-PT;q=0.1,pt-BR;q=0.4,en;q=0.7' => 'en', 'eh-oh-laa-laa' => 'eh-oh-laa-laa', 'eh-oh-laa' => 'eh-oh-laa-laa', 'eh-oh' => 'eh-oh-laa-laa', 'eh' => 'eh-oh-laa-laa', 'fr,en;q=0.5' => 'fr-CA', 'fr,en;q=0.5,fr-CA;q=0.25' => 'fr', '*,fr-CA;q=0.5' => 'en', '*,en;q=0.25' => 'fr-CA', 'en,en-US;q=0.5,fr;q=0.25' => 'en', 'en-US,en;q=0.5,fr;q=0.25' => 'en-US', '' => FALSE, 'de,pl' => FALSE, 'iecRswK4eh' => FALSE, $this->randomMachineName(10) => FALSE, 'zh-cn, en-us;q=0.90, en;q=0.80, zh;q=0.70' => 'zh-hans', 'zh-tw, en-us;q=0.90, en;q=0.80, zh;q=0.70' => 'zh-hant', 'zh-hant, en-us;q=0.90, en;q=0.80, zh;q=0.70' => 'zh-hant', 'zh-hans, en-us;q=0.90, en;q=0.80, zh;q=0.70' => 'zh-hans', 'zh-cn' => 'zh-hans', 'zh-sg' => 'zh-hans', 'zh-tw' => 'zh-hant', 'zh-hk' => 'zh-hant', 'zh-mo' => 'zh-hant', 'zh-hans' => 'zh-hans', 'zh-hant' => 'zh-hant', 'zh-chs' => 'zh-hans', 'zh-cht' => 'zh-hant');
     $mappings = $this->container->get('config.factory')->get('language.mappings')->get();
     foreach ($test_cases as $accept_language => $expected_result) {
         $result = UserAgent::getBestMatchingLangcode($accept_language, array_keys($languages), $mappings);
         $this->assertIdentical($result, $expected_result, format_string("Language selection '@accept-language' selects '@result', result = '@actual'", array('@accept-language' => $accept_language, '@result' => $expected_result, '@actual' => isset($result) ? $result : 'none')));
     }
 }
 /**
  * {@inheritdoc}
  */
 public function getLangcode(Request $request = NULL)
 {
     $langcode = NULL;
     if ($this->languageManager && $request && $request->server->get('HTTP_ACCEPT_LANGUAGE')) {
         $http_accept_language = $request->server->get('HTTP_ACCEPT_LANGUAGE');
         $langcodes = array_keys($this->languageManager->getLanguages());
         $mappings = $this->config->get('language.mappings')->get('map');
         $langcode = UserAgent::getBestMatchingLangcode($http_accept_language, $langcodes, $mappings);
         // Internal page cache with multiple languages and browser negotiation
         // could lead to wrong cached sites. Therefore disabling the internal
         // page cache.
         // @todo Solve more elegantly in https://www.drupal.org/node/2430335.
         \Drupal::service('page_cache_kill_switch')->trigger();
     }
     return $langcode;
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state, $install_state = NULL)
 {
     if (count($install_state['translations']) > 1) {
         $files = $install_state['translations'];
     } else {
         $files = array();
     }
     $standard_languages = LanguageManager::getStandardLanguageList();
     $select_options = array();
     $browser_options = array();
     $form['#title'] = $this->t('Choose language');
     // Build a select list with language names in native language for the user
     // to choose from. And build a list of available languages for the browser
     // to select the language default from.
     if (count($files)) {
         // Select lists based on available language files.
         foreach ($files as $langcode => $uri) {
             $select_options[$langcode] = isset($standard_languages[$langcode]) ? $standard_languages[$langcode][1] : $langcode;
             $browser_options[] = $langcode;
         }
     } else {
         // Select lists based on all standard languages.
         foreach ($standard_languages as $langcode => $language_names) {
             $select_options[$langcode] = $language_names[1];
             $browser_options[] = $langcode;
         }
     }
     $request = Request::createFromGlobals();
     $browser_langcode = UserAgent::getBestMatchingLangcode($request->server->get('HTTP_ACCEPT_LANGUAGE'), $browser_options);
     $form['langcode'] = array('#type' => 'select', '#title' => $this->t('Choose language'), '#title_display' => 'invisible', '#options' => $select_options, '#default_value' => !empty($browser_langcode) ? $browser_langcode : 'en');
     if (empty($files)) {
         $form['help'] = array('#type' => 'item', '#markup' => String::format('<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="!english">English</a>.</p>', array('!english' => install_full_redirect_url(array('parameters' => array('langcode' => 'en'))))), '#states' => array('invisible' => array('select[name="langcode"]' => array('value' => 'en'))));
     }
     $form['actions'] = array('#type' => 'actions');
     $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Save and continue'), '#button_type' => 'primary');
     return $form;
 }
Esempio n. 5
0
  public function buildForm(array $form, FormStateInterface $form_state, $install_state = NULL) {
    if (count($install_state['translations']) > 1) {
      $files = $install_state['translations'];
    }
    else {
      $files = array();
    }
    $standard_languages = LanguageManager::getStandardLanguageList();
    $select_options = array();
    $browser_options = array();

    $form['#title'] = 'Choose languages';

    // Build a select list with language names in native language for the user
    // to choose from. And build a list of available languages for the browser
    // to select the language default from.
    // Select lists based on all standard languages.
    foreach ($standard_languages as $langcode => $language_names) {
      $select_options[$langcode] = $language_names[1];
      $browser_options[$langcode] = $langcode;
    }
    // Add languages based on language files in the translations directory.
    if (count($files)) {
      foreach ($files as $langcode => $uri) {
        $select_options[$langcode] = isset($standard_languages[$langcode]) ? $standard_languages[$langcode][1] : $langcode;
        $browser_options[$langcode] = $langcode;
      }
    }
    asort($select_options);
    $request = Request::createFromGlobals();
    $browser_langcode = UserAgent::getBestMatchingLangcode($request->server->get('HTTP_ACCEPT_LANGUAGE'), $browser_options);
    $form['langcode'] = array(
      '#type' => 'select',
      '#title' => 'Choose default language',
      '#title_display' => 'before',
      '#options' => $select_options,
      // Use the browser detected language as default or English if nothing found.
      '#default_value' => !empty($browser_langcode) ? $browser_langcode : 'en',
    );
    $link_to_english = install_full_redirect_url(array('parameters' => array('langcode' => 'en')));
    $form['help'] = array(
      '#type' => 'item',
      // #markup is XSS admin filtered which ensures unsafe protocols will be
      // removed from the url.
      '#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>',
      '#states' => array(
        'invisible' => array(
          'select[name="langcode"]' => array('value' => 'en'),
        ),
      ),
    );

    $form['langcodes'] = array(
      '#type' => 'select',
      '#title' => 'Choose another languages',
      '#title_display' => 'before',
      '#options' => $select_options,
      '#multiple' => TRUE,
      '#description' => 'Select another languages if your site is multilingual',
    );

    $form['actions'] = array('#type' => 'actions');
    $form['actions']['submit'] =  array(
      '#type' => 'submit',
      '#value' => 'Save and continue',
      '#button_type' => 'primary',
    );

    return $form;
  }
Esempio n. 6
0
 /**
  * Test matching language from user agent.
  *
  * @dataProvider providerTestGetBestMatchingLangcode
  * @covers ::getBestMatchingLangcode
  */
 public function testGetBestMatchingLangcode($accept_language, $expected)
 {
     $result = UserAgent::getBestMatchingLangcode($accept_language, $this->getLanguages(), $this->getMappings());
     $this->assertSame($expected, $result);
 }