getBestMatchingLangcode example

/** * {@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.     $this->pageCacheKillSwitch->trigger();

    return $langcode;
  }

}
'zh-chs' => 'zh-hans',
    ];
  }

  /** * Tests 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);
  }

  /** * Data provider for testGetBestMatchingLangcode(). * * @return array * - An accept-language string. * - Expected best matching language code. */
  public function providerTestGetBestMatchingLangcode() {
    
$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'] = [
      '#type' => 'select',
      '#title' => 'Choose language',
      '#title_display' => 'invisible',
      '#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(['parameters' => ['langcode' => 'en']]);
    $form['help'] = [
      '#type' => 'item',
      
Home | Imprint | This part of the site doesn't use cookies.