sqlalchemy / mako

Mako Templates for Python
https://www.makotemplates.org
MIT License
358 stars 62 forks source link

Suggestion: Custom Python message extractor in the Babel plugin #136

Open sqlalchemy-bot opened 14 years ago

sqlalchemy-bot commented 14 years ago

Migrated issue, originally created by Anonymous

I use a gettext wrapper function with slightly modified semantics. I have written a Python message extractor for it, and now I would like to use the same extractor also in Mako. Would it be possible to specify the extract_python function to be should be used in mako.ext.babelplugin:extract_nodes ?

A simple solution would be along the lines of:

extractor = options.get('python_extract_function', extract_python)

and then calling extractor instead of extract_python directly.

Or, the babel.messages.extract:extract function could be called directly, for access to Babel's dispatching mechanism (with a slight backwards incompatibility):

from babel.messages.extract import extract as babel_extract
...
method = options.get('python_extractor', 'python')

and call babel_extract(method, ...) instead of extract_python(...)

(Sorry for filing this under the "runtime" component, it doesn't fit in any of the available ones.)

sqlalchemy-bot commented 14 years ago

Philip Jenvey (@pjenvey) wrote:

I'm really curious about the modifications you've done to extract_python. Before I consider this, could you tell me what customizations you've done?

sqlalchemy-bot commented 14 years ago

Anonymous wrote:

Sure. I've simplified the gettext API to only use one function instead of ugettext/ungettext, with keyword arguments. It also handles translation context (like GTK's Q() macro). So the function has the signature: def (message, plural=None, n=None, context=None)

Some examples:[[BR]] ('file') calls ugettext[[BR]] ('%s file', '%s files', n=3) calls ungettext[[BR]] ('file', context='noun') can have different translation than ('file', context='verb')

Babel's Python extractor can't handle keyword arguments, so another one is needed. My extractor currently compiles the code to AST and analyzes that.

sqlalchemy-bot commented 14 years ago

Changes by Philip Jenvey (@pjenvey):