smhg / gettext-parser

Parse and compile gettext po and mo files, nothing more, nothing less
MIT License
160 stars 44 forks source link

Compatible with Browserify? #21

Closed lachmanski closed 7 years ago

lachmanski commented 8 years ago

I'd like to use this library in-browser. I installed node-js, Browserify, the encoding module, and bundled up your code. Everything runs without throwing any errors, but I'm getting strange PO file output. I was expecting a string of text, but I'm getting a string of numbers. Can you recommend a fix?

gettext-parser.js (before bundling with Browserify)

window.poParser = require('./poparser.js');
window.poCompiler = require('./pocompiler.js');
window.moParser = require('./moparser.js');
window.moCompiler = require('./mocompiler.js');

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>gettext-parser</title>
</head>
<body>
<script src="./gettext-parser.js"></script>
<script>
var example = {
  "charset": "iso-8859-1",

  "headers": {
    "content-type": "text/plain; charset=iso-8859-1",
    "plural-forms": "nplurals=2; plural=(n!=1);"
  },

  "translations": {
    "": {
      "": {
        "msgid": "",
        "msgstr": ["Content-Type: text/plain; charset=iso-8859-1\n..."]
      }
    },
    "another context": {
      "%s example": {
        "msgctx": "another context",
        "msgid": "%s example",
        "msgid_plural": "%s examples",
        "msgstr": ["% näide", "%s näidet"],
        "comments": {
          "translator": "This is regular comment",
          "reference": "/path/to/file:123"
        }
      }
    }
  }
};

var raw = poCompiler(example);
var blob = new Blob(raw, {type: "application/po"});
var objectUrl = URL.createObjectURL(blob);
window.open(objectUrl);
</script>
</body>
</html>

PO File Output:

10911510310510032343410109115103115116114323434103467111110116101110116458412111210158321161011201164711210897105110593299104971141151011166110511511145565653574549921103410348010811711497108457011111410911558321101121081171149710811561505932112108117114971086140110336149415992110341010353284104105115321051153211410110311710897114329911110910910111011610355832471129711610447116111471021051081015849505110109115103105100323437115321011209710911210810134101091151031051009511210811711497108323437115321011209710911210810111534101091151031151161149148933234373211022810510010134101091151031151161149149933234371153211022810510010111634

One thing I noticed is that my poCompiler reference does not contain .po.parse, .po.compile, .mo.parse, or .mo.compile function declarations. It is a bare function:

function (table) {
    var compiler = new Compiler(table);
    return compiler.compile();
}
smhg commented 7 years ago

Hi! I recently took over this project and want to follow up on your issue.

You mention you didn't have the po.parse, po.compile and so on methods. This seems to be because you require each component individually in gettext-parser.js. When you use something like window.gettextParser = require('gettext-parser'), this should be ok.

If you then do:

var raw = gettextParser.po.compile(example);
console.log(raw.toString());

Everything seems to work fine. Maybe also use UTF-8 as the character set in the example object.

I'm going to close this issue, but feel free to ask if anything is unclear.