diff options
| author | Claude Paroz <claude@2xlibre.net> | 2016-09-29 16:13:10 +0200 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2016-10-01 14:57:16 +0200 |
| commit | fa2f55cfd554c6f99653ffe0d40fd7ca74cb680e (patch) | |
| tree | 9fcc03004ae628a176dca0a98accc9ead4910021 /django | |
| parent | 63bf615d5e5566e23ba698f95e64acfa9f2651ff (diff) | |
Refs #26940 -- Re-allowed makemessages without settings
Thanks Tim Graham for the review.
Diffstat (limited to 'django')
| -rw-r--r-- | django/core/management/commands/makemessages.py | 23 | ||||
| -rw-r--r-- | django/utils/translation/__init__.py | 4 | ||||
| -rw-r--r-- | django/utils/translation/template.py | 5 |
3 files changed, 23 insertions, 9 deletions
diff --git a/django/core/management/commands/makemessages.py b/django/core/management/commands/makemessages.py index 93039ac096..72390e4a75 100644 --- a/django/core/management/commands/makemessages.py +++ b/django/core/management/commands/makemessages.py @@ -11,6 +11,7 @@ from itertools import dropwhile import django from django.conf import settings +from django.core.exceptions import ImproperlyConfigured from django.core.files.temp import NamedTemporaryFile from django.core.management.base import BaseCommand, CommandError from django.core.management.utils import ( @@ -103,13 +104,14 @@ class BuildFile(object): if not self.is_templatized: return - with io.open(self.path, 'r', encoding=settings.FILE_CHARSET) as fp: + encoding = settings.FILE_CHARSET if self.command.settings_available else 'utf-8' + with io.open(self.path, 'r', encoding=encoding) as fp: src_data = fp.read() if self.domain == 'djangojs': content = prepare_js_for_gettext(src_data) elif self.domain == 'django': - content = templatize(src_data, self.path[2:]) + content = templatize(src_data, origin=self.path[2:], charset=encoding) with io.open(self.work_path, 'w', encoding='utf-8') as fp: fp.write(content) @@ -325,7 +327,8 @@ class Command(BaseCommand): self.default_locale_path = self.locale_paths[0] self.invoked_for_django = True else: - self.locale_paths.extend(settings.LOCALE_PATHS) + if self.settings_available: + self.locale_paths.extend(settings.LOCALE_PATHS) # Allow to run makemessages inside an app dir if os.path.isdir('locale'): self.locale_paths.append(os.path.abspath('locale')) @@ -377,6 +380,16 @@ class Command(BaseCommand): else: raise CommandError("Unable to get gettext version. Is it installed?") + @cached_property + def settings_available(self): + try: + settings.LOCALE_PATHS + except ImproperlyConfigured: + if self.verbosity > 1: + self.stderr.write("Running without configured settings.") + return False + return True + def build_potfiles(self): """ Build pot files and apply msguniq to them. @@ -438,7 +451,9 @@ class Command(BaseCommand): norm_patterns.append(p) all_files = [] - ignored_roots = [os.path.normpath(p) for p in (settings.MEDIA_ROOT, settings.STATIC_ROOT) if p] + ignored_roots = [] + if self.settings_available: + ignored_roots = [os.path.normpath(p) for p in (settings.MEDIA_ROOT, settings.STATIC_ROOT) if p] for dirpath, dirnames, filenames in os.walk(root, topdown=True, followlinks=self.symlinks): for dirname in dirnames[:]: if (is_ignored(os.path.normpath(os.path.join(dirpath, dirname)), norm_patterns) or diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py index a5975b005c..39d0f4162b 100644 --- a/django/utils/translation/__init__.py +++ b/django/utils/translation/__init__.py @@ -212,9 +212,9 @@ def get_language_from_path(path): return _trans.get_language_from_path(path) -def templatize(src, origin=None): +def templatize(src, **kwargs): from .template import templatize - return templatize(src, origin) + return templatize(src, **kwargs) def deactivate_all(): diff --git a/django/utils/translation/template.py b/django/utils/translation/template.py index fd7c065256..b8618c4dd2 100644 --- a/django/utils/translation/template.py +++ b/django/utils/translation/template.py @@ -3,7 +3,6 @@ from __future__ import unicode_literals import re import warnings -from django.conf import settings from django.template.base import ( TOKEN_BLOCK, TOKEN_COMMENT, TOKEN_TEXT, TOKEN_VAR, TRANSLATOR_COMMENT_MARK, Lexer, @@ -40,13 +39,13 @@ plural_re = re.compile(r"""^\s*plural$""") constant_re = re.compile(r"""_\(((?:".*?")|(?:'.*?'))\)""") -def templatize(src, origin=None): +def templatize(src, origin=None, charset='utf-8'): """ Turn a Django template into something that is understood by xgettext. It does so by translating the Django translation tags into standard gettext function invocations. """ - src = force_text(src, settings.FILE_CHARSET) + src = force_text(src, charset) out = StringIO('') message_context = None intrans = False |
