Ticket #18731: makemessages-master.patch

File makemessages-master.patch, 8.4 KB (added by Robin Jarry, 10 years ago)

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

  • django/core/management/commands/makemessages.py

    diff --git a/django/core/management/commands/makemessages.py b/django/core/management/commands/makemessages.py
    index a7e9817..72d8dd3 100644
    a b class TranslatableFile(object): 
    4242            return self.file < other.file
    4343        return self.dirpath < other.dirpath
    4444
    45     def process(self, command, potfile, domain, keep_pot=False):
     45    def process(self, command, potfile, domain, keep_pot=False, extra_keywords=None):
    4646        """
    4747        Extract translatable literals from self.file for :param domain:
    4848        creating or updating the :param potfile: POT file.
    class TranslatableFile(object): 
    7373                '--keyword=gettext_lazy',
    7474                '--keyword=ngettext_lazy:1,2',
    7575                '--keyword=pgettext:1c,2',
    76                 '--keyword=npgettext:1c,2,3',
     76                '--keyword=npgettext:1c,2,3'
     77            ]
     78            if extra_keywords:
     79                for kw in extra_keywords:
     80                    args.append('--keyword=%s' % kw)
     81            args += [
    7782                '--from-code=UTF-8',
    7883                '--add-comments=Translators',
    7984                '--output=-'
    class TranslatableFile(object): 
    108113                '--keyword=pgettext:1c,2',
    109114                '--keyword=npgettext:1c,2,3',
    110115                '--keyword=pgettext_lazy:1c,2',
    111                 '--keyword=npgettext_lazy:1c,2,3',
     116                '--keyword=npgettext_lazy:1c,2,3'
     117            ]
     118            if extra_keywords:
     119                for kw in extra_keywords:
     120                    args.append('--keyword=%s' % kw)
     121            args += [
    112122                '--from-code=UTF-8',
    113123                '--add-comments=Translators',
    114124                '--output=-'
    class Command(NoArgsCommand): 
    182192            default=False, help="Remove obsolete message strings."),
    183193        make_option('--keep-pot', action='store_true', dest='keep_pot',
    184194            default=False, help="Keep .pot file after making messages. Useful when debugging."),
     195        make_option('--extra-keyword', dest='extra_keywords', action='append',
     196            help='If you use import aliases for ugettext and its variations, '
     197                 'you can specify it here to make sure that xgettext will find '
     198                 'your translatable strings.'),
    185199    )
    186200    help = ("Runs over the entire source tree of the current directory and "
    187201"pulls out all strings marked for translation. It creates (or updates) a message "
    class Command(NoArgsCommand): 
    207221        self.location = '--no-location' if options.get('no_location') else ''
    208222        self.no_obsolete = options.get('no_obsolete')
    209223        self.keep_pot = options.get('keep_pot')
     224        self.extra_keywords = options.get('extra_keywords')
    210225
    211226        if self.domain not in ('django', 'djangojs'):
    212227            raise CommandError("currently makemessages only supports domains "
    class Command(NoArgsCommand): 
    294309            os.unlink(potfile)
    295310
    296311        for f in file_list:
    297             f.process(self, potfile, self.domain, self.keep_pot)
     312            f.process(self, potfile, self.domain, self.keep_pot, self.extra_keywords)
    298313        return potfile
    299314
    300315    def find_files(self, root):
  • docs/man/django-admin.1

    diff --git a/docs/man/django-admin.1 b/docs/man/django-admin.1
    index 4d937b4..2d4abbf 100644
    a b Executes 
    6060.B sqlall
    6161for the given app(s) in the current database.
    6262.TP
    63 .BI "makemessages [" "\-\-locale=LOCALE" "] [" "\-\-domain=DOMAIN" "] [" "\-\-extension=EXTENSION" "] [" "\-\-all" "] [" "\-\-symlinks" "] [" "\-\-ignore=PATTERN" "] [" "\-\-no\-default\-ignore" "] [" "\-\-no\-wrap" "] [" "\-\-no\-location" "]"
     63.BI "makemessages [" "\-\-locale=LOCALE" "] [" "\-\-domain=DOMAIN" "] [" "\-\-extension=EXTENSION" "] [" "\-\-all" "] [" "\-\-symlinks" "] [" "\-\-ignore=PATTERN" "] [" "\-\-no\-default\-ignore" "] [" "\-\-no\-wrap" "] [" "\-\-no\-location" "] [" "\-\-extra\-keyword" "]"
    6464Runs over the entire source tree of the current directory and pulls out all
    6565strings marked for translation. It creates (or updates) a message file in the
    6666conf/locale (in the django tree) or locale (for project and application) directory.
  • docs/ref/django-admin.txt

    diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt
    index 6277b22..fb85eae 100644
    a b Use the ``--keep-pot`` option to prevent django from deleting the temporary 
    520520.pot file it generates before creating the .po file. This is useful for
    521521debugging errors which may prevent the final language files from being created.
    522522
     523.. django-admin-option:: --extra-keyword
     524
     525.. versionadded:: 1.6
     526
     527When you import the ``ugettext`` and ``ugettext_lazy`` functions as aliases,
     528``makemessages`` may not find translatable strings in your python source code.
     529With the ``--extra-keyword`` option, you can force it to look for other aliases
     530you may choose.
     531
     532Example usage:
     533
     534If you want ``makemessages`` to find translatable strings in the following
     535python code.
     536
     537.. code-block:: python
     538
     539   from django.utils.translation import ugettext as tr, ugettext_lazy as tr_lazy
     540
     541   tr('some translatable string')
     542   tr_lazy('some lazy translatable string')
     543
     544You will need to add the option ``--extra-keyword`` twice::
     545
     546    django-admin.py makemessages --locale=fr_FR --extra-keyword=tr --extra-keyword=tr_lazy
     547
     548.. note:: The default alias ``'_'`` will allways be detected by ``makemessages``
     549   whether you use the ``--extra-keyword`` option or not.
     550
     551
    523552runfcgi [options]
    524553-----------------
    525554
  • tests/i18n/commands/extraction.py

    diff --git a/tests/i18n/commands/extraction.py b/tests/i18n/commands/extraction.py
    index 80e4ee0..83060e3 100644
    a b class MultipleLocaleExtractionTests(ExtractorTests): 
    423423        management.call_command('makemessages', locale='pt,de,ch', verbosity=0)
    424424        self.assertTrue(os.path.exists(self.PO_FILE_PT))
    425425        self.assertTrue(os.path.exists(self.PO_FILE_DE))
     426
     427
     428class UgettextAliasesTests(ExtractorTests):
     429
     430    def test_missing_keyword(self):
     431        os.chdir(self.test_dir)
     432        shutil.copyfile('./ugettext-aliases.sample', './ugettext_aliases.py')
     433        management.call_command('makemessages', locale=LOCALE, verbosity=0)
     434        self.assertTrue(os.path.exists(self.PO_FILE))
     435        with open(self.PO_FILE, 'r') as fp:
     436            po_contents = force_text(fp.read())
     437            self.assertNotMsgId('Should be found only with the --extra-keyword option', po_contents)
     438            self.assertNotMsgId('Lazy, should be found only with the --extra-keyword option', po_contents)
     439        os.remove('./ugettext_aliases.py')
     440
     441    def test_added_keywords(self):
     442        os.chdir(self.test_dir)
     443        shutil.copyfile('./ugettext-aliases.sample', './ugettext_aliases.py')
     444        management.call_command('makemessages', locale=LOCALE, verbosity=0, extra_keywords=['tr', 'tr_lazy'])
     445        self.assertTrue(os.path.exists(self.PO_FILE))
     446        with open(self.PO_FILE, 'r') as fp:
     447            po_contents = force_text(fp.read())
     448            self.assertMsgId('Should be found only with the --extra-keyword option', po_contents)
     449            self.assertMsgId('Lazy, should be found only with the --extra-keyword option', po_contents)
     450        os.remove('./ugettext_aliases.py')
  • new file tests/i18n/commands/ugettext-aliases.sample

    diff --git a/tests/i18n/commands/ugettext-aliases.sample b/tests/i18n/commands/ugettext-aliases.sample
    new file mode 100644
    index 0000000..17c61fe
    - +  
     1from django.utils.translation import ugettext as tr, ugettext_lazy as tr_lazy
     2
     3tr('Should be found only with the --extra-keyword option')
     4tr_lazy('Lazy, should be found only with the --extra-keyword option')
  • tests/i18n/tests.py

    diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py
    index d26a201..89baa7a 100644
    a b if can_run_extraction_tests: 
    3838        JavascriptExtractorTests, IgnoredExtractorTests, SymlinkExtractorTests,
    3939        CopyPluralFormsExtractorTests, NoWrapExtractorTests,
    4040        NoLocationExtractorTests, KeepPotFileExtractorTests,
    41         MultipleLocaleExtractionTests)
     41        MultipleLocaleExtractionTests, UgettextAliasesTests)
    4242if can_run_compilation_tests:
    4343    from .commands.compilation import (PoFileTests, PoFileContentsTests,
    4444        PercentRenderingTests, MultipleLocaleCompilationTests,
Back to Top