Skip to content

extract will return literals that are part of an expression #415

Open

Description

@asottile
_('foo' + bar)
_(bar['foo'])

Though those are both incorrect usages of gettext, neither should result in 'foo' being picked up as a localized string.

However:

 $ pybabel extract foo.py -o /dev/stdout
extracting messages from foo.py
writing PO template file to /dev/stdout
# Translations template for PROJECT.
# Copyright (C) 2016 ORGANIZATION
# This file is distributed under the same license as the PROJECT project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2016.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2016-06-06 18:32-0700\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.3.4\n"

#: foo.py:1 foo.py:2
msgid "foo"
msgstr ""

Activity

  1. asottile commented on Jun 7, 2016

    @asottile
    ContributorAuthor

    fwiw, xgettext also picks up the first one but not the second one -- I still think picking up the first one is bug as well :)

    $ xgettext test.py -o /dev/stdout
    # SOME DESCRIPTIVE TITLE.
    # Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
    # This file is distributed under the same license as the PACKAGE package.
    # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
    #
    #, fuzzy
    msgid ""
    msgstr ""
    "Project-Id-Version: PACKAGE VERSION\n"
    "Report-Msgid-Bugs-To: \n"
    "POT-Creation-Date: 2016-06-06 18:36-0700\n"
    "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
    "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
    "Language-Team: LANGUAGE <LL@li.org>\n"
    "Language: \n"
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=CHARSET\n"
    "Content-Transfer-Encoding: 8bit\n"
    
    #: test.py:1
    msgid "foo"
    msgstr ""
    
  2. akx commented on Jul 15, 2016

    @akx
    Member

    Ungh, I think fixing this will require one of the following:

    • A dirty hack (lookahead/lookbehind in the token stream to make sure we're not in, or going to be in, an expression)
    • Rewriting the extractor to use the AST module instead of the token stream (likely slower, though probably not by an awful lot). Also, I don't think it's much of an issue, but this would make parsing (more?) Python version dependent.

    I'd honestly go with the second option.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions