From c242eb17c93a4fae8803b847c101a3d7b1315cfb Mon Sep 17 00:00:00 2001 From: meeb Date: Sat, 25 May 2024 19:20:28 +1000 Subject: [PATCH] switch to using settings.DISTILL_LANGUAGES for distilling i18n URLs, related to #80 --- README.md | 24 +++++++++++++++++++----- django_distill/renderer.py | 16 +++++++++++----- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 7d7dfd3..b9d65e5 100644 --- a/README.md +++ b/README.md @@ -248,7 +248,7 @@ use `distill_path` or `distill_re_path` if you're building a new site now. Internationalization is only supported for URLs, page content is unable to be dynamically translated. By default your site will be generated using the `LANGUAGE_CODE` value in your `settings.py`. If you also set `settings.USE_I18N` to -`True` then set other languages in your `settings.LANGUAGES` value and register +`True` then set other language codes in your `settings.DISTILL_LANGUAGES` value and register URLs with `i18n_patterns(...)` then your site will be generated in multiple languges. This assumes your multi-language site works as expected before adding `django-distill`. @@ -260,10 +260,10 @@ If you have something like this in your `settings.py` instead: ```python USE_I18N = True -LANGUAGES = [ - ('en', 'English'), - ('fr', 'French'), - ('de', 'German'), +DISTILL_LANGUAGES = [ + 'en', + 'fr', + 'de', ] ``` @@ -445,6 +445,20 @@ copying directories containing files from apps you're not using that get bundled to `['some_dir']` the static files directory `static/some_dir` would be skipped. +**DISTILL_LANGUAGES**: list, defaults to `[]` + +```python +DISTILL_LANGUAGES = [ + 'en', + 'fr', + 'de', +] +``` + +Set `DISTILL_LANGUAGES` to a list of language codes to attempt to render URLs with. +See the "Internationalization" section for more details. + + # Developing locally with HTTPS If you are using a local development environment which has HTTPS support you may need diff --git a/django_distill/renderer.py b/django_distill/renderer.py index 729f542..0b3cade 100644 --- a/django_distill/renderer.py +++ b/django_distill/renderer.py @@ -225,11 +225,17 @@ def render(self, view_name=None, status_codes=None, view_args=None, view_kwargs= def get_langs(self): langs = [] - if settings.LANGUAGES: - for code, name in settings.LANGUAGES: - langs.append(code) - else: - langs.append(settings.LANGUAGE_CODE) + default_lang = str(getattr(settings, 'LANGUAGE_CODE', 'en')) + try: + DISTILL_LANGUAGES = list(getattr(settings, 'DISTILL_LANGUAGES', [])) + except (ValueError, TypeError): + DISTILL_LANGUAGES = [] + if default_lang not in DISTILL_LANGUAGES: + langs.append(default_lang) + for lang in settings.LANGUAGES: + if len(lang) != 2: + raise DistillError('Invalid settings.LANGUAGES value') + langs.append(lang[0]) return langs def _get_filename(self, file_name, uri, param_set):