Skip to content

Commit

Permalink
add option to use the django user language
Browse files Browse the repository at this point in the history
  • Loading branch information
goapunk committed Feb 29, 2024
1 parent ce2a4b3 commit bf22891
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
23 changes: 23 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,29 @@ Custom storage example:
location = os.path.join(settings.MEDIA_ROOT, "django_ckeditor_5")
base_url = urljoin(settings.MEDIA_URL, "django_ckeditor_5/")
Changing the language:
^^^^^^^^^^^^^^^^^^^^^^
You can change the language via the `language` key in the config\:
.. code-block:: python
CKEDITOR_5_CONFIGS = {
'default': {
'toolbar': ['heading', '|', 'bold', 'italic', 'link',
'bulletedList', 'numberedList', 'blockQuote', 'imageUpload', ],
'language': 'de',
},
``language`` can be either\:
1. a string containing a single language
2. a list of languages
3. a dict ``{"ui": <a string (1) or a list of languages (2)>}``
If you want the language to change with the user language in django
you can add ``CKEDITOR_5_USER_LANGUAGE=True`` to your django settings.
Additionally you will have to list all available languages in the ckeditor
config as shown above.
Installing from GitHub:
^^^^^^^^^^^^^^^^^^^^^^^
.. code-block:: bash
Expand Down
22 changes: 17 additions & 5 deletions django_ckeditor_5/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.forms.renderers import get_default_renderer
from django.urls import reverse
from django.utils.safestring import mark_safe
from django.utils.translation import get_language

if get_version() >= "4.0":
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -49,19 +50,30 @@ class Media:
custom_css = getattr(settings, "CKEDITOR_5_CUSTOM_CSS", None)
if custom_css:
css["all"].append(custom_css)
js = ["django_ckeditor_5/dist/bundle.js" ]
js = ["django_ckeditor_5/dist/bundle.js"]
configs = getattr(settings, "CKEDITOR_5_CONFIGS", None)
if configs is not None:
for config in configs:
language = configs[config].get('language')
if language:
if isinstance(language, str) and language != "en":
js += [f"django_ckeditor_5/dist/translations/{language}.js" ]
elif isinstance(language, dict) and language.get('ui') and language["ui"] != "en":
js += [f"django_ckeditor_5/dist/translations/{language['ui']}.js" ]
languages = []
if isinstance(language, dict) and language.get('ui'):
language = language.get('ui')
elif isinstance(language, str):
languages.append(language)
elif isinstance(language, list):
languages = language
for lang in languages:
if lang != "en":
js += [f"django_ckeditor_5/dist/translations/{lang}.js"]

def render(self, name, value, attrs=None, renderer=None):
context = super().get_context(name, value, attrs)
use_language = getattr(settings, "CKEDITOR_5_USER_LANGUAGE", False)
if use_language:
language = get_language().lower()
if language:
self.config["language"] = language

if renderer is None:
renderer = get_default_renderer()
Expand Down

0 comments on commit bf22891

Please sign in to comment.