code: public function setupLocale()
      {
          // The virtual host may prescribe a language
          if (isset($this->variables['i18n']))
          {
              $vhost = $this->vhost;
              $locales = $vhost->getLocales();
              $session = $this->request->session;
              $locale = null;
              
              if ($session !== null && $session->has('locale', Type::STRING))
              {
                  $locval = $session['locale'];
                  foreach ($locales as $supported_locale)
                  {
                      if ($locval === $supported_locale->getLocale())
                      {
                          $locale = $locval;
                          self::$logger->debug("Set language based on session variable to {0}", [$locale]);
                          break;
                      }
                  }
              }
  
              if ($locale === null && !empty($locales))
              {
                  $accept = $this->request->accept_language;
                  $locale = $vhost->selectLocaleFromAcceptHeader($accept);
  
                  if ($locale === null)
                  {
                      $locale = reset($locales);
                      self::$logger->debug("Set language based on default locale {0}", [$locale]);
                  }
                  else
                  {
                      self::$logger->debug("Set language based on HTTP request headers to {0}", [$locale]);
                  }
  
                  if ($session !== null)
                      $session->set('locale', $locale);
              }
  
              if (!empty($locale))
                  $this->variables['i18n']->setLocale($locale);
          }
      }
docs: Set up the language / locale based on the users preferences and the
  provided locales by the virtual host.
  
  When neither the user or the virtual host specifies any locale,
  the default is maintained.