misc: add filter to detect language (#43335)
This commit is contained in:
parent
1517222ab2
commit
b8b9244fc8
|
@ -30,6 +30,7 @@ Recommends: libreoffice-writer-nogui | libreoffice-writer,
|
||||||
poppler-utils,
|
poppler-utils,
|
||||||
python3-dns,
|
python3-dns,
|
||||||
python3-docutils,
|
python3-docutils,
|
||||||
|
python3-langdetect,
|
||||||
python3-magic,
|
python3-magic,
|
||||||
python3-qrcode,
|
python3-qrcode,
|
||||||
python3-xlwt
|
python3-xlwt
|
||||||
|
|
|
@ -4,6 +4,11 @@ import datetime
|
||||||
import pytest
|
import pytest
|
||||||
import string
|
import string
|
||||||
|
|
||||||
|
try:
|
||||||
|
import langdetect
|
||||||
|
except ImportError:
|
||||||
|
langdetect = None
|
||||||
|
|
||||||
from django.test import override_settings
|
from django.test import override_settings
|
||||||
from quixote import cleanup
|
from quixote import cleanup
|
||||||
from wcs.qommon.substitution import CompatibilityNamesDict
|
from wcs.qommon.substitution import CompatibilityNamesDict
|
||||||
|
@ -604,3 +609,11 @@ def test_phonenumber_fr():
|
||||||
assert t.render({'number': None}) == 'None'
|
assert t.render({'number': None}) == 'None'
|
||||||
t = Template('{{ number|decimal|phonenumber_fr }}')
|
t = Template('{{ number|decimal|phonenumber_fr }}')
|
||||||
assert t.render({'number': '1,33'}) == '1.33'
|
assert t.render({'number': '1,33'}) == '1.33'
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif('langdetect is None')
|
||||||
|
def test_language_detect():
|
||||||
|
t = Template('{{ plop|language_detect }}')
|
||||||
|
assert t.render({'plop': 'Good morning world'}) == 'en'
|
||||||
|
assert t.render({'plop': 'Bonjour tout le monde'}) == 'fr'
|
||||||
|
assert t.render({'plop': '2132133'}) == ''
|
||||||
|
|
1
tox.ini
1
tox.ini
|
@ -32,6 +32,7 @@ deps =
|
||||||
Pillow
|
Pillow
|
||||||
python-magic
|
python-magic
|
||||||
docutils
|
docutils
|
||||||
|
langdetect
|
||||||
git+https://git.entrouvert.org/debian/django-ckeditor.git
|
git+https://git.entrouvert.org/debian/django-ckeditor.git
|
||||||
django111: django>=1.11,<1.12
|
django111: django>=1.11,<1.12
|
||||||
django22: django>=2.2,<2.3
|
django22: django>=2.2,<2.3
|
||||||
|
|
|
@ -26,6 +26,12 @@ import random
|
||||||
import pyproj
|
import pyproj
|
||||||
from pyproj import Geod
|
from pyproj import Geod
|
||||||
|
|
||||||
|
try:
|
||||||
|
import langdetect
|
||||||
|
from langdetect.lang_detect_exception import LangDetectException
|
||||||
|
except ImportError:
|
||||||
|
langdetect = None
|
||||||
|
|
||||||
from django import template
|
from django import template
|
||||||
from django.template import defaultfilters
|
from django.template import defaultfilters
|
||||||
from django.utils import dateparse
|
from django.utils import dateparse
|
||||||
|
@ -440,6 +446,16 @@ def has_role(user, role_name):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
@register.filter
|
||||||
|
def language_detect(value):
|
||||||
|
if langdetect is None:
|
||||||
|
return ''
|
||||||
|
try:
|
||||||
|
return langdetect.detect(str(value))
|
||||||
|
except LangDetectException:
|
||||||
|
return ''
|
||||||
|
|
||||||
|
|
||||||
@register.filter(is_safe=False)
|
@register.filter(is_safe=False)
|
||||||
def phonenumber_fr(value, separator=' '):
|
def phonenumber_fr(value, separator=' '):
|
||||||
DROMS = ('262', '508', '590', '594', '596')
|
DROMS = ('262', '508', '590', '594', '596')
|
||||||
|
|
Loading…
Reference in New Issue