/************************************************************************************/ export default function autoTranslateItems(beast_traits, translations) { let compmod = game.wfrp4efr.compmod; var fulltraits = game.packs.get(compmod+'.traits'); var fullskills = game.packs.get(compmod+'.skills'); var fulltalents = game.packs.get(compmod+'.talents'); var fullcareers = game.packs.get(compmod+'.careers'); var fulltrappings = game.packs.get(compmod+'.trappings'); var fullspells = game.packs.get(compmod+'.spells'); var fullprayers = game.packs.get(compmod+'.prayers'); var eisitems = game.packs.get('eis.eisitems'); var eisspells = game.packs.get('eis.eisspells'); var ugtalents = game.packs.get('wfrp4e-unofficial-grimoire.ug-careerstalentstraits'); var ugspells = game.packs.get('wfrp4e-unofficial-grimoire.ug-spells'); //console.log("Comp :", compmod, fulltraits); for (let trait_en of beast_traits) { var special = ""; var nbt = ""; var name_en = trait_en.name.trim(); // strip \r in some traits name if ( trait_en.type == "trait") { if ( name_en.includes("Tentacles") ) { // Process specific Tentacles case var re = /(.d*)x Tentacles/i; var res = re.exec( name_en ); if ( res && res[1] ) nbt = res[1] + "x "; name_en = "Tentacles"; } else if ( name_en.includes("(") && name_en.includes(")") ) { // Then process specific traits name with (xxxx) inside var re = /(.*) \((.*)\)/i; var res = re.exec( name_en ); name_en = res[1]; // Get the root traits name special = " (" + game.i18n.localize( res[2].trim() ) + ")"; // And the special keyword } var trait_fr = fulltraits.translate( { name: name_en } ); //console.log(">>>>> Trait ?", name_en, nbt, trait_fr.name, special); trait_en.name = nbt + trait_fr.name + special; if ( trait_fr.data && trait_fr.data.description && trait_fr.data.description.value ) { trait_en.data.description.value = trait_fr.data.description.value; } else if ( eisitems ) { // No description in the FR compendium -> test other compendium if presenr trait_fr = eisitems.translate( { name: name_en } ); trait_en.name = nbt + trait_fr.name + special; if ( trait_fr.data && trait_fr.data.description && trait_fr.data.description.value ) trait_en.data.description.value = trait_fr.data.description.value; } if ( trait_en.data && trait_en.data.specification && isNaN(trait_en.data.specification.value) ) { // This is a string, so translate it //console.log("Translating : ", trait_en.data.specification.value); trait_en.data.specification.value = game.i18n.localize( trait_en.data.specification.value.trim() ); } } else if ( trait_en.type == "skill") { if ( name_en.includes("(") && name_en.includes(")") ) { // Then process specific skills name with (xxxx) inside var re = /(.*) +\((.*)\)/i; var res = re.exec( name_en ); name_en = res[1].trim(); // Get the root skill name special = " (" + game.i18n.localize( res[2].trim() ) + ")"; // And the special keyword } var trait_fr = fullskills.translate( { name: name_en } ); //console.log(">>>>> Skill ?", name_en, special, trait_fr.name, trait_fr); if (trait_fr.translated) { trait_en.name = trait_fr.name + special; if ( trait_fr.data ) { trait_en.data.description.value = trait_fr.data.description.value; } } } else if ( trait_en.type == "prayer") { var trait_fr = fullprayers.translate( { name: name_en } ); //console.log(">>>>> Prayer ?", name_en, special, trait_fr.name ); trait_en.name = trait_fr.name + special; if ( trait_fr.data && trait_fr.data.description && trait_fr.data.description.value ) trait_en.data.description.value = trait_fr.data.description.value; } else if ( trait_en.type == "spell") { var trait_fr = fullspells.translate( { name: name_en } ); if ( (!trait_fr.data || !trait_fr.data.description || !trait_fr.data.description.value) && eisspells) { // If no translation, test eisspells trait_fr = eisspells.translate( { name: name_en } ); } if ( (!trait_fr.data || !trait_fr.data.description || !trait_fr.data.description.value) && ugspells) { // If no translation, test eisspells trait_fr = ugspells.translate( { name: name_en } ); } //console.log(">>>>> Spell ?", name_en, special, trait_fr.name ); trait_en.name = trait_fr.name + special; if ( trait_fr.data && trait_fr.data.description && trait_fr.data.description.value ) trait_en.data.description.value = trait_fr.data.description.value; } else if ( trait_en.type == "talent") { if ( name_en.includes("(") && name_en.includes(")") ) { // Then process specific skills name with (xxxx) inside var re = /(.*) +\((.*)\)/i; var res = re.exec( name_en ); name_en = res[1].trim(); // Get the root talent name, no parenthesis this time... special = " (" + game.i18n.localize( res[2].trim() ) + ")"; // And the special keyword } var trait_fr = fulltalents.translate( { name: name_en } ); //console.log(">>>>> Talent ?", name_en, special, trait_fr.name); if ( (!trait_fr.data || !trait_fr.data.description || !trait_fr.data.description.value) && ugtalents) { // If no translation, test ugtalents trait_fr = ugtalents.translate( { name: name_en } ); } if ( trait_fr.translated) { trait_en.name = trait_fr.name + special; if ( trait_fr.data ) { // Why ??? trait_en.data.description.value = trait_fr.data.description.value; } } } else if ( trait_en.type == "career") { var career_fr = fullcareers.translate( trait_en ); //console.log(">>>>> Career ?", name_en, career_fr.name); trait_en = career_fr; } else if ( trait_en.type == "trapping" || trait_en.type == "weapon" || trait_en.type == "armour" || trait_en.type == "container" || trait_en.type == "money") { var trapping_fr = fulltrappings.translate( trait_en ); //console.log(">>>>> Trapping ?", name_en, trapping_fr.name); trait_en.name = trapping_fr.name; if ( trapping_fr.data) { trait_en.data.description = trapping_fr.data.description; } } } return beast_traits; } /************************************************************************************/ let str1 = `JABBERSLYTHE M WS BS S T I Agi Dex Int WP Fel W 7 45 40 55 50 20 35 - 10 20 - 20 Traits: Armour 3, Bestial, Bite+9, Bounce, Corrosive Blood, Distracting, Infected, Maddening Aura (see page 17), Night Vision, Size (Enormous), Tail +8, Tongue Attack +5 (12), Venom, Weapon +9. `; let str = `REINER AND DIETER LEDERMANN SMUGGLERS (BRASS 3) M WS BS S T I Agi Dex Int WP Fel W 4 33 33 32 35 38 41 39 33 37 38 12 Traits: Weapon (Dagger +5, Sword +7) Skills: Bribery 43, Charm 43, Cool 42, Consume Alcohol 45, Gossip 43, Haggle 43, Lore (Local 38), Perception 43, Secret Signs (Smuggler) 37 Talents: Briber, Criminal, Dealmaker, Etiquette (Criminals, Doktor, Guilder) Trappings: Dagger, Hand Weapon (Sword) ` import "./xregexp-all.js"; const us_carac = 'm\\s+ws\\s+bs\\s+s\\s+t\\s+i\\s+agi?\\s+dex\\s+int\\s+\\wp\\s+fel\\s+w'; const fr_carac = 'm\\s+ws\\s+bs\\s+s\\s+t\\s+i\\s+agi?\\s+dex\\s+int\\s+\\wp\\s+fel\\s+w'; const carac_val = '(?[0-9-]+)\\s+(?[0-9-]+)\\s+(?[0-9-]+)\\s+(?[0-9-]+)\\s+(?[0-9-]+)\\s+(?[0-9-]+)\\s+(?[0-9-]+)\\s+(?[0-9-]+)\\s+(?[0-9-]+)\\s+(?[0-9-]+)\\s+(?[0-9-]+)\\s+(?[0-9-]+)'; let sectionData = [ { name: "trait", toFind:"Traits\\s*:", secondParse: '(?[a-z\\s]*)[\\s\\+]*(?.*|[0-9]+)', index:-1 }, { name: "skill", toFind:"Skills\\s*:", index:-1 }, { name: "talent", toFind:"Talents\\s*:", index:-1 }, { name: "trapping", toFind:"Trappings\\s*:", index:-1 } ]; let regSep = XRegExp('\\s*,\\s*', 'gi'); // Term separator, with auto trim let regLine1 = XRegExp('\\r|\\n|\\.', 'gi'); // Term separator, with auto trim /************************************************************************************/ Hooks.once('ready', () => { let t1 = "agi"; let reg0 = XRegExp('agi?', 'gi'); //console.log("PARSER pos 1: ", reg0.test( t1 ) ); let reg1 = XRegExp(us_carac, 'gi'); let res = reg1.test(str); if (res) { //stat block identified go on let reg2 = XRegExp(carac_val, 'gi'); let res = XRegExp.exec(str, reg2); console.log("Movement is : ", res); // Search position of skills/talents/... for( let def of sectionData ) { def.regDef = XRegExp(def.toFind, 'gi'); let res = XRegExp.exec(str, def.regDef); if (res ) def.index = res.index; // Get the index in the string //console.log(" Parsing", def.name, res); } sectionData.sort( function(a, b) { return a.index - b.index; } ); // Loop again to extract for(let i=0; i< sectionData.length; i++ ) { let def = sectionData[i]; if ( def.index > -1) { let maxIndex = str.length; if ( sectionData[i+1] && sectionData[i+1].index > -1 ) maxIndex = sectionData[i+1].index; def.substring = str.substring(def.index, maxIndex); def.substring = XRegExp.replace(def.substring, def.regDef, ""); def.substring = XRegExp.replace(def.substring, regLine1, " "); console.log("Substring :", def.substring); let termList = XRegExp.split(def.substring, regSep); def.termList = []; for (let name of termList) { if (def.secondParse) { let subres = XRegExp.exec( name, XRegExp(def.secondParse, 'gi') ); console.log("Subres", subres); name = subres.name.trim(); if ( subres.value.length > 0 ) { if ( subres.value.substring(0, 1) == '(' ) name += ' ' + subres.value; else name += ' (' + subres.value + ')'; } } def.termList.push( { name: name, type: def.name, data: { description: { value: "" } } } ); } let newlist = autoTranslateItems( def.termList ); console.log(newlist); } } } //Do we have Traits ? } )