Added lazy JSON encoder in order to support i18n in CKEditor settings

A LazyEncoder has been implemented based on Djangos DjangoJSONEncoder,
rather than json.JSONEncoder, as described [here](https://docs.djangoproject.com/en/dev/topics/serialization/#serialization-formats-json)

This allows us to use `ugettext_lazy` (and other laze functions) in
`settings.py` which adds the possibility to localize strings for use
in the `CKEDITOR_CONFIGS` settings etc.

**Example**

    from django.utils.translation import ugettext_lazy as _

    CKEDITOR_CONFIGS = {
      'default': {
        'stylesSet': [
          { 'name': _('Lead') , 'element': 'p', 'attributes': { 'class': 'lead' } },
          ...
        ]
        ...
      }
This commit is contained in:
Simon Fransson 2014-10-22 13:49:17 +02:00
parent 0526219288
commit a5f6d2fcad
1 changed files with 11 additions and 2 deletions

View File

@ -8,10 +8,19 @@ from django.utils.encoding import force_text
from django.utils.translation import get_language
from django.core.exceptions import ImproperlyConfigured
from django.forms.util import flatatt
import json
from django.utils.functional import Promise
from django.utils.encoding import force_text
from django.core.serializers.json import DjangoJSONEncoder
class LazyEncoder(DjangoJSONEncoder):
def default(self, obj):
if isinstance(obj, Promise):
return force_text(obj)
return super(LazyEncoder, self).default(obj)
json_encode = json.JSONEncoder().encode
json_encode = LazyEncoder().encode
DEFAULT_CONFIG = {
'skin': 'moono',