From 5ec367ccdd2ff70270b1f578821c817785d7aecf Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Tue, 11 Nov 2014 09:40:03 +0100 Subject: [PATCH] Fixed #23788 -- Used new JavaScript support in recent gettext JavaScript string extraction support has been added in gettext 0.18.3. Thanks Aymeric Augustin for the review. --- .../core/management/commands/makemessages.py | 37 ++++++++++++------- tests/i18n/commands/javascript.js | 1 + tests/i18n/test_extraction.py | 1 + 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/django/core/management/commands/makemessages.py b/django/core/management/commands/makemessages.py index 4f6cad6832..6c22dfe1ea 100644 --- a/django/core/management/commands/makemessages.py +++ b/django/core/management/commands/makemessages.py @@ -14,7 +14,7 @@ from django.core.management.base import CommandError, BaseCommand from django.core.management.utils import (handle_extensions, find_command, popen_wrapper) from django.utils.encoding import force_str -from django.utils.functional import total_ordering +from django.utils.functional import cached_property, total_ordering from django.utils import six from django.utils.text import get_text_list from django.utils.jslex import prepare_js_for_gettext @@ -63,19 +63,20 @@ class TranslatableFile(object): command.stdout.write('processing file %s in %s\n' % (self.file, self.dirpath)) _, file_ext = os.path.splitext(self.file) if domain == 'djangojs' and file_ext in command.extensions: - is_templatized = True orig_file = os.path.join(self.dirpath, self.file) - with io.open(orig_file, encoding=settings.FILE_CHARSET) as fp: - src_data = fp.read() - src_data = prepare_js_for_gettext(src_data) - thefile = '%s.c' % self.file - work_file = os.path.join(self.dirpath, thefile) - with io.open(work_file, "w", encoding='utf-8') as fp: - fp.write(src_data) + work_file = orig_file + is_templatized = command.gettext_version < (0, 18, 3) + if is_templatized: + with io.open(orig_file, 'r', encoding=settings.FILE_CHARSET) as fp: + src_data = fp.read() + src_data = prepare_js_for_gettext(src_data) + work_file = os.path.join(self.dirpath, '%s.c' % self.file) + with io.open(work_file, "w", encoding='utf-8') as fp: + fp.write(src_data) args = [ 'xgettext', '-d', domain, - '--language=C', + '--language=%s' % ('C' if is_templatized else 'JavaScript',), '--keyword=gettext_noop', '--keyword=gettext_lazy', '--keyword=ngettext_lazy:1,2', @@ -85,17 +86,16 @@ class TranslatableFile(object): ] + command.xgettext_options args.append(work_file) elif domain == 'django' and (file_ext == '.py' or file_ext in command.extensions): - thefile = self.file orig_file = os.path.join(self.dirpath, self.file) + work_file = orig_file is_templatized = file_ext in command.extensions if is_templatized: with io.open(orig_file, 'r', encoding=settings.FILE_CHARSET) as fp: src_data = fp.read() - thefile = '%s.py' % self.file content = templatize(src_data, orig_file[2:]) - with io.open(os.path.join(self.dirpath, thefile), "w", encoding='utf-8') as fp: + work_file = os.path.join(self.dirpath, '%s.py' % self.file) + with io.open(work_file, "w", encoding='utf-8') as fp: fp.write(content) - work_file = os.path.join(self.dirpath, thefile) args = [ 'xgettext', '-d', domain, @@ -307,6 +307,15 @@ class Command(BaseCommand): if not self.keep_pot: self.remove_potfiles() + @cached_property + def gettext_version(self): + out, err, status = popen_wrapper(['xgettext', '--version']) + m = re.search(r'(\d)\.(\d+)\.(\d+)', out) + if m: + return tuple(int(d) for d in m.groups()) + else: + raise CommandError("Unable to get gettext version. Is it installed?") + def build_potfiles(self): """ Build pot files and apply msguniq to them. diff --git a/tests/i18n/commands/javascript.js b/tests/i18n/commands/javascript.js index fa059d70f4..76eb5bc9fb 100644 --- a/tests/i18n/commands/javascript.js +++ b/tests/i18n/commands/javascript.js @@ -1,5 +1,6 @@ // ' gettext('This literal should be included.') +gettext_noop('gettext_noop should, too.'); x = y; // ' gettext("This one as well.") diff --git a/tests/i18n/test_extraction.py b/tests/i18n/test_extraction.py index 459de45c18..b2972105c7 100644 --- a/tests/i18n/test_extraction.py +++ b/tests/i18n/test_extraction.py @@ -352,6 +352,7 @@ class JavascriptExtractorTests(ExtractorTests): os.chdir(self.test_dir) _, po_contents = self._run_makemessages(domain='djangojs') self.assertMsgId('This literal should be included.', po_contents) + self.assertMsgId('gettext_noop should, too.', po_contents) self.assertMsgId('This one as well.', po_contents) self.assertMsgId(r'He said, \"hello\".', po_contents) self.assertMsgId("okkkk", po_contents)