romanvm / django-tinymce4-lite

TinyMCE 4 editor widget for Django
MIT License
126 stars 47 forks source link

Use django JSON serialization to support lazy gettext #50

Closed merwok closed 5 years ago

merwok commented 5 years ago

Hello! I wanted to define a block_formats option with translated menu labels, so I used gettext_lazy as is usual.

Traceback:

  File lib/python3.7/site-packages/tinymce/widgets.py", line 140, in render_tinymce_init_js
    'tinymce_config': json.dumps(mce_config)[1:-1],
  File "lib/python3.7/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "lib/python3.7/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "lib/python3.7/json/encoder.py", line 258, in iterencode
    return _iterencode(o, 0)
  File "lib/python3.7/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '

Solution: call json.dumps with this encoder class: https://docs.djangoproject.com/en/2.1/topics/serialization/#djangojsonencoder

romanvm commented 5 years ago

Thanks for reporting this. Yes, Django's gettext seems to return Django's own str sublcass not supported by the original JSON encoder.

merwok commented 5 years ago

I think it returns a Proxy or Promise, not a str subclass (which would work with default JSON encoder but cause other kinds of trouble), but the solution is the same!

merwok commented 5 years ago

Will you accept a PR for this?

romanvm commented 5 years ago

Yes, of course.