Question

I have an application in which the main strings are in English and then various translations are made in various .po/.mo files, as usual (using Flask and Flask-Babel). Is it possible to get a list of all the English strings somewhere within my Python code? Specifically, I'd like to have an admin interface on the website which lets someone log in and choose an arbitrary phrase to be used in a certain place without having to poke at actual Python code or .po/.mo files. This phrase might change over time but needs to be translated, so it needs to be something Babel knows about.

I do have access to the actual .pot file, so I could just parse that, but I was hoping for a cleaner method if possible.

Était-ce utile?

La solution 2

If you alredy use babel you can get all items from po file:

from babel.messages.pofile import read_po

catalog = read_po(open(full_file_name))
for message in catalog:
    print message.id, message.string

See http://babel.edgewall.org/browser/trunk/babel/messages/pofile.py.

You alredy can try get items from mo file:

from babel.messages.mofile import read_mo

catalog = read_po(open(full_file_name))
for message in catalog:
    print message.id, message.string

But when I try use it last time it's not was availible. See http://babel.edgewall.org/browser/trunk/babel/messages/mofile.py.

You can use polib as @Miguel wrote.

Autres conseils

You can use polib for this.

This section of the documentation shows examples of how to iterate over the contents of a .po file. Here is one taken from that page:

import polib

po = polib.pofile('path/to/catalog.po')
for entry in po:
    print entry.msgid, entry.msgstr
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top