Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

babel.cfg keywords? #71

Open
jacobsvante opened this issue Nov 30, 2013 · 6 comments
Open

babel.cfg keywords? #71

jacobsvante opened this issue Nov 30, 2013 · 6 comments

Comments

@jacobsvante
Copy link

I'm finding it hard to locate documentation on how to specify keywords in my babel.cfg. I have a config file that looks like this:

[python: **.py]
encoding = utf-8
[jinja2: **/templates/**.html]
encoding = utf-8
extensions=jinja2.ext.autoescape,jinja2.ext.with_
[javascript: .tmp/**.js]
encoding = utf-8
keywords = translate, ifPlural

All messages are extracted as expected except javascript. I've tried just setting one keyword in the config, changing the key from keywords to extract_messages, and keyword but none seem to work. I've checked what keywords get passed to babel.messages.extract.extract_javascript (by simply doing sys.exit(keywords) at the start of the function) and they never appear. However, doing pybabel extract -k translate -k ifPlural works just fine.

@buma
Copy link

buma commented Apr 11, 2014

I found the same problem and after looking at the source I think I something is missing or I don't understand something.

This is my babel.cfg

[javascript:*.js]
encoding = utf-8
add_comments = TRANSLATORS:
keywords = npgettext,ifPlural

in frontend.py in function parse_mapping babel.cfg is parsed. And you get out as expected:

[('*.js', 'javascript')]
{'*.js': {'keywords': 'npgettext,ifPlural', 'add_comments': 'TRANSLATORS:', 'encoding': 'utf-8'}}

Then we come to extract from dir function, which calles extract_from_file -> extract then extract_javascript is called. None of previously mentioned options didn't even touch options parsed from cfg file. And also in extract_javascript the only thing that is read from cfg is encoding. Interestingly in extract_python it is the same story.

I tried to fix it so that I add "add_comments" key and "keywords" to comment_tags and keywords in extract_javascript. Adding to comment_tags worked. Adding to keywords didn't because you need parse_keywords function which is in fronted. So I think that in frontend.py or extract.py something is missing.

@jacobsvante
Copy link
Author

@mitsuhiko have u looked at this? Would be nice if someone who knows the source would take a look at it.

@dmitriid
Copy link

dmitriid commented Aug 11, 2016

Two years later the issue is still here. pybabel 2.3.4

According to the docs:

# Extraction from JavaScript files

[javascript: **.js]
extract_messages = $._, jQuery._

However, babel (specifically, CLI) ignores any values in extract_messages and only extracts strings wrapped with _()

In my case:
babel.cfg:

[javascript: **.js]
extract_messages = __

Then in app.js:

// note _ vs __
_("Atmosphere") + __("Anything else")

Then run babel:

$ pybabel extract -F babel.cfg --sort-by-file -o ./dist/lang/en.pot ./dist/app.js
extracting messages from ./dist/app.js (extract_messages="__")
writing PO template file to ./dist/lang/en.pot

Result:

# This file is distributed under the same license as the keyflow-website
# project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2016.
#
#, fuzzy
msgid ""
msgstr ""
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2016-08-11 11:25+0200\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"

#: dist/app.js:1
msgid "Atmosphere"
msgstr ""

If you change __ to _, strings are extracted. It doesn't matter what I change extract_messages to, it's always ignored

@dmitriid
Copy link

Ok. More weirdness.

Babel CLI ignores babel.cfg, but --keywords option is also weird.

babel.cfg

[javascript: **.js]
extract_messages = __
keywords = __

Babel CLI ignores this.

But run it with --keywords:

$ pybabel extract -F babel.cfg --sort-by-file -o ./dist/lang/en.pot --keywords=__ ./dist/app.js
extracting messages from ./dist/app.js (keywords="__", extract_messages="__")
writing PO template file to ./dist/lang/en.pot

And here's the result. It extracted both _ and __:

# This file is distributed under the same license as the keyflow-website
# project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2016.
#
#, fuzzy
msgid ""
msgstr ""
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2016-08-11 11:39+0200\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"

#: dist/app.js:3387
msgid "Atmosphere"
msgstr ""

#: dist/app.js:3387
msgid "Anything else"
msgstr ""

@dmitriid
Copy link

@scop @akx @mbirtwell Alerting you to this old/weird issue :)

@remram44
Copy link

There still doesn't seem to be any way to specify keywords in the mapping file :( I'm considering duplicating the keywords in my extractor as both an option (specifiable in mapping file, not cli) and keywords (specifiable on cli, not mapping file), but it sounds like this would be very easy to support in Babel.

seppeljordan added a commit to ida-arbeitszeit/arbeitszeitapp that referenced this issue Nov 28, 2023
This change was made because of bug in `pybabel` which we hit.

python-babel/babel#71
seppeljordan added a commit to ida-arbeitszeit/arbeitszeitapp that referenced this issue Nov 28, 2023
This change was made because of bug in `pybabel` which we hit.

python-babel/babel#71
seppeljordan added a commit to ida-arbeitszeit/arbeitszeitapp that referenced this issue Nov 28, 2023
This change was made because of bug in `pybabel` which we hit.

python-babel/babel#71
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants