const languages = require('../../config/language');
const createError = require('http-errors');
const ObjectId = require('mongoose').Types.ObjectId;

/**
 * 根据http的Accept-Language返回locale
 * 有支持中文的则返回zh，其他一律返回en
 * @param {*} clientLocales 
 */
function getLocale(clientLocales) {
  if (!clientLocales) return 'en'; // 识别不到客户端的locale，则返回默认中文
  let langs = languages.map(e => e.code);
  for (let lang of langs) {
    if (clientLocales[0].includes(lang)) return lang;
  }
  return 'en';  // 客户端locale没有中文这一项，那么用英文
}


function ensureLanguage(lang) {
  let langs = languages.map(e => e.code);
  if (langs.includes(lang)) return lang;
  return 'en';
}


module.exports = {
  getLocale, ensureLanguage,
}
