Skip to content

Commit

Permalink
Give priority to config language over Django language
Browse files Browse the repository at this point in the history
Thanks @andre-silva-14 for the initial patch.
  • Loading branch information
claudep committed Sep 19, 2020
1 parent b8d1b76 commit cc1d745
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
3 changes: 3 additions & 0 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ file.
The default TinyMCE configuration to use. See `the TinyMCE manual`_ for all
options. To set the configuration for a specific TinyMCE editor, see the
``mce_attrs`` parameter for the :ref:`widget <widget>`.
!Important: The ``language`` attribute should only be set to force TinyMCE to
have a different language than Django's current active language.

``TINYMCE_SPELLCHECKER`` (default: ``False``)
Whether to use the spell checker through the supplied view. You must add
Expand Down Expand Up @@ -167,6 +169,7 @@ Example::
"fullscreen preview save print | insertfile image media pageembed template link anchor codesample | "
"a11ycheck ltr rtl | showcomments addcomment code",
"custom_undo_redo_levels": 10,
"language": "es_ES", # To force a specific language instead of the Django current language.
}
TINYMCE_SPELLCHECKER = True
TINYMCE_COMPRESSOR = True
Expand Down
36 changes: 27 additions & 9 deletions tinymce/tests/test_widgets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from contextlib import contextmanager
from unittest.mock import patch

from django import forms
from django.test import TestCase
Expand All @@ -23,47 +24,64 @@ def override_tinymce_settings(settings_dict):
@override_settings(LANGUAGES=[("en", "English")])
class TestWidgets(TestCase):
def test_default_config(self):
config = get_language_config()
config = get_language_config("en")
config_ok = {
"spellchecker_languages": "+English=en",
"directionality": "ltr",
"language": "en_US",
"spellchecker_rpc_url": "/tinymce/spellchecker/",
}
self.assertEqual(config, config_ok)

def test_no_active_language(self):
widget = TinyMCE()
with override(None):
# Even when no language is activated
config = get_language_config()
self.assertEqual(config, config_ok)
config = widget.get_mce_config(attrs={"id": "id"})
self.assertEqual(config["language"], "en_US")
self.assertEqual(config["spellchecker_languages"], "+English=en")

@override_settings(LANGUAGES_BIDI=["en"])
def test_default_config_rtl(self):
config = get_language_config()
config = get_language_config("en")
config_ok = {
"spellchecker_languages": "+English=en",
"directionality": "rtl",
"language": "en_US",
"spellchecker_rpc_url": "/tinymce/spellchecker/",
}
self.assertEqual(config, config_ok)

def test_config_from_language_code(self):
langs = [
("en", "en"),
("fr", "fr"),
("pt-br", "pt_BR"),
("sr-latn", "sr_Latn"),
]
widget = TinyMCE()
for lang_code, lang_expected in langs:
with override_settings(LANGUAGE_CODE=lang_code):
config = get_language_config()
config = widget.get_mce_config(attrs={"id": "id"})
self.assertEqual(config["language"], lang_expected)

def test_language_override_from_config(self):
"""language in DEFAULT_CONFIG has priority over current Django language."""
widget = TinyMCE()
orig_config = tinymce.settings.DEFAULT_CONFIG
with patch.dict(tinymce.settings.DEFAULT_CONFIG, {**orig_config, "language": "es_ES"}):
config = widget.get_mce_config(attrs={"id": "id"})
self.assertEqual(config["language"], "es_ES")

def test_mce_attrs_language_priority(self):
widget = TinyMCE(mce_attrs={"language": "ru"})
orig_config = tinymce.settings.DEFAULT_CONFIG
with patch.dict(tinymce.settings.DEFAULT_CONFIG, {**orig_config, "language": "es_ES"}):
config = widget.get_mce_config(attrs={"id": "id"})
self.assertEqual(config["language"], "ru")

def test_content_language(self):
config = get_language_config("ru-ru")
config_ok = {
"spellchecker_languages": "English=en",
"directionality": "ltr",
"language": "en_US",
"spellchecker_rpc_url": "/tinymce/spellchecker/",
}
self.assertEqual(config, config_ok)
Expand Down
21 changes: 10 additions & 11 deletions tinymce/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,12 @@ class TinyMCE(forms.Textarea):
"""

def __init__(self, content_language=None, attrs=None, mce_attrs=None):
super(TinyMCE, self).__init__(attrs)
super().__init__(attrs)
mce_attrs = mce_attrs or {}
self.mce_attrs = mce_attrs
if "mode" not in self.mce_attrs:
self.mce_attrs["mode"] = "exact"
self.mce_attrs["strict_loading_mode"] = 1
if content_language is None:
content_language = mce_attrs.get("language", None)
self.content_language = content_language

def use_required_attribute(self, *args):
Expand All @@ -56,7 +54,9 @@ def use_required_attribute(self, *args):

def get_mce_config(self, attrs):
mce_config = tinymce.settings.DEFAULT_CONFIG.copy()
mce_config.update(get_language_config(self.content_language))
if "language" not in mce_config:
mce_config["language"] = get_language_from_django()
mce_config.update(get_language_config(self.content_language or mce_config["language"]))
if tinymce.settings.USE_FILEBROWSER:
mce_config["file_browser_callback"] = "djangoFileBrowser"
mce_config.update(self.mce_attrs)
Expand Down Expand Up @@ -116,17 +116,16 @@ class AdminTinyMCE(TinyMCE, admin_widgets.AdminTextareaWidget):
pass


def get_language_config(content_language=None):
def get_language_from_django():
language = get_language()
language = to_locale(language) if language is not None else "en_US"
if content_language:
content_language = content_language[:2]
else:
content_language = language[:2]
return language

config = {}
config["language"] = language

def get_language_config(content_language):
content_language = content_language[:2]

config = {}
lang_names = OrderedDict()
for lang, name in settings.LANGUAGES:
if lang[:2] not in lang_names:
Expand Down

0 comments on commit cc1d745

Please sign in to comment.