From d72abb85390b2bd3f48df6ed7b5194ee53594c7f Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Fri, 26 Feb 2016 00:35:40 +0530 Subject: [PATCH] config: Don't restrict supported languages - There is no reason to limit the number of languages supported by the application. This is what happens when LANGUAGES is set during Django configuration. Users should be able to set a language using browser preferences and see at least the Django translated messages even if Plinth messages are not translated (example user create/edit form). - If we wish to limit the number of language options shown duing language selection (for quality perception reasons), we should perform the limiting at the time of configuration. - Note that when a new language is requested in translation tool, we create an empty file and that may lead to language being shown but with few strings actually translated. This will have poor impact on user experience. I wonder if we should filter language at all, keeping this in mind. --- plinth/__main__.py | 14 +------------- plinth/modules/config/config.py | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/plinth/__main__.py b/plinth/__main__.py index 0e8db1b73..d23fa1ed1 100644 --- a/plinth/__main__.py +++ b/plinth/__main__.py @@ -21,6 +21,7 @@ import django.conf from django.contrib.messages import constants as message_constants import django.core.management import django.core.wsgi +from django.utils import translation import importlib import logging import os @@ -224,18 +225,6 @@ def configure_django(): if cfg.secure_proxy_ssl_header: secure_proxy_ssl_header = (cfg.secure_proxy_ssl_header, 'https') - # Read translated languages from the 'locale' directory - languages = [('en', 'English')] - locale_dir = os.path.join(os.path.dirname(__file__), 'locale') - if os.path.isdir(locale_dir): - translated_language_codes = next(os.walk(locale_dir))[1] - all_languages = dict(django.conf.global_settings.LANGUAGES) - for code in translated_language_codes: - lang_code = code.replace('_', '-') - if lang_code in all_languages: - languages.append((code, all_languages[lang_code])) - languages = sorted(languages, key=lambda tup: tup[1]) - django.conf.settings.configure( ALLOWED_HOSTS=['*'], CACHES={'default': @@ -245,7 +234,6 @@ def configure_django(): 'NAME': cfg.store_file}}, DEBUG=cfg.debug, INSTALLED_APPS=applications, - LANGUAGES=languages, LOGGING=logging_configuration, LOGIN_URL='users:login', LOGIN_REDIRECT_URL='apps:index', diff --git a/plinth/modules/config/config.py b/plinth/modules/config/config.py index 54c687c2b..4957589bd 100644 --- a/plinth/modules/config/config.py +++ b/plinth/modules/config/config.py @@ -28,9 +28,11 @@ from django.template.response import TemplateResponse from django.utils import translation from django.utils.translation import ugettext as _, ugettext_lazy import logging +import os import re import socket +import plinth from plinth import actions from plinth import cfg from plinth.modules import firewall @@ -77,6 +79,7 @@ class TrimmedCharField(forms.CharField): return super(TrimmedCharField, self).clean(value) + def domain_label_validator(domainname): """Validate domain name labels.""" for label in domainname.split('.'): @@ -123,10 +126,22 @@ class ConfigurationForm(forms.Form): language = forms.ChoiceField( label=ugettext_lazy('Language'), - help_text=\ - ugettext_lazy('Language for this web administration interface'), - required=False, - choices=settings.LANGUAGES) + help_text=ugettext_lazy( + 'Language for this web administration interface'), + required=False) + + def __init__(self, *args, **kwargs): + """Set limited language choices.""" + super().__init__(*args, **kwargs) + languages = [] + for language_code, language_name in settings.LANGUAGES: + locale_code = translation.to_locale(language_code) + plinth_dir = os.path.dirname(plinth.__file__) + if language_code == 'en' or os.path.exists( + os.path.join(plinth_dir, 'locale', locale_code)): + languages.append((language_code, language_name)) + + self.fields['language'].choices = languages def init():