poe-i18n
Version:
i18n utility for Path of Exile
240 lines (239 loc) • 8.79 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __values = (this && this.__values) || function (o) {
var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
if (m) return m.call(o);
return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spread = (this && this.__spread) || function () {
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
var translate_1 = require("../translate");
var StatValue_1 = require("../types/StatValue");
exports.DEFAULT_RANGE_MESSAGE = '({min}–{max})';
var NoDescriptionFound = /** @class */ (function (_super) {
__extends(NoDescriptionFound, _super);
function NoDescriptionFound(stats) {
return _super.call(this, 'no descriptions found for ' + stats.map(function (_a) {
var id = _a.id;
return id;
}).join(',')) || this;
}
return NoDescriptionFound;
}(Error));
exports.NoDescriptionFound = NoDescriptionFound;
var Fallback;
(function (Fallback) {
Fallback[Fallback["throw"] = 0] = "throw";
Fallback[Fallback["id"] = 1] = "id";
Fallback[Fallback["skip"] = 2] = "skip";
})(Fallback = exports.Fallback || (exports.Fallback = {}));
var initial_options = {
datas: {},
fallback: Fallback.throw,
start_file: 'stat_descriptions',
getFormatters: function (t) { return t.formatters; },
range_message: exports.DEFAULT_RANGE_MESSAGE
};
var formatStats = function (stats, options) {
if (options === void 0) { options = {}; }
var e_1, _a;
var _b = Object.assign({}, initial_options, options), datas = _b.datas, fallback = _b.fallback, start_file = _b.start_file, getFormatters = _b.getFormatters, range_message = _b.range_message;
// translated lines
var lines = [];
// array of stat_ids for which hash lookup failed
var untranslated = new Map(stats.map(function (stat) { return [stat.id, stat]; }));
var description_file = datas[start_file];
while (description_file !== undefined) {
var data = description_file.data;
try {
for (var _c = __values(createDescriptionFindStrategies(data)), _d = _c.next(); !_d.done; _d = _c.next()) {
var descriptionFinder = _d.value;
lines.push.apply(lines, __spread(formatWithFinder(untranslated, descriptionFinder, {
getFormatters: getFormatters,
range_message: range_message
})));
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
}
finally { if (e_1) throw e_1.error; }
}
description_file = description_file.meta.include
? datas[description_file.meta.include]
: undefined;
}
lines.push.apply(lines, __spread(formatWithFallback(untranslated, fallback)));
return lines;
};
exports.default = formatStats;
/**
* creates an array of methods that can be used to find a description for a
* given stat.
*
* return value is to be interpreted as a priority queue
* @param descriptions
*/
function createDescriptionFindStrategies(descriptions) {
return [
function (_a) {
var id = _a.id;
return descriptions[id];
},
function (_a) {
var id = _a.id;
return Object.values(descriptions).find(function (_a) {
var stats = _a.stats;
return stats.includes(id);
});
}
];
}
exports.createDescriptionFindStrategies = createDescriptionFindStrategies;
function formatWithFinder(stats, find, options) {
if (options === void 0) { options = {}; }
var e_2, _a;
var _b = options.getFormatters, getFormatters = _b === void 0 ? function (t) { return t.formatters; } : _b, _c = options.range_message, range_message = _c === void 0 ? exports.DEFAULT_RANGE_MESSAGE : _c;
var lines = [];
var translated = new Set();
var _loop_1 = function (stat_id, stat) {
if (translated.has(stat_id)) {
return "continue";
}
var description = find(stat);
if (description !== undefined) {
var translation = translate_1.default(description, stats, function (t, n) { return getFormatters(t, stat, n); }, range_message);
if (translation === undefined) {
var requiredStatsAreZero = requiredStats(description, stats).every(function (_a) {
var value = _a.value;
return StatValue_1.isZero(value);
});
if (!requiredStatsAreZero) {
throw new Error("matching translation not found for '" + stat.id + "'");
}
}
else {
// mark as translated
description.stats.forEach(function (translated_id) {
stats.delete(translated_id);
translated.add(translated_id);
});
if (translation === translate_1.NO_DESCRIPTION) {
lines.push(stat_id + " (hidden)");
}
else {
lines.push(translation);
}
}
}
};
try {
for (var _d = __values(Array.from(stats.entries())), _e = _d.next(); !_e.done; _e = _d.next()) {
var _f = __read(_e.value, 2), stat_id = _f[0], stat = _f[1];
_loop_1(stat_id, stat);
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
}
finally { if (e_2) throw e_2.error; }
}
return lines;
}
function requiredStats(description, provided) {
// intersect the required stat_ids from the desc with the provided
return description.stats
.map(function (stat_id) {
var stat = provided.get(stat_id);
// default the value to 0
if (stat === undefined) {
return {
id: stat_id,
value: 0
};
}
else {
return stat;
}
})
.filter(function (stat) { return stat !== null; });
}
function formatWithFallback(stats, fallback) {
var non_zero_stats = Array.from(stats.entries()).filter(function (_a) {
var _b = __read(_a, 2), stat = _b[1];
return !StatValue_1.isZero(stat.value);
});
if (non_zero_stats.length === 0) {
return [];
}
if (fallback === Fallback.throw) {
if (stats.size > 0) {
throw new NoDescriptionFound(non_zero_stats.map(function (_a) {
var _b = __read(_a, 2), stat = _b[1];
return stat;
}));
}
else {
return [];
}
}
else if (fallback === Fallback.id) {
return non_zero_stats.map(function (_a) {
var _b = __read(_a, 1), key = _b[0];
return key;
});
}
else if (fallback === Fallback.skip) {
return [];
}
else if (typeof fallback === 'function') {
return non_zero_stats
.map(function (_a) {
var _b = __read(_a, 2), id = _b[0], stat = _b[1];
return fallback(id, stat);
})
.filter(function (line) { return typeof line === 'string'; });
}
else {
// should ts recognize that this is unreachable code? enums can prob
// be extended at runtime an therfore somebody could mess with them
throw new Error("unrecognized fallback type '" + fallback + "'");
}
}