Skip to content

How to get msgstr and msgid at the same time? #890

@Lounarok

Description

@Lounarok

I know babel cannot extract variables.

However I'd like to reduce the duplication of my code between logging and translation.
I'd like to have my logging with msgid but my response is msgstr. It's easier to lookup the log in a fixed language or it'd be hard for looking into the log in Klingon. (No offense to Klingon)

Eg: I want to have logging text with User request is completed while response is qaStaHvIS qemlI'.

Example:

from flask_babel import _

# ... do something

logging.info('User request is completed') # Duplicate msg for not getting translated.
return _('User request is completed')     # Is it possible to reduce the duplication?

My expected way to deal with it:

# ... do something
msg, msgid = gettext_and_id('User request is completed')
# `msg` is translated string 'qaStaHvIS qemlI'
# `msgid` is the id 'User request is completed'

logging.info(msgid)
return msg

However I did check the source code of flask-babel and there is no similar functions.
Any suggestion is welcome.

Indeed, I could make a helper function on my own, but I'd like to have a better solution.

def gettext_and_id(msgid):
    return gettext(msgid), msgid

However this solution doesn't fix everything.
If I want to use Some {file} as a string for substitution, then it's still tedious...

filename = 'neverland'
# ... file doesn't exist
msg, msgid = gettext_and_id('{file} is not found')
msg = msg.format(file=filename)
msgid = msgid.format(file=filename)

My question is posted at stackoverflow as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions