<commit_before>package lila.i18n

import play.api.mvc.Results.Redirect
import play.api.mvc.{ Action, RequestHeader, Handler, Result }

import lila.common.HTTPRequest

final class I18nRequestHandler(
    pool: I18nPool,
    protocol: String,
    cdnDomain: String
) {

  def apply(req: RequestHeader, userOption: Option[lila.user.User]): Option[Result] =
    if (appliesTo(req))
      userOption.flatMap(_.lang) match {
        // user has a lang that doesn't match the request, redirect to user lang
        case Some(userLang) if !pool.domainLang(req).exists(_.language == userLang) =>
          Redirect(redirectUrlLang(req, userLang)).some
        // no user lang
        case None => pool.domainLang(req) match {
          // header accepts the req lang, just proceed
          case Some(reqLang) if req.acceptLanguages.has(reqLang) => none
          // no req lang, or header doesn't accept req lang, redirect based on header
          case _ => Redirect(redirectUrlLang(req, pool.preferred(req).language)).some
        }
        case _ => none
      }
    else none

  private def appliesTo(req: RequestHeader) =
    HTTPRequest.isRedirectable(req) && req.host != cdnDomain && !excludePath(req.path)
<commit_msg>don't redirect if no better language can be found<commit_after>package lila.i18n

import play.api.mvc.Results.Redirect
import play.api.mvc.{ Action, RequestHeader, Handler, Result }

import lila.common.HTTPRequest

final class I18nRequestHandler(
    pool: I18nPool,
    protocol: String,
    cdnDomain: String
) {

  def apply(req: RequestHeader, userOption: Option[lila.user.User]): Option[Result] =
    if (appliesTo(req))
      userOption.flatMap(_.lang) match {
        // user has a lang that doesn't match the request, redirect to user lang
        case Some(userLang) if !pool.domainLang(req).exists(_.language == userLang) =>
          Redirect(redirectUrlLang(req, userLang)).some
        // no user lang
        case None => pool.domainLang(req) match {
          // header accepts the req lang, just proceed
          case Some(reqLang) if req.acceptLanguages.has(reqLang) => none
          // header refuses the req lang, redirect if a better lang can be found
          case Some(reqLang) =>
            val preferred = pool preferred req
            (preferred != reqLang) option Redirect(redirectUrlLang(req, preferred.language))
          // no req lang, redirect based on header
          case None => Redirect(redirectUrlLang(req, pool.preferred(req).language)).some
        }
        case _ => none
      }
    else none

  private def appliesTo(req: RequestHeader) =
    HTTPRequest.isRedirectable(req) && req.host != cdnDomain && !excludePath(req.path)

{'fraction_non_alphanumeric': 0.10285926476049016, 'fraction_numerical': 0.004455997029335313, 'mean_word_length': 2.96759941089838, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4902719', 'n_tokens_mistral': 790, 'n_tokens_neox': 764, 'n_words': 224}