server/helpers/i18n.js

"use strict";

require("core-js/modules/es.regexp.constructor.js");
require("core-js/modules/es.regexp.to-string.js");
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/es.weak-map.js");
require("core-js/modules/web.dom-collections.iterator.js");
require("core-js/modules/es.array.reduce.js");
require("core-js/modules/es.object.keys.js");
require("core-js/modules/es.symbol.replace.js");
require("core-js/modules/es.string.replace.js");
require("core-js/modules/es.symbol.js");
require("core-js/modules/es.symbol.description.js");
require("core-js/modules/es.array.join.js");
require("core-js/modules/es.array.slice.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getAcceptedLanguageCodes = getAcceptedLanguageCodes;
exports.parseAcceptLanguage = parseAcceptLanguage;
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _setPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/setPrototypeOf"));
require("core-js/modules/es.array.sort.js");
require("core-js/modules/es.array.filter.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.array.map.js");
require("core-js/modules/es.regexp.exec.js");
require("core-js/modules/es.string.split.js");
require("core-js/modules/es.string.match.js");
require("core-js/modules/es.parse-float.js");
function _wrapRegExp() { _wrapRegExp = function _wrapRegExp(re, groups) { return new BabelRegExp(re, void 0, groups); }; var _super = RegExp.prototype, _groups = new WeakMap(); function BabelRegExp(re, flags, groups) { var _this = new RegExp(re, flags); return _groups.set(_this, groups || _groups.get(re)), (0, _setPrototypeOf2.default)(_this, BabelRegExp.prototype); } function buildGroups(result, re) { var g = _groups.get(re); return Object.keys(g).reduce(function (groups, name) { var i = g[name]; if ("number" == typeof i) groups[name] = result[i];else { for (var k = 0; void 0 === result[i[k]] && k + 1 < i.length;) { k++; } groups[name] = result[i[k]]; } return groups; }, Object.create(null)); } return (0, _inherits2.default)(BabelRegExp, RegExp), BabelRegExp.prototype.exec = function (str) { var result = _super.exec.call(this, str); if (result) { result.groups = buildGroups(result, this); var indices = result.indices; indices && (indices.groups = buildGroups(indices, this)); } return result; }, BabelRegExp.prototype[Symbol.replace] = function (str, substitution) { if ("string" == typeof substitution) { var groups = _groups.get(this); return _super[Symbol.replace].call(this, str, substitution.replace(/\$<([^>]+)>/g, function (_, name) { var group = groups[name]; return "$" + (Array.isArray(group) ? group.join("$") : group); })); } if ("function" == typeof substitution) { var _this = this; return _super[Symbol.replace].call(this, str, function () { var args = arguments; return "object" != _typeof(args[args.length - 1]) && (args = [].slice.call(args)).push(buildGroups(args, _this)), substitution.apply(this, args); }); } return _super[Symbol.replace].call(this, str, substitution); }, _wrapRegExp.apply(this, arguments); }
/*
 * Copyright (C) 2023  David Kellner
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/**
 * Parses an Accept-Language header to obtain the language codes, optional subtags and weights.
 * @param {string} acceptLanguage - Accept-Language header value.
 * @returns {Array} Parsed languages, sorted by weight in descending order.
 */
function parseAcceptLanguage(acceptLanguage) {
  return acceptLanguage.split(',').map(function (value) {
    var _match$groups$subtag$, _match$groups$subtag, _match$groups$weight;
    var match = value.match( /*#__PURE__*/_wrapRegExp(/([A-Za-z]{2,3})(?:\x2D([\x2D0-9A-Z_a-z]+))?(?:;q=([01](?:\.[0-9]+)?))?/, {
      tag: 1,
      subtag: 2,
      weight: 3
    }));
    return match ? {
      code: match.groups.tag,
      subtags: (_match$groups$subtag$ = (_match$groups$subtag = match.groups.subtag) === null || _match$groups$subtag === void 0 ? void 0 : _match$groups$subtag.split('-')) !== null && _match$groups$subtag$ !== void 0 ? _match$groups$subtag$ : [],
      weight: parseFloat((_match$groups$weight = match.groups.weight) !== null && _match$groups$weight !== void 0 ? _match$groups$weight : '1')
    } : null;
  }).filter(function (value) {
    return value !== null;
  }).sort(function (a, b) {
    return b.weight - a.weight;
  });
}

/**
 * Extracts language codes from the Accept-Language header, ordered by weight/preference.
 * @param {Request} request - Request object which includes HTTP headers.
 * @returns {string[]} Parsed language codes, sorted by weight in descending order.
 */
function getAcceptedLanguageCodes(request) {
  var _request$headers$acce;
  return parseAcceptLanguage((_request$headers$acce = request.headers['accept-language']) !== null && _request$headers$acce !== void 0 ? _request$headers$acce : '').map(function (language) {
    return language.code;
  });
}
//# sourceMappingURL=i18n.js.map