0001-misc-add-filter-to-detect-language-43335.patch
tests/test_templates.py | ||
---|---|---|
4 | 4 |
import pytest |
5 | 5 |
import string |
6 | 6 | |
7 |
try: |
|
8 |
import langdetect |
|
9 |
except ImportError: |
|
10 |
langdetect = None |
|
11 | ||
7 | 12 |
from django.test import override_settings |
8 | 13 |
from quixote import cleanup |
9 | 14 |
from wcs.qommon.substitution import CompatibilityNamesDict |
... | ... | |
606 | 611 |
assert t.render({'number': None}) == 'None' |
607 | 612 |
t = Template('{{ number|decimal|phonenumber_fr }}') |
608 | 613 |
assert t.render({'number': '1,33'}) == '1.33' |
614 | ||
615 | ||
616 |
@pytest.mark.skipif('langdetect is None') |
|
617 |
def test_language_detect(): |
|
618 |
t = Template('{{ plop|language_detect }}') |
|
619 |
assert t.render({'plop': 'Good morning world'}) == 'en' |
|
620 |
assert t.render({'plop': 'Bonjour tout le monde'}) == 'fr' |
|
621 |
assert t.render({'plop': '2132133'}) == '' |
tox.ini | ||
---|---|---|
32 | 32 |
Pillow |
33 | 33 |
python-magic |
34 | 34 |
docutils |
35 |
langdetect |
|
35 | 36 |
git+https://git.entrouvert.org/debian/django-ckeditor.git |
36 | 37 |
django111: django>=1.11,<1.12 |
37 | 38 |
django22: django>=2.2,<2.3 |
wcs/qommon/templatetags/qommon.py | ||
---|---|---|
26 | 26 |
import pyproj |
27 | 27 |
from pyproj import Geod |
28 | 28 | |
29 |
try: |
|
30 |
import langdetect |
|
31 |
from langdetect.lang_detect_exception import LangDetectException |
|
32 |
except ImportError: |
|
33 |
langdetect = None |
|
34 | ||
29 | 35 |
from django import template |
30 | 36 |
from django.template import defaultfilters |
31 | 37 |
from django.utils import dateparse |
... | ... | |
455 | 461 |
return False |
456 | 462 | |
457 | 463 | |
464 |
@register.filter |
|
465 |
def language_detect(value): |
|
466 |
if langdetect is None: |
|
467 |
return '' |
|
468 |
try: |
|
469 |
return langdetect.detect(str(value)) |
|
470 |
except LangDetectException: |
|
471 |
return '' |
|
472 | ||
473 | ||
458 | 474 |
@register.filter(is_safe=False) |
459 | 475 |
def phonenumber_fr(value, separator=' '): |
460 | 476 |
DROMS = ('262', '508', '590', '594', '596') |
461 |
- |