@myrotvorets/po2json
Version:
Convers a PO files into JED compatible with WordPress i18n
2 lines • 6.79 kB
JavaScript
(()=>{var r={31:(r,e,t)=>{function trim(r){return r.replace(/^\s+|\s+$/g,"")}var PO=function(){this.comments=[];this.extractedComments=[];this.headers={};this.headerOrder=[];this.items=[]};PO.prototype.save=function(r,e){t(147).writeFile(r,this.toString(),e)};PO.prototype.toString=function(){var r=[];if(this.comments){this.comments.forEach((function(e){r.push(("# "+e).trim())}))}if(this.extractedComments){this.extractedComments.forEach((function(e){r.push(("#. "+e).trim())}))}r.push('msgid ""');r.push('msgstr ""');var e=this;var t=[];this.headerOrder.forEach((function(r){if(r in e.headers){t.push(r)}}));var s=Object.keys(this.headers);s.forEach((function(r){if(t.indexOf(r)===-1){t.push(r)}}));t.forEach((function(t){r.push('"'+t+": "+e.headers[t]+'\\n"')}));r.push("");this.items.forEach((function(e){r.push(e.toString());r.push("")}));return r.join("\n")};PO.load=function(r,e){t(147).readFile(r,"utf-8",(function(r,t){if(r){return e(r)}var s=PO.parse(t);e(null,s)}))};PO.parse=function(r){r=r.replace(/\r\n/g,"\n");var e=new PO;var t=r.split(/\n\n/);var s=[];while(t[0]&&(s.length===0||s[s.length-1].indexOf('msgid ""')<0)){if(t[0].match(/msgid\s+"[^"]/)){s.push('msgid ""')}else{s.push(t.shift())}}s=s.join("\n");var i=t.join("\n").split(/\n/);e.headers={"Project-Id-Version":"","Report-Msgid-Bugs-To":"","POT-Creation-Date":"","PO-Revision-Date":"","Last-Translator":"",Language:"","Language-Team":"","Content-Type":"","Content-Transfer-Encoding":"","Plural-Forms":""};e.headerOrder=[];s.split(/\n/).reduce((function(r,e){if(r.merge){e=r.pop().slice(0,-1)+e.slice(1);delete r.merge}if(/^".*"$/.test(e)&&!/^".*\\n"$/.test(e)){r.merge=true}r.push(e);return r}),[]).forEach((function(r){if(r.match(/^#\./)){e.extractedComments.push(r.replace(/^#\.\s*/,""))}else if(r.match(/^#/)){e.comments.push(r.replace(/^#\s*/,""))}else if(r.match(/^"/)){r=r.trim().replace(/^"/,"").replace(/\\n"$/,"");var t=r.split(/:/);var s=t.shift().trim();var i=t.join(":").trim();e.headers[s]=i;e.headerOrder.push(s)}}));var a=PO.parsePluralForms(e.headers["Plural-Forms"]);var n=a.nplurals;var u=new PO.Item({nplurals:n});var c=null;var o=0;var l=0;var f=0;function finish(){if(u.msgid.length>0){if(l>=f){u.obsolete=true}l=0;f=0;e.items.push(u);u=new PO.Item({nplurals:n})}}function extract(r){r=trim(r);r=r.replace(/^[^"]*"|"$/g,"");r=r.replace(/\\([abtnvfr'"\\?]|([0-7]{3})|x([0-9a-fA-F]{2}))/g,(function(r,e,t,s){if(t){return String.fromCharCode(parseInt(t,8))}if(s){return String.fromCharCode(parseInt(s,16))}switch(e){case"a":return"";case"b":return"\b";case"t":return"\t";case"n":return"\n";case"v":return"\v";case"f":return"\f";case"r":return"\r";default:return e}}));return r}while(i.length>0){var m=trim(i.shift());var h=false;var p=false;if(m.match(/^#\~/)){m=trim(m.substring(2));h=true}if(m.match(/^#:/)){finish();u.references.push(trim(m.replace(/^#:/,"")))}else if(m.match(/^#,/)){finish();var g=trim(m.replace(/^#,/,"")).split(",");for(var d=0;d<g.length;d++){u.flags[g[d]]=true}}else if(m.match(/^#($|\s+)/)){finish();u.comments.push(trim(m.replace(/^#($|\s+)/,"")))}else if(m.match(/^#\./)){finish();u.extractedComments.push(trim(m.replace(/^#\./,"")))}else if(m.match(/^msgid_plural/)){u.msgid_plural=extract(m);c="msgid_plural";f++}else if(m.match(/^msgid/)){finish();u.msgid=extract(m);c="msgid";f++}else if(m.match(/^msgstr/)){var v=m.match(/^msgstr\[(\d+)\]/);o=v&&v[1]?parseInt(v[1]):0;u.msgstr[o]=extract(m);c="msgstr";f++}else if(m.match(/^msgctxt/)){finish();u.msgctxt=extract(m);c="msgctxt";f++}else{if(m.length>0){f++;if(c==="msgstr"){u.msgstr[o]+=extract(m)}else if(c==="msgid"){u.msgid+=extract(m)}else if(c==="msgid_plural"){u.msgid_plural+=extract(m)}else if(c==="msgctxt"){u.msgctxt+=extract(m)}}}if(h){l++}}finish();return e};PO.parsePluralForms=function(r){var e=(r||"").split(";").reduce((function(r,e){var t=e.trim();var s=t.indexOf("=");var i=t.substring(0,s).trim();var a=t.substring(s+1).trim();r[i]=a;return r}),{});return{nplurals:e.nplurals,plural:e.plural}};PO.Item=function(r){var e=r&&r.nplurals;this.msgid="";this.msgctxt=null;this.references=[];this.msgid_plural=null;this.msgstr=[];this.comments=[];this.extractedComments=[];this.flags={};this.obsolete=false;var t=Number(e);this.nplurals=isNaN(t)?2:t};PO.Item.prototype.toString=function(){var r=[];var e=this;var _escape=function(r){r=r.replace(/[\x07\b\t\v\f\r"\\]/g,(function(r){switch(r){case"":return"\\a";case"\b":return"\\b";case"\t":return"\\t";case"\v":return"\\v";case"\f":return"\\f";case"\r":return"\\r";default:return"\\"+r}}));return r};var _process=function(r,e,t){var s=[];var i=e.split(/\n/);var a=typeof t!=="undefined"?"["+t+"]":"";if(i.length>1){s.push(r+a+' ""');i.forEach((function(r){s.push('"'+_escape(r)+'"')}))}else{s.push(r+a+' "'+_escape(e)+'"')}return s};var _processLineBreak=function(r,e,t){var s=_process(r,e,t);for(var i=1;i<s.length-1;i++){s[i]=s[i].slice(0,-1)+'\\n"'}return s};this.comments.forEach((function(e){r.push("# "+e)}));this.extractedComments.forEach((function(e){r.push("#. "+e)}));this.references.forEach((function(e){r.push("#: "+e)}));var t=Object.keys(this.flags).filter((function(r){return!!this.flags[r]}),this);if(t.length>0){r.push("#, "+t.join(","))}var s=this.obsolete?"#~ ":"";["msgctxt","msgid","msgid_plural","msgstr"].forEach((function(t){var i=e[t];if(i!=null){var a=false;if(Array.isArray(i)){a=i.some((function(r){return r}))}if(Array.isArray(i)&&i.length>1){i.forEach((function(e,i){var a=_processLineBreak(t,e,i);r=r.concat(s+a.join("\n"+s))}))}else if(e.msgid_plural&&t==="msgstr"&&!a){for(var n=0;n<e.nplurals;n++){r=r.concat(s+_process(t,"",n))}}else{var u=e.msgid_plural&&Array.isArray(i)?0:undefined;i=Array.isArray(i)?i.join():i;var c=_processLineBreak(t,i,u);r=r.concat(s+c.join("\n"+s))}}}));return r.join("\n")};r.exports=PO},147:r=>{"use strict";r.exports=require("fs")},282:r=>{"use strict";r.exports=require("process")}};var e={};function __nccwpck_require__(t){var s=e[t];if(s!==undefined){return s.exports}var i=e[t]={exports:{}};var a=true;try{r[t](i,i.exports,__nccwpck_require__);a=false}finally{if(a)delete e[t]}return i.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var t={};(()=>{const r=__nccwpck_require__(282);const e=__nccwpck_require__(31);if(r.argv.length===3){e.load(r.argv[2],((e,t)=>{if(!e){const e={"":{domain:t.headers["X-Domain"]||"messages",lang:t.headers.Language,plural_forms:t.headers["Plural-Forms"]}};t.items.forEach((r=>{const t=r.msgctxt!==null?`${r.msgctxt}${r.msgid}`:r.msgid;e[t]=r.msgstr}));const s={domain:t.headers["X-Domain"]||"messages",locale_data:{messages:e}};r.stdout.write(JSON.stringify(s));r.exit(0)}r.stderr.write(e.toString());r.exit(1)}))}else{r.stderr.write("Usage: po2json input.po\n")}})();module.exports=t})();