Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to use the django user language #187

Merged
merged 1 commit into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,32 @@ 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
Loading