<commit_before>import glob from 'glob';
import path from 'path';
import immutable from 'immutable';
import merger from './merger';

function loadLanguages() {
  const files = glob
    .sync('src/client/*/i18n/*.js')
    .map(file => {
      const translations = require(path.join(__dirname, '../../../', file));
      const tokens = file.match(/([^/]+)\/i18n\/([^/]+).js$/);
      return {
        language: tokens[2],
        feature: tokens[1],
        translations
      };
    });

  const messages = {};
  const locales = [];

  files.forEach(({language, feature, translations}) => {
    if (!messages[language]) messages[language] = {};
    messages[language][feature] = translations;

    if (locales.indexOf(language) < 0) locales.push(language);
  });

  return {
    formats: {},
    locales,
    messages
  };
}

function extractLocaleFromReq(req, locales) {
  return req.acceptsLanguages(locales);
}

export default function middleware({defaultLocale, getLocaleFromRequest = extractLocaleFromReq}) {
  const {locales, messages} = loadLanguages();

  return (req, res, next) => {
    const locale = getLocaleFromRequest(req, locales);
    const defaultMessages = messages[defaultLocale];
    const localeMessages = messages[locale];
    req.i18n = {
      locale,
      messages: defaultLocale !== locale
        ? immutable.fromJS(defaultMessages).mergeWith(merger, localeMessages)
        : defaultMessages
    };
    next();
  };
<commit_msg>Rename const to be more obvious<commit_after>import glob from 'glob';
import path from 'path';
import immutable from 'immutable';
import merger from './merger';

function loadLanguages() {
  const files = glob
    .sync('src/client/*/i18n/*.js')
    .map(file => {
      const translations = require(path.join(__dirname, '../../../', file));
      const tokens = file.match(/([^/]+)\/i18n\/([^/]+).js$/);
      return {
        language: tokens[2],
        feature: tokens[1],
        translations
      };
    });

  const messages = {};
  const locales = [];

  files.forEach(({language, feature, translations}) => {
    if (!messages[language]) messages[language] = {};
    messages[language][feature] = translations;

    if (locales.indexOf(language) < 0) locales.push(language);
  });

  return {
    formats: {},
    locales,
    messages
  };
}

function extractLocaleFromReq(req, locales) {
  return req.acceptsLanguages(locales);
}

export default function middleware({defaultLocale, getLocaleFromRequest = extractLocaleFromReq}) {
  const {locales, messages} = loadLanguages();

  return (req, res, next) => {
    const userLocale = getLocaleFromRequest(req, locales);
    const defaultMessages = messages[defaultLocale];
    const userMessages = messages[userLocale];
    req.i18n = {
      locale,
      messages: defaultLocale !== userLocale
        ? immutable.fromJS(defaultMessages).mergeWith(merger, userMessages)
        : defaultMessages
    };
    next();
  };

{'fraction_non_alphanumeric': 0.1524846834581348, 'fraction_numerical': 0.006126616746085773, 'mean_word_length': 3.309384164222874, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1664682', 'n_tokens_mistral': 882, 'n_tokens_neox': 859, 'n_words': 193}