From bf228918adc3bbe82b9a004ef9ac37f722fc0e54 Mon Sep 17 00:00:00 2001 From: Julian Dehm Date: Thu, 29 Feb 2024 16:51:36 +0100 Subject: [PATCH] add option to use the django user language --- README.rst | 23 +++++++++++++++++++++++ django_ckeditor_5/widgets.py | 22 +++++++++++++++++----- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index ad37edb..a496b97 100644 --- a/README.rst +++ b/README.rst @@ -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": }`` + +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 diff --git a/django_ckeditor_5/widgets.py b/django_ckeditor_5/widgets.py index 5f70c0e..2145424 100644 --- a/django_ckeditor_5/widgets.py +++ b/django_ckeditor_5/widgets.py @@ -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 _ @@ -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()