poe-i18n
Version:
i18n utility for Path of Exile
57 lines (56 loc) • 1.83 kB
JavaScript
import { matchesTranslation } from './match';
import printf from './printf';
export var NO_DESCRIPTION = 'NO_DESCRIPTION';
export default function translate(description, provided,
/**
* @param t
* @param count {number} number of params
*/
getFormatters, range_message) {
if (getFormatters === void 0) {
/**
* @param t
* @param count {number} number of params
*/
getFormatters = function (t) { return t.formatters; }; }
if (range_message === void 0) { range_message = '({min}–{max})'; }
var stats = description.stats, no_description = description.no_description, translations = description.translations;
if (no_description === true) {
return NO_DESCRIPTION;
}
// intersect the required stat_ids from the desc with the provided
var required_stats = 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; });
var translation = matchingTranslation(translations, required_stats);
if (translation === undefined) {
return undefined;
}
else {
return printf(translation.text, required_stats.map(function (_a) {
var value = _a.value;
return value;
}), getFormatters(translation, required_stats.length), range_message);
}
}
function matchingTranslation(translations, stats) {
var args = stats.map(function (_a) {
var value = _a.value;
return value;
});
return translations.find(function (translation) {
return matchesTranslation(translation, args);
});
}