mirror of
https://github.com/django/django.git
synced 2025-08-10 20:09:12 +00:00
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.
This commit is contained in:
parent
b8ba73cd0c
commit
5ec367ccdd
@ -14,7 +14,7 @@ from django.core.management.base import CommandError, BaseCommand
|
|||||||
from django.core.management.utils import (handle_extensions, find_command,
|
from django.core.management.utils import (handle_extensions, find_command,
|
||||||
popen_wrapper)
|
popen_wrapper)
|
||||||
from django.utils.encoding import force_str
|
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 import six
|
||||||
from django.utils.text import get_text_list
|
from django.utils.text import get_text_list
|
||||||
from django.utils.jslex import prepare_js_for_gettext
|
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))
|
command.stdout.write('processing file %s in %s\n' % (self.file, self.dirpath))
|
||||||
_, file_ext = os.path.splitext(self.file)
|
_, file_ext = os.path.splitext(self.file)
|
||||||
if domain == 'djangojs' and file_ext in command.extensions:
|
if domain == 'djangojs' and file_ext in command.extensions:
|
||||||
is_templatized = True
|
|
||||||
orig_file = os.path.join(self.dirpath, self.file)
|
orig_file = os.path.join(self.dirpath, self.file)
|
||||||
with io.open(orig_file, encoding=settings.FILE_CHARSET) as fp:
|
work_file = orig_file
|
||||||
src_data = fp.read()
|
is_templatized = command.gettext_version < (0, 18, 3)
|
||||||
src_data = prepare_js_for_gettext(src_data)
|
if is_templatized:
|
||||||
thefile = '%s.c' % self.file
|
with io.open(orig_file, 'r', encoding=settings.FILE_CHARSET) as fp:
|
||||||
work_file = os.path.join(self.dirpath, thefile)
|
src_data = fp.read()
|
||||||
with io.open(work_file, "w", encoding='utf-8') as fp:
|
src_data = prepare_js_for_gettext(src_data)
|
||||||
fp.write(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 = [
|
args = [
|
||||||
'xgettext',
|
'xgettext',
|
||||||
'-d', domain,
|
'-d', domain,
|
||||||
'--language=C',
|
'--language=%s' % ('C' if is_templatized else 'JavaScript',),
|
||||||
'--keyword=gettext_noop',
|
'--keyword=gettext_noop',
|
||||||
'--keyword=gettext_lazy',
|
'--keyword=gettext_lazy',
|
||||||
'--keyword=ngettext_lazy:1,2',
|
'--keyword=ngettext_lazy:1,2',
|
||||||
@ -85,17 +86,16 @@ class TranslatableFile(object):
|
|||||||
] + command.xgettext_options
|
] + command.xgettext_options
|
||||||
args.append(work_file)
|
args.append(work_file)
|
||||||
elif domain == 'django' and (file_ext == '.py' or file_ext in command.extensions):
|
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)
|
orig_file = os.path.join(self.dirpath, self.file)
|
||||||
|
work_file = orig_file
|
||||||
is_templatized = file_ext in command.extensions
|
is_templatized = file_ext in command.extensions
|
||||||
if is_templatized:
|
if is_templatized:
|
||||||
with io.open(orig_file, 'r', encoding=settings.FILE_CHARSET) as fp:
|
with io.open(orig_file, 'r', encoding=settings.FILE_CHARSET) as fp:
|
||||||
src_data = fp.read()
|
src_data = fp.read()
|
||||||
thefile = '%s.py' % self.file
|
|
||||||
content = templatize(src_data, orig_file[2:])
|
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)
|
fp.write(content)
|
||||||
work_file = os.path.join(self.dirpath, thefile)
|
|
||||||
args = [
|
args = [
|
||||||
'xgettext',
|
'xgettext',
|
||||||
'-d', domain,
|
'-d', domain,
|
||||||
@ -307,6 +307,15 @@ class Command(BaseCommand):
|
|||||||
if not self.keep_pot:
|
if not self.keep_pot:
|
||||||
self.remove_potfiles()
|
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):
|
def build_potfiles(self):
|
||||||
"""
|
"""
|
||||||
Build pot files and apply msguniq to them.
|
Build pot files and apply msguniq to them.
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
// '
|
// '
|
||||||
gettext('This literal should be included.')
|
gettext('This literal should be included.')
|
||||||
|
gettext_noop('gettext_noop should, too.');
|
||||||
x = y; // '
|
x = y; // '
|
||||||
gettext("This one as well.")
|
gettext("This one as well.")
|
||||||
|
|
||||||
|
@ -352,6 +352,7 @@ class JavascriptExtractorTests(ExtractorTests):
|
|||||||
os.chdir(self.test_dir)
|
os.chdir(self.test_dir)
|
||||||
_, po_contents = self._run_makemessages(domain='djangojs')
|
_, po_contents = self._run_makemessages(domain='djangojs')
|
||||||
self.assertMsgId('This literal should be included.', po_contents)
|
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('This one as well.', po_contents)
|
||||||
self.assertMsgId(r'He said, \"hello\".', po_contents)
|
self.assertMsgId(r'He said, \"hello\".', po_contents)
|
||||||
self.assertMsgId("okkkk", po_contents)
|
self.assertMsgId("okkkk", po_contents)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user