3 namespace Drupal\language\Plugin\LanguageNegotiation;
5 use Drupal\language\LanguageNegotiationMethodBase;
6 use Symfony\Component\HttpFoundation\Request;
9 * Class that determines the language to be assigned to URLs when none is
12 * The language negotiation process has a fallback chain that ends with the
13 * default language negotiation method. Each built-in language type has a
14 * separate initialization:
15 * - Interface language, which is the only configurable one, always gets a valid
16 * value. If no request-specific language is detected, the default language
18 * - Content language merely inherits the interface language by default.
19 * - URL language is detected from the requested URL and will be used to rewrite
20 * URLs appearing in the page being rendered. If no language can be detected,
21 * there are two possibilities:
22 * - If the default language has no configured path prefix or domain, then the
23 * default language is used. This guarantees that (missing) URL prefixes are
24 * preserved when navigating through the site.
25 * - If the default language has a configured path prefix or domain, a
26 * requested URL having an empty prefix or domain is an anomaly that must be
27 * fixed. This is done by introducing a prefix or domain in the rendered
28 * page matching the detected interface language.
30 * @LanguageNegotiation(
31 * id = Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrlFallback::METHOD_ID,
32 * types = {Drupal\Core\Language\LanguageInterface::TYPE_URL},
34 * name = @Translation("URL fallback"),
35 * description = @Translation("Use an already detected language for URLs if none is found.")
38 class LanguageNegotiationUrlFallback extends LanguageNegotiationMethodBase {
41 * The language negotiation method id.
43 const METHOD_ID = 'language-url-fallback';
48 public function getLangcode(Request $request = NULL) {
51 if ($this->languageManager) {
52 $default = $this->languageManager->getDefaultLanguage();
53 $config = $this->config->get('language.negotiation')->get('url');
54 $prefix = ($config['source'] == LanguageNegotiationUrl::CONFIG_PATH_PREFIX);
56 // If the default language is not configured to convey language
57 // information, a missing URL language information indicates that URL
58 // language should be the default one, otherwise we fall back to an
59 // already detected language.
60 if (($prefix && empty($config['prefixes'][$default->getId()])) || (!$prefix && empty($config['domains'][$default->getId()]))) {
61 $langcode = $default->getId();
64 $langcode = $this->languageManager->getCurrentLanguage()->getId();