Opened 3 years ago

Closed 9 months ago

#18731 closed Cleanup/optimization (fixed)

Document how to pass additional keywords to xgettext in makemessages

Reported by: diabeteman Owned by: nobody
Component: Documentation Version: master
Severity: Normal Keywords: makemessages i18n gettext
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi there,

when doing translations for one of my projects, I noticed that none of the strings that I had identified as "translatable" in python files were gathered in PO files.

After some code browsing, I saw that the "makemessages" management command has a fixed set of --keyword arguments passed to xgettext.

It occurs that I don't use imports with their full name:

from django.utils.translation import ugettext

Nor with the standard "_" alias:

from django.utils.translation import ugettext as _

Because "_" is a dummy variable that I use a lot when doing tuple expansion with unused items (I do use "tr" because it does not mix with other of my stuff but that's not the point here)

It would be cool if the "makemessages" command could accept user defined keywords. I did a small patch myself that accomplishes this. Please take it in consideration :-)

Thanks in advance keep up the great work,

Robin

Attachments (3)

makemessages.patch (10.8 KB) - added by diabeteman 2 years ago.
Adds --extra-keyword to the makemessages command. (with docs and tests)
makemessages-master.patch (8.4 KB) - added by diabeteman 2 years ago.
Adds --extra-keyword to the makemessages command. (with docs and tests)
makemessages-1.5.x.patch (11.1 KB) - added by diabeteman 2 years ago.
Adds --extra-keyword to the makemessages command. (with docs and tests) [for django 1.5.x]

Download all attachments as: .zip

Change History (23)

comment:1 Changed 3 years ago by diabeteman

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

I forgot to explain some stuff about the patch :)

Basically, if you use the i18n functions like this:

from django.utils.translation import ugettext as tr, ugettext_lazy as tr_lazy

tr('some translatable string')
tr_lazy('some lazy translatable string')

You can generate/update the .po files with this command (run into the app directory and/or from the project root)

$ django-admin.py makemessages --locale=fr_FR --extra-keyword=tr --extra-keyword=tr_lazy

It will take all your translatable strings into account.

comment:2 Changed 3 years ago by Anssi Kääriäinen <akaariai@…>

  • Resolution set to fixed
  • Status changed from new to closed

In [c1684e3dcb2adf0fec8fd423cc73122330c268fe]:

Fixed #18731 -- Cleaned up split_exclude's use of can_reuse

The outer query's set of reusable joins (can_reuse) was passed to the
inner query's add_filter call. This was incorrect.

comment:3 Changed 3 years ago by akaariai

  • Resolution fixed deleted
  • Status changed from closed to reopened

Wrong ticket number in commit message -- reopening.

comment:4 Changed 3 years ago by lrekucki

  • Needs documentation set
  • Needs tests set
  • Triage Stage changed from Unreviewed to Accepted

comment:5 Changed 2 years ago by aaugustin

  • Status changed from reopened to new

comment:6 Changed 2 years ago by diabeteman

Hello,

Could this patch be integrated in the next release? It is not a big change :)

Maybe I can help with something?

Thanks,
--Robin

Last edited 2 years ago by diabeteman (previous) (diff)

comment:7 Changed 2 years ago by aaugustin

It looks like the patch needs tests and documentation. Uploading a new diff against master with code, docs and tests changes would help.

comment:8 Changed 2 years ago by diabeteman

Hello,

I wrote docs and tests in a new patch against master. Tell me if I need to change something :)

Changed 2 years ago by diabeteman

Adds --extra-keyword to the makemessages command. (with docs and tests)

comment:9 Changed 2 years ago by diabeteman

Oops,

the patch I uploaded is not complete (missing sample files)
Here are 2 new patches (one for django 1.5.x and one for the master HEAD)

my bad :)

Changed 2 years ago by diabeteman

Adds --extra-keyword to the makemessages command. (with docs and tests)

Changed 2 years ago by diabeteman

Adds --extra-keyword to the makemessages command. (with docs and tests) [for django 1.5.x]

comment:10 Changed 2 years ago by diabeteman

I made a pull request on github :)

https://github.com/django/django/pull/959

comment:11 Changed 18 months ago by timo

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement set

Patch no longer applies cleanly (and docs need updating for the fact that this would go in 1.7).

comment:12 Changed 18 months ago by diabeteman

The patch has been reworked on the HEAD.

Here's a new pull request: https://github.com/django/django/pull/2311

comment:13 Changed 17 months ago by diabeteman

  • Patch needs improvement unset
  • Triage Stage changed from Accepted to Ready for checkin

Gentle bump.

I hope it is not too late for integration in 1.7 :-)

comment:14 Changed 17 months ago by timo

  • Triage Stage changed from Ready for checkin to Accepted

You need someone besides yourself to review the patch in order to mark it RFC, thanks.

comment:15 Changed 17 months ago by diabeteman

oops sorry :-/

comment:16 Changed 17 months ago by claudep

  • Component changed from Core (Management commands) to Internationalization
  • Needs documentation set
  • Patch needs improvement set
  • Type changed from New feature to Cleanup/optimization
  • Version changed from 1.4 to master

In my recent commit in makemessages ([06efeae598c6dafbe56d2ea323a0dccdd5bf2b8e]), I added an options list for various gettext commands at the Command level.

Therefore, the new way to solve this issue is to create a custom makemessages command in one of your project's apps, and complete the Command.xgettext_options list. I'm keeping this ticket open so as to add an example in the documentation.

comment:17 Changed 12 months ago by tkhyn

To people who - like me lately - would land here after a google search and want to add gettext extra keywords to Django < 1.7 without having to patch Django, you may want to use this management command override which will work with Django 1.6 and earlier. Or simply use the django-extra_keywords package.

It uses an approach that differs from what is suggested above, by monkey-patching popen_wrapper (>= 1.6) or _popen (< 1.6) in core.management.commands.makemessages and adding arguments to the command. A bit hackish, but it does the job.

comment:18 Changed 9 months ago by timgraham

  • Component changed from Internationalization to Documentation
  • Needs documentation unset
  • Patch needs improvement unset

Berker created a documentation pull request as suggested by Claude.

comment:19 Changed 9 months ago by timgraham

  • Summary changed from "makemessages" management command should handle import aliases to Document how to pass additional keywords to xgettext in makemessages

The patch looks okay to me, but Claude should review as well as I'm not a makemessages user.

comment:20 Changed 9 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from new to closed

In cbd936d0f87cf5109d3ef67c07797c657912f424:

Fixed #18731 -- Added an example about customizing "makemessages" command.

Thanks claudp for the suggestion and review.

Note: See TracTickets for help on using tickets.
Back to Top