Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order of messages in generated POT file #39

Open
wjdp opened this issue Feb 11, 2020 · 1 comment
Open

Order of messages in generated POT file #39

wjdp opened this issue Feb 11, 2020 · 1 comment

Comments

@wjdp
Copy link

wjdp commented Feb 11, 2020

The generated POT file's messages are sorted alphabetically (i.e. msgid "a" is above msgid "b")

In other implementations such as Python's Babel (http://babel.pocoo.org) messages are sorted by source file path. For example

base/a.py
base/b.py
base/a/a.py
base/a/b.py

Note that files in 'higher' directories are kept at the top and subdirectories come afterwards.

When messages are present in multiple files babel seems to select the directory with the least subdirectories as the sort key.

This makes translators lives easier in tools that rely on the order of messages in the PO files as similar messages are grouped together.

Can we have an option to set the sort order of the generated POT file?

@wjdp wjdp changed the title Order of messages in POT file Order of messages in generated POT file Feb 11, 2020
@wjdp
Copy link
Author

wjdp commented Feb 11, 2020

I've implemented the above in our extraction.js file.

Path sorting as described above is implemented in the path-sort package, just used that.

const pofile = require("pofile");
// From https://github.com/hughsk/path-sort
const pathsort = require("path-sort");
const pathsortStandalone = require("path-sort").standalone();

function getPotString(headers = {}) {
    const po = new pofile();
    po.items = extractor.getPofileItems().sort((a, b) => {
        return pathsortStandalone(pathsort(a.references)[0], pathsort(b.references)[0]);
    });
    po.headers = Object.assign({'Content-Type': 'text/plain; charset=UTF-8'}, headers);
    return po.toString();
}

fs.writeFileSync('./messages_js.pot', getPotString());

This gave results like

js/d.js
js/components/a.js
js/views/a/index.js
js/views/b/index.js
js/views/c/index.js
js/views/a/components/dropdown.js

In retrospect splitting 'deeper' files down the bottom (the above dropdown.js) actually doesn't seem a good idea. 🤦‍♀️

Next I tried dumb string sorting. Really should have done that first.

const pofile = require("pofile");

function getPotString(headers = {}) {
    const po = new pofile();
    po.items = extractor.getPofileItems().sort((a, b) => (a.references.sort()[0] > b.references.sort()[0]) ? 1 : -1);
    po.headers = Object.assign({'Content-Type': 'text/plain; charset=UTF-8'}, headers);
    return po.toString();
}

fs.writeFileSync('./messages_js.pot', getPotString());

This gives the more desirable result 😄

js/components/a.js
js/d.js
js/views/a/index.js
js/views/a/components/dropdown.js
js/views/b/index.js
js/views/c/index.js

d.js isn't at the top anymore but for our case this seems fine. js/views/a/ files are all together which is much nicer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants