summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2024-08-27 21:31:07 +0200
committerClaude Paroz <claude@2xlibre.net>2024-08-30 13:39:04 +0200
commit2c1f27d0d0346889e3af3b1be03e39e7117b1165 (patch)
treee040d3b366798108ce635ad4ea5f36d6192417d0 /django
parent2ff00251f929cc3e014dd447f6847196e66e69b8 (diff)
Dropped safeguards against very old versions of gettext.
gettext 0.19 was released in 2014.
Diffstat (limited to 'django')
-rw-r--r--django/core/management/commands/compilemessages.py4
-rw-r--r--django/core/management/commands/makemessages.py27
-rw-r--r--django/utils/jslex.py250
3 files changed, 8 insertions, 273 deletions
diff --git a/django/core/management/commands/compilemessages.py b/django/core/management/commands/compilemessages.py
index eddf31b794..c56e2a237c 100644
--- a/django/core/management/commands/compilemessages.py
+++ b/django/core/management/commands/compilemessages.py
@@ -79,8 +79,8 @@ class Command(BaseCommand):
if find_command(self.program) is None:
raise CommandError(
- "Can't find %s. Make sure you have GNU gettext "
- "tools 0.15 or newer installed." % self.program
+ f"Can't find {self.program}. Make sure you have GNU gettext "
+ "tools 0.19 or newer installed."
)
basedirs = [os.path.join("conf", "locale"), "locale"]
diff --git a/django/core/management/commands/makemessages.py b/django/core/management/commands/makemessages.py
index dc3d4026e2..076667d41a 100644
--- a/django/core/management/commands/makemessages.py
+++ b/django/core/management/commands/makemessages.py
@@ -19,7 +19,6 @@ from django.core.management.utils import (
)
from django.utils.encoding import DEFAULT_LOCALE_ENCODING
from django.utils.functional import cached_property
-from django.utils.jslex import prepare_js_for_gettext
from django.utils.regex_helper import _lazy_re_compile
from django.utils.text import get_text_list
from django.utils.translation import templatize
@@ -35,8 +34,8 @@ def check_programs(*programs):
for program in programs:
if find_command(program) is None:
raise CommandError(
- "Can't find %s. Make sure you have GNU gettext tools 0.15 or "
- "newer installed." % program
+ f"Can't find {program}. Make sure you have GNU gettext tools "
+ "0.19 or newer installed."
)
@@ -80,9 +79,7 @@ class BuildFile:
@cached_property
def is_templatized(self):
- if self.domain == "djangojs":
- return self.command.gettext_version < (0, 18, 3)
- elif self.domain == "django":
+ if self.domain == "django":
file_ext = os.path.splitext(self.translatable.file)[1]
return file_ext != ".py"
return False
@@ -99,11 +96,7 @@ class BuildFile:
"""
if not self.is_templatized:
return self.path
- extension = {
- "djangojs": "c",
- "django": "py",
- }.get(self.domain)
- filename = "%s.%s" % (self.translatable.file, extension)
+ filename = f"{self.translatable.file}.py"
return os.path.join(self.translatable.dirpath, filename)
def preprocess(self):
@@ -117,9 +110,7 @@ class BuildFile:
with open(self.path, encoding="utf-8") as fp:
src_data = fp.read()
- if self.domain == "djangojs":
- content = prepare_js_for_gettext(src_data)
- elif self.domain == "django":
+ if self.domain == "django":
content = templatize(src_data, origin=self.path[2:])
with open(self.work_path, "w", encoding="utf-8") as fp:
@@ -349,11 +340,6 @@ class Command(BaseCommand):
self.msgattrib_options = self.msgattrib_options[:] + ["--no-location"]
self.xgettext_options = self.xgettext_options[:] + ["--no-location"]
if options["add_location"]:
- if self.gettext_version < (0, 19):
- raise CommandError(
- "The --add-location option requires gettext 0.19 or later. "
- "You have %s." % ".".join(str(x) for x in self.gettext_version)
- )
arg_add_location = "--add-location=%s" % options["add_location"]
self.msgmerge_options = self.msgmerge_options[:] + [arg_add_location]
self.msguniq_options = self.msguniq_options[:] + [arg_add_location]
@@ -636,12 +622,11 @@ class Command(BaseCommand):
build_files.append(build_file)
if self.domain == "djangojs":
- is_templatized = build_file.is_templatized
args = [
"xgettext",
"-d",
self.domain,
- "--language=%s" % ("C" if is_templatized else "JavaScript",),
+ "--language=JavaScript",
"--keyword=gettext_noop",
"--keyword=gettext_lazy",
"--keyword=ngettext_lazy:1,2",
diff --git a/django/utils/jslex.py b/django/utils/jslex.py
deleted file mode 100644
index fc46a686c7..0000000000
--- a/django/utils/jslex.py
+++ /dev/null
@@ -1,250 +0,0 @@
-"""JsLex: a lexer for JavaScript"""
-
-# Originally from https://bitbucket.org/ned/jslex
-import re
-
-
-class Tok:
- """
- A specification for a token class.
- """
-
- num = 0
-
- def __init__(self, name, regex, next=None):
- self.id = Tok.num
- Tok.num += 1
- self.name = name
- self.regex = regex
- self.next = next
-
-
-def literals(choices, prefix="", suffix=""):
- """
- Create a regex from a space-separated list of literal `choices`.
-
- If provided, `prefix` and `suffix` will be attached to each choice
- individually.
- """
- return "|".join(prefix + re.escape(c) + suffix for c in choices.split())
-
-
-class Lexer:
- """
- A generic multi-state regex-based lexer.
- """
-
- def __init__(self, states, first):
- self.regexes = {}
- self.toks = {}
-
- for state, rules in states.items():
- parts = []
- for tok in rules:
- groupid = "t%d" % tok.id
- self.toks[groupid] = tok
- parts.append("(?P<%s>%s)" % (groupid, tok.regex))
- self.regexes[state] = re.compile("|".join(parts), re.MULTILINE | re.VERBOSE)
-
- self.state = first
-
- def lex(self, text):
- """
- Lexically analyze `text`.
-
- Yield pairs (`name`, `tokentext`).
- """
- end = len(text)
- state = self.state
- regexes = self.regexes
- toks = self.toks
- start = 0
-
- while start < end:
- for match in regexes[state].finditer(text, start):
- name = match.lastgroup
- tok = toks[name]
- toktext = match[name]
- start += len(toktext)
- yield (tok.name, toktext)
-
- if tok.next:
- state = tok.next
- break
-
- self.state = state
-
-
-class JsLexer(Lexer):
- """
- A JavaScript lexer
-
- >>> lexer = JsLexer()
- >>> list(lexer.lex("a = 1"))
- [('id', 'a'), ('ws', ' '), ('punct', '='), ('ws', ' '), ('dnum', '1')]
-
- This doesn't properly handle non-ASCII characters in the JavaScript source.
- """
-
- # Because these tokens are matched as alternatives in a regex, longer
- # possibilities must appear in the list before shorter ones, for example,
- # '>>' before '>'.
- #
- # Note that we don't have to detect malformed JavaScript, only properly
- # lex correct JavaScript, so much of this is simplified.
-
- # Details of JavaScript lexical structure are taken from
- # https://www.ecma-international.org/publications-and-standards/standards/ecma-262/
-
- # A useful explanation of automatic semicolon insertion is at
- # http://inimino.org/~inimino/blog/javascript_semicolons
-
- both_before = [
- Tok("comment", r"/\*(.|\n)*?\*/"),
- Tok("linecomment", r"//.*?$"),
- Tok("ws", r"\s+"),
- Tok(
- "keyword",
- literals(
- """
- break case catch class const continue debugger
- default delete do else enum export extends
- finally for function if import in instanceof
- new return super switch this throw try typeof
- var void while with
- """,
- suffix=r"\b",
- ),
- next="reg",
- ),
- Tok("reserved", literals("null true false", suffix=r"\b"), next="div"),
- Tok(
- "id",
- r"""
- ([a-zA-Z_$ ]|\\u[0-9a-fA-Z]{4}) # first char
- ([a-zA-Z_$0-9]|\\u[0-9a-fA-F]{4})* # rest chars
- """,
- next="div",
- ),
- Tok("hnum", r"0[xX][0-9a-fA-F]+", next="div"),
- Tok("onum", r"0[0-7]+"),
- Tok(
- "dnum",
- r"""
- ( (0|[1-9][0-9]*) # DecimalIntegerLiteral
- \. # dot
- [0-9]* # DecimalDigits-opt
- ([eE][-+]?[0-9]+)? # ExponentPart-opt
- |
- \. # dot
- [0-9]+ # DecimalDigits
- ([eE][-+]?[0-9]+)? # ExponentPart-opt
- |
- (0|[1-9][0-9]*) # DecimalIntegerLiteral
- ([eE][-+]?[0-9]+)? # ExponentPart-opt
- )
- """,
- next="div",
- ),
- Tok(
- "punct",
- literals(
- """
- >>>= === !== >>> <<= >>= <= >= == != << >> &&
- || += -= *= %= &= |= ^=
- """
- ),
- next="reg",
- ),
- Tok("punct", literals("++ -- ) ]"), next="div"),
- Tok("punct", literals("{ } ( [ . ; , < > + - * % & | ^ ! ~ ? : ="), next="reg"),
- Tok("string", r'"([^"\\]|(\\(.|\n)))*?"', next="div"),
- Tok("string", r"'([^'\\]|(\\(.|\n)))*?'", next="div"),
- ]
-
- both_after = [
- Tok("other", r"."),
- ]
-
- states = {
- # slash will mean division
- "div": both_before
- + [
- Tok("punct", literals("/= /"), next="reg"),
- ]
- + both_after,
- # slash will mean regex
- "reg": both_before
- + [
- Tok(
- "regex",
- r"""
- / # opening slash
- # First character is..
- ( [^*\\/[] # anything but * \ / or [
- | \\. # or an escape sequence
- | \[ # or a class, which has
- ( [^\]\\] # anything but \ or ]
- | \\. # or an escape sequence
- )* # many times
- \]
- )
- # Following characters are same, except for excluding a star
- ( [^\\/[] # anything but \ / or [
- | \\. # or an escape sequence
- | \[ # or a class, which has
- ( [^\]\\] # anything but \ or ]
- | \\. # or an escape sequence
- )* # many times
- \]
- )* # many times
- / # closing slash
- [a-zA-Z0-9]* # trailing flags
- """,
- next="div",
- ),
- ]
- + both_after,
- }
-
- def __init__(self):
- super().__init__(self.states, "reg")
-
-
-def prepare_js_for_gettext(js):
- """
- Convert the JavaScript source `js` into something resembling C for
- xgettext.
-
- What actually happens is that all the regex literals are replaced with
- "REGEX".
- """
-
- def escape_quotes(m):
- """Used in a regex to properly escape double quotes."""
- s = m[0]
- if s == '"':
- return r"\""
- else:
- return s
-
- lexer = JsLexer()
- c = []
- for name, tok in lexer.lex(js):
- if name == "regex":
- # C doesn't grok regexes, and they aren't needed for gettext,
- # so just output a string instead.
- tok = '"REGEX"'
- elif name == "string":
- # C doesn't have single-quoted strings, so make all strings
- # double-quoted.
- if tok.startswith("'"):
- guts = re.sub(r"\\.|.", escape_quotes, tok[1:-1])
- tok = '"' + guts + '"'
- elif name == "id":
- # C can't deal with Unicode escapes in identifiers. We don't
- # need them for gettext anyway, so replace them with something
- # innocuous
- tok = tok.replace("\\", "U")
- c.append(tok)
- return "".join(c)