Skip to content

Commit

Permalink
Add i18n support
Browse files Browse the repository at this point in the history
  • Loading branch information
codingjoe committed Jul 16, 2017
1 parent b88e26b commit b15c679
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 2 deletions.
2 changes: 1 addition & 1 deletion django_select2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
"""

__version__ = "5.10.0"
__version__ = "5.11.0"
82 changes: 82 additions & 0 deletions django_select2/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,88 @@ class Select2Conf(AppConf):
develop without an Internet connection.
"""

I18N_PATH = '//cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/i18n'
"""
The base URI for the Select2 i18n files. By default this points to the Cloudflare CDN.
If you want to select the version of the JS library used, or want to serve it from
the local 'static' resources, add a line to your settings.py like so::
SELECT2_JS = 'assets/js/i18n'
.. tip:: Change this setting to a local asset in your development environment to
develop without an Internet connection.
"""

I18N_AVAILABLE_LANGUAGES = [
'ar',
'az',
'bg',
'ca',
'cs',
'da',
'de',
'el',
'en',
'es',
'et',
'eu',
'fa',
'fi',
'fr',
'gl',
'he',
'hi',
'hr',
'hu',
'id',
'is',
'it',
'ja',
'km',
'ko',
'lt',
'lv',
'mk',
'ms',
'nb',
'nl',
'pl',
'pt-BR',
'pt',
'ro',
'ru',
'sk',
'sr-Cyrl',
'sr',
'sv',
'th',
'tr',
'uk',
'vi',
'zh-CN',
'zh-TW',
]
"""
List of available translations.
List of ISO 639-1 language codes that are supported by Select2.
If currently set language code (e.g. using the HTTP ``Accept-Language`` header)
is in this list, Django-Select2 will use the language code to create load
the proper translation.
The full path for the language file consists of::
from django.utils import translations
full_path = "{i18n_path}/{language_code}.js".format(
i18n_path=settings.DJANGO_SELECT2_I18N,
language_code=translations.get_language(),
)
``settings.DJANGO_SELECT2_I18N`` refers to :attr:`.I18N_PATH`.
"""

class Meta:
"""Prefix for all Django-Select2 settings."""

Expand Down
5 changes: 4 additions & 1 deletion django_select2/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
from django.forms.models import ModelChoiceIterator
from django.utils.encoding import force_text
from django.utils.six.moves.cPickle import PicklingError as cPicklingError
from django.utils.translation import get_language

from .cache import cache
from .conf import settings
Expand Down Expand Up @@ -112,8 +113,10 @@ def _get_media(self):
.. Note:: For more information visit
https://docs.djangoproject.com/en/1.8/topics/forms/media/#media-as-a-dynamic-property
"""
i18n_file = '%s/%s.js' % (settings.SELECT2_I18N_PATH, get_language())
i18n_file = (i18n_file,) if get_language() in settings.SELECT2_I18N_AVAILABLE_LANGUAGES else ()
return forms.Media(
js=(settings.SELECT2_JS, 'django_select2/django_select2.js'),
js=(settings.SELECT2_JS,) + i18n_file + ('django_select2/django_select2.js',),
css={'screen': (settings.SELECT2_CSS,)}
)

Expand Down
22 changes: 22 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest
from django.core import signing
from django.db.models import QuerySet
from django.utils import translation
from django.utils.encoding import force_text
from django.utils.six import text_type
from selenium.common.exceptions import NoSuchElementException
Expand Down Expand Up @@ -103,6 +104,27 @@ def test_empty_option(self, db):
assert multiple_select.widget.allow_multiple_selected
assert '<option value=""></option>' not in multiple_select.widget.render('featured_artists', None)

def test_i18n(self):
translation.activate('de')
assert Select2Widget().media._js == [
'//cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js',
'//cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/i18n/de.js',
'django_select2/django_select2.js'
]

translation.activate('en')
assert Select2Widget().media._js == [
'//cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js',
'//cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/i18n/en.js',
'django_select2/django_select2.js'
]

translation.activate('00')
assert Select2Widget().media._js == [
'//cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js',
'django_select2/django_select2.js'
]


class TestSelect2MixinSettings(object):
def test_default_media(self):
Expand Down
6 changes: 6 additions & 0 deletions tests/testapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
SITE_ID = 1
ROOT_URLCONF = 'tests.testapp.urls'

LANGUAGES = [
('de', 'German'),
('en', 'English'),
]

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
Expand All @@ -44,6 +49,7 @@
SECRET_KEY = '123456'

USE_L10N = True
USE_I18N = True

if os.environ.get('TRAVIS'):
CACHES = {
Expand Down

0 comments on commit b15c679

Please sign in to comment.