Search code examples
pythonflaskpython-babel

How can I get a list of all the strings that Babel knows about?


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.


Solution

  • 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.