@drincs/pixi-vn-json
Version:
Pixi'VN can be integrated with JSON files to create a visual novel.
1 lines • 2.9 kB
JavaScript
var g=class t{static _beforeToTranslate=void 0;static _translate=e=>e;static _afterToTranslate=void 0;static t(e){return Array.isArray(e)?e.map(i=>t.translate(`${i}`)):t.translate(`${e}`)}static set beforeToTranslate(e){t._beforeToTranslate=e;}static set translate(e){t._translate=e;}static get translate(){return e=>{let i=e;return t._beforeToTranslate&&(i=t._beforeToTranslate(i)),i=t._translate(i),t._afterToTranslate&&(i=t._afterToTranslate(i)),i}}static set afterToTranslate(e){t._afterToTranslate=e;}static addKey(e,i,s){let n=s.defaultValue||"empty_string";typeof i=="string"&&(i=[i]),Array.isArray(i)&&i.forEach(r=>{e[r]===void 0&&(n==="empty_string"?e[r]="":n==="copy_key"&&(t._beforeToTranslate?e[r]=t._beforeToTranslate(r):e[r]=r));});}static getConditionalsThenElse(e,i=[]){if(typeof e=="object"&&e&&"type"in e)if(e.type==="ifelse")e.then&&t.getConditionalsThenElse(e.then,i),e.else&&t.getConditionalsThenElse(e.else,i);else if(e.type==="stepswitch"){if(e.elements)if(Array.isArray(e.elements))e.elements.forEach(s=>{t.getConditionalsThenElse(s,i);});else if(e.elements.type==="ifelse"){let s=[];t.getConditionalsThenElse(e.elements,s),s.forEach(n=>{i.push(...n);});}else if(e.elements.type==="stepswitch"){let s=[];t.getConditionalsThenElse(e.elements,s),s.forEach(n=>{i.push(...n);});}else t.getConditionalsThenElse(e.elements,i);}else e.type==="resulttocombine"?(e.firstItem&&t.getConditionalsThenElse(e.firstItem,i),e.secondConditionalItem&&e.secondConditionalItem.forEach(s=>{t.getConditionalsThenElse(s,i);})):i.push(e);else e&&i.push(e);return i}static generateJsonTranslation(e,i={},s={}){return e.forEach(n=>{if(n.choices){let r=[];Array.isArray(n.choices)?r=[n.choices]:r=t.getConditionalsThenElse(n.choices),r.forEach(o=>o.forEach(a=>{if("type"in a){let T=[];a.type==="ifelse"||a.type==="stepswitch"?t.getConditionalsThenElse(a,T):T=[a],T.map(l=>t.getConditionalsThenElse(l.text)).forEach(l=>{Array.isArray(l)&&l.forEach(f=>{Array.isArray(f)?f.forEach(h=>{typeof h=="string"?t.addKey(i,h,s):t.getConditionalsThenElse(h).forEach(c=>{Array.isArray(c)?c.forEach(p=>{typeof p=="string"&&t.addKey(i,p,s);}):typeof c=="string"&&t.addKey(i,c,s);});}):typeof f=="string"&&t.addKey(i,f,s);});});}}));}if(n.dialogue){let r=[];Array.isArray(n.dialogue)?r=[n.dialogue]:r=t.getConditionalsThenElse(n.dialogue),r.forEach(o=>{typeof o=="string"?t.addKey(i,o,s):"text"in o&&Array.isArray(o.text)&&o.text.forEach(a=>{typeof a=="string"?t.addKey(i,a,s):t.getConditionalsThenElse(a).forEach(y=>{typeof y=="string"?t.addKey(i,y,s):Array.isArray(y)&&y.forEach(l=>{typeof l=="string"?t.addKey(i,l,s):t.getConditionalsThenElse(l).forEach(f=>{typeof f=="string"&&t.addKey(i,f,s);});});});});});}n.conditionalStep&&t.getConditionalsThenElse(n.conditionalStep).forEach(o=>{Array.isArray(o)?t.generateJsonTranslation(o,i,s):t.generateJsonTranslation([o],i,s);});}),i}};var u=g;export{u as translator};