<commit_before>package xitrum

import java.util.{Collections, List => JList}
import java.util.Locale

import scala.collection.JavaConverters._
import scala.util.control.NonFatal

import io.netty.handler.codec.http.HttpHeaderNames

import xitrum.i18n.PoLoader

trait I18n {
  this: Action =>

  // For Component locale to work, this should only be used in
  // "locale" and "locale_=" methods below.
  // Other places should not use "_locale" directly. See ComponentTest.
  private var _locale = Locale.ENGLISH

  /** Default locale is "en". */
  def locale = _locale

  /**
   * Sets current locale manually. The locale should have a corresponding file
   * `i18n/languageTag.po` in classpath (the languageTag should be in IETF BCP 47 format).
   */
  def locale_=(locale: Locale) {
    _locale = locale
  }

  /** @return List of [[Locale.LanguageRange]] sorted by priority from high to low */
  lazy val browserLanguageRanges: JList[Locale.LanguageRange] = {
    val header = request.headers.get(HttpHeaderNames.ACCEPT_LANGUAGE)
    if (header == null) {
      Collections.emptyList[Locale.LanguageRange]
    } else {
      try {
        Locale.LanguageRange.parse(header)
      } catch {
        case NonFatal(e) =>
          log.debug("Cannot parse Accept-Language header", e)
          Collections.emptyList[Locale.LanguageRange]
      }
    }
  }

  /**
   * Sets current locale automatically based on the matching of the "Accept-Language"
   * header against the list of given locales. The locales should have corresponding files
   * `i18n/languageTag.po` in classpath (the languageTag should be in IETF BCP 47 format).
   *
   * If there's no match, the locale is the default "en".
   */
  def autosetLanguage(locales: Locale*) {
    val bestMatched = Locale.lookup(browserLanguageRanges, locales.asJava)
    if (bestMatched != null) locale = bestMatched
  }

  //----------------------------------------------------------------------------

  def t(singular: String) =
    PoLoader.get(locale).t(singular)

  def tc(ctx: String, singular: String) =
    PoLoader.get(locale).tc(ctx, singular)

  def tn(singular: String, plural: String, n: Long) =
    PoLoader.get(locale).tn(singular, plural, n)

  def tcn(ctx: String, singular: String, plural: String, n: Long) =
    PoLoader.get(locale).tcn(ctx, singular, plural, n)

  //----------------------------------------------------------------------------<commit_msg>#630 Replace autosetLanguage with autosetLocale<commit_after>package xitrum

import java.util.{Collections, List => JList}
import java.util.Locale

import scala.collection.JavaConverters._
import scala.util.control.NonFatal

import io.netty.handler.codec.http.HttpHeaderNames

import xitrum.i18n.PoLoader

trait I18n {
  this: Action =>

  // For Component locale to work, this should only be used in
  // "locale" and "locale_=" methods below.
  // Other places should not use "_locale" directly. See ComponentTest.
  private var _locale = Locale.ENGLISH

  /** Default locale is "en". */
  def locale = _locale

  /**
   * Sets current locale manually. The locale should have a corresponding file
   * `i18n/languageTag.po` in classpath (the languageTag should be in IETF BCP 47 format).
   */
  def locale_=(locale: Locale) {
    _locale = locale
  }

  /** @return List of [[Locale.LanguageRange]] sorted by priority from high to low */
  lazy val browserLanguageRanges: JList[Locale.LanguageRange] = {
    val header = request.headers.get(HttpHeaderNames.ACCEPT_LANGUAGE)
    if (header == null) {
      Collections.emptyList[Locale.LanguageRange]
    } else {
      try {
        Locale.LanguageRange.parse(header)
      } catch {
        case NonFatal(e) =>
          log.debug("Cannot parse Accept-Language header", e)
          Collections.emptyList[Locale.LanguageRange]
      }
    }
  }

  /**
   * Sets current locale automatically based on the matching of the "Accept-Language"
   * header against the list of given locales. The locales should have corresponding files
   * `i18n/languageTag.po` in classpath (the languageTag should be in IETF BCP 47 format).
   *
   * If there's no match, the locale is the default "en".
   */
  def autosetLocale(availableLocales: Locale*) {
    val bestMatched = Locale.lookup(browserLanguageRanges, availableLocales.asJava)
    if (bestMatched != null) locale = bestMatched
  }

  //----------------------------------------------------------------------------

  def t(singular: String) =
    PoLoader.get(locale).t(singular)

  def tc(ctx: String, singular: String) =
    PoLoader.get(locale).tc(ctx, singular)

  def tn(singular: String, plural: String, n: Long) =
    PoLoader.get(locale).tn(singular, plural, n)

  def tcn(ctx: String, singular: String, plural: String, n: Long) =
    PoLoader.get(locale).tcn(ctx, singular, plural, n)

  //----------------------------------------------------------------------------
{'fraction_non_alphanumeric': 0.16156045751633988, 'fraction_numerical': 0.0055147058823529415, 'mean_word_length': 2.608695652173913, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '5809969', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1403, 'n_words': 424}