Skip to content
Browse files
filtering custom extractor that uses the js extractor for svelte files (

* use custom extractor for svelte files

* use a different, and perhaps more-solid approach
  • Loading branch information
escattone committed Nov 29, 2022
1 parent 9f5f3d4 commit 1f97cc3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
@@ -1,8 +1,9 @@
jinja2_custom = kitsune.lib.babel:extract_jinja
jinja2_custom = kitsune.lib.babel:extract_jinja
svelte_custom = kitsune.lib.babel:extract_svelte

[ignore: kitsune/**/static/**/js/*-all.js]
[ignore: kitsune/**/static/**/js/*-min.js]
[javascript: kitsune/**/static/**/js/*.js]
[javascript: svelte/**/*.svelte]
[svelte_custom: svelte/**/*.svelte]
[jinja2_custom: kitsune/**/static/**/tpl/**.njk]
@@ -1,8 +1,14 @@
from io import BytesIO

from babel.messages.extract import extract_javascript
import django
from django.conf import settings
from jinja2.ext import babel_extract

SVELTE_SPECIAL_TAG_PREFIXES = set([b"{#", b"{:", b"{/", b"{@"])

def generate_option(value):
Generate option to pass to babel_extract() from a TEMPLATES['OPTION'] value
@@ -18,6 +24,36 @@ def generate_option(value):
return value

def extract_svelte(fileobj, keywords, comment_tags, options):
Custom extractor for Svelte files.
Since Babel's built-in JavaScript extractor is designed to handle JSX
files as well plain JavaScript files, it can also handle nearly all of
the syntax within a Svelte file. What it doesn't always handle well are
the statements that open, continue, and close Svelte logic blocks. They
sometimes confuse it such that it fails to recognize some localizable
strings. This custom extractor essentially hides those statements from
the underlying Babel JavaScript extractor, while maintaining the same
line numbers as the original Svelte file.
NOTE: This approach assumes that any special Svelte tags always exist
on their own line without anything else on that line. Of course,
this approach is not as elegant as using a full-blown parser for
Svelte files, but it's much easier and will work for our current
practical purposes.
filtered = BytesIO()
for line in fileobj:
if line.lstrip()[:2] in SVELTE_SPECIAL_TAG_PREFIXES:
# Write a new line to maintain equivalent line numbers.
return extract_javascript(filtered, keywords, comment_tags, options)

def extract_jinja(fileobj, keywords, comment_tags, options):
Wrapper around jinja2's babel_extract() that sets the relevant options by

0 comments on commit 1f97cc3

Please sign in to comment.