Skip to content

Commit

Permalink
Update Django i18n lint tool
Browse files Browse the repository at this point in the history
This commit updates the Django i18n lint tool by making the following changes:

- Added `.venv` and `.env` to the `.gitignore` file.
- Updated the `tox.ini` file to use `py310` as the Python environment.
- Added `pytest` as a dependency in the `tox.ini` file.
- Removed the `vim-helpers.vim` file.
- Updated the `setup.py` file to change the package name to `django-i18n-lint` and the description to "Lint tool to find untranslated text in Django templates".
- Updated the `tests.py` file to import `django_i18n_lint` instead of `django_template_i18n_lint`.
- Updated the `README.md` file to reflect the changes in the template tag names and added usage examples.
  • Loading branch information
jkaeske committed Dec 28, 2023
1 parent 5d0d728 commit 5b75a14
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 44 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
.tox
.coverage
dist/*
.venv
.env
44 changes: 32 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
Django Template i18n lint
=========================
# Django i18n Lint

[![Build Status](https://travis-ci.org/rory/django-template-i18n-lint.png?branch=master)](https://travis-ci.org/rory/django-template-i18n-lint)
[![Coverage Status](https://coveralls.io/repos/rory/django-template-i18n-lint/badge.png?branch=master)](https://coveralls.io/r/rory/django-template-i18n-lint?branch=master)
[![PyPI version](https://pypip.in/v/django-template-i18n-lint/badge.png)](https://pypi.python.org/pypi/django-template-i18n-lint)
[![PyPI Downloads](https://pypip.in/d/django-template-i18n-lint/badge.png)](https://pypi.python.org/pypi/django-template-i18n-lint)
A simple script to find non-i18n text in a Django template.

Note: Since Django 3.1 the template tag was update from `{% trans "" %}` to `{% translate "" %}` and from `{% blocktrans "" %}` to `{% blocktranslate "" %}`.
The old `trans` versions are still recognized by the linter but the string wrapping will change the string to the new `translate` version.

A simple script to find non-i18n text in a Django template.
## Usage

It can also automatically wrap the strings in `{% trans "" %}` tags, by running it with the `-r` command-line flag.
The translation will be written to a new file, `<filename>_translated.html`.
To use the Django Template i18n lint tool, you can run the `django_i18n_lint.py` script from the command line like so:

For more info see [Lint tool to find non-i18n strings in a django template](http://www.technomancy.org/python/django-template-i18n-lint/)
```bash
python django_i18n_lint.py [options] <path_to_your_template>
```

Code is copyright Rory McCann 2013, and dual licenced under the GNU GPL version3 (or at your option a later version), and the BSD licence. See the files LICENCE.GPLv3 and LICENCE.BSD for more information
Running this command without any additonal options will output any non-i18n text found in the specified Django template to the command line.

Please replace `<path_to_your_template>` with the actual path to your Django template file.

### Options
- `-r`, `--replace`: Ask to wrap the strings in the file in `{% translate "" %}` tags.
- `-o`, `--overwrite`: When replacing the strings, overwrite the original file. If not specified, the file will be saved in a separate file named `X_translated.html`.
- `-f`, `--force`: Force to wrap strings with no questions.
- `-e`, `--exclude`: Exclude these filenames from being linted. This option can be used multiple times to exclude multiple files.
- `-x`, `--accept`: Exclude these regexes from results. This option can be used multiple times to exclude multiple regexes.

### Examples

[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rory/django-template-i18n-lint/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

```bash
# Lint all files in the current directory, excluding `exclude1.html` and `exclude2.html`
python django_i18n_lint.py -e exclude1.html -e exclude2.html

# Lint `file.html`, replacing strings in the file and overwriting the original file
python django_i18n_lint.py -r -o file.html

# Lint `file.html`, replacing strings in the file without asking for confirmation
python django_i18n_lint.py -r -f file.html
```

Code is copyright Rory McCann 2013, and dual licenced under the GNU GPL version3 (or at your option a later version), and the BSD licence. See the files LICENCE.GPLv3 and LICENCE.BSD for more information
11 changes: 7 additions & 4 deletions django_template_i18n_lint.py → django_i18n_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ def location(str, pos):
# django comment
( {%\ comment\ %}.*?{%\ endcomment\ %}
# already translated text
# already translated text (until Django 3.0)
|{%\ ?blocktrans.*?{%\ ?endblocktrans\ ?%}
# already translated text (Django 3.1+)
|{%\ ?blocktranslate.*?{%\ ?endblocktranslate\ ?%}
# any django template function (catches {% trans ..) aswell
|{%.*?%}
Expand Down Expand Up @@ -112,7 +115,7 @@ def location(str, pos):
# Stops us matching non-letter parts, e.g. just hypens, full stops etc.
LETTERS = re.compile(r"[^\W\d_]")

LEADING_TRAILING_WHITESPACE = re.compile("(^\W+|\W+$)")
LEADING_TRAILING_WHITESPACE = re.compile(r"(^\W+|\W+$)")

def split_into_good_and_bad(template):
for index, match in enumerate(GOOD_STRINGS.split(template)):
Expand Down Expand Up @@ -169,12 +172,12 @@ def replace_strings(filename, overwrite=False, force=False, accept=[]):
elif lineno in ignore_lines:
full_text_lines.append(message)
elif force:
full_text_lines.append('{% trans "'+message.replace('"', '\\"')+'" %}')
full_text_lines.append('{% translate "'+message.replace('"', '\\"')+'" %}')

else:
change = raw_input("Make %r translatable? [Y/n] " % message)
if change == 'y' or change == "":
full_text_lines.append('{% trans "'+message.replace('"', '\\"')+'" %}')
full_text_lines.append('{% translate "'+message.replace('"', '\\"')+'" %}')
else:
full_text_lines.append(message)

Expand Down
14 changes: 5 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
from setuptools import setup

setup(
name="django-template-i18n-lint",
name="django-i18n-lint",
version="1.2.0",
author="Rory McCann",
author_email="[email protected]",
py_modules=['django_template_i18n_lint'],
py_modules=['django_i18n_lint'],
license='GPLv3+',
url='http://www.technomancy.org/python/django-template-i18n-lint/',
description='Lint tool to find non-trans/blocktrans text in django templates',
test_suite='tests',
description='Lint tool to find untranslated text in django templates',
classifiers=[
'Development Status :: 5 - Production/Stable',
'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)',
Expand All @@ -21,14 +20,11 @@
'Intended Audience :: Developers',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.10',
],
entry_points={
'console_scripts': [
'django-template-i18n-lint = django_template_i18n_lint:main',
'django-i18n-lint = django_i18n_lint:main',
]
},
)
7 changes: 5 additions & 2 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
Unittests for django-template-i18n-lint.
"""
import unittest
import django_template_i18n_lint
import django_i18n_lint


def _known_good_output(input_string, expected_output):
def test(self):
actual_output = list(django_template_i18n_lint.non_translated_text(input_string))
actual_output = list(django_i18n_lint.non_translated_text(input_string))
self.assertEqual(actual_output, expected_output)
test.__doc__ = "Input string {0} should give output of {1}".format(
repr(input_string[:30]), repr(expected_output)[:30])
Expand All @@ -28,6 +28,9 @@ class DjangoTemplateI18nLintTestCase(unittest.TestCase):
testBlocktransOK1 = _known_good_output("<b>{% blocktrans %}Foo{% endblocktrans %}</b>", [])
testBlocktransOK2 = _known_good_output("<b>{% blocktrans with var=bar %}Foo{% endblocktrans %}</b>", [])
testBlocktransOK3 = _known_good_output("<b>{% blocktrans with var as bar %}Foo{% endblocktrans %}</b>", [])
testBlocktranslateOK1 = _known_good_output("<b>{% blocktranslate %}Foo{% endblocktranslate %}</b>", [])
testBlocktranslateOK2 = _known_good_output("<b>{% blocktranslate with var=bar %}Foo{% endblocktranslate %}</b>", [])
testBlocktranslateOK3 = _known_good_output("<b>{% blocktranslate with var as bar %}Foo{% endblocktranslate %}</b>", [])
testDjangoCustomTag = _known_good_output("{% load foo %}", [])
testJS = _known_good_output("Foo<script>alert('Foo');</script>Bar", [(1, 1, 'Foo'), (1, 34, 'Bar')])
testDjangoVar = _known_good_output("Foo{{ bar }}Baz", [(1, 1, 'Foo'), (1, 13, 'Baz')])
Expand Down
7 changes: 4 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
# and then run "tox" from this directory.

[tox]
envlist = py27, py33
envlist = py310

[testenv]
commands = {envpython} setup.py test

deps = pytest
commands =
{envpython} -m pytest tests.py -v -s
14 changes: 0 additions & 14 deletions vim-helpers.vim

This file was deleted.

0 comments on commit 5b75a14

Please sign in to comment.