From 7ee4d13dce3c720b836a673ec534c75ffc532cb9 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Tue, 8 Jan 2019 18:21:31 -0800 Subject: [PATCH] Show Gujarati in the list of UI languages - Explicitly setup the list of Django languages. Keep them sorted by language code. - Fallback to language name when Django can't provide locale language name. Signed-off-by: Sunil Mohan Adapa --- plinth/forms.py | 13 ++++++++++--- plinth/web_framework.py | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/plinth/forms.py b/plinth/forms.py index 56d499b60..a315b331f 100644 --- a/plinth/forms.py +++ b/plinth/forms.py @@ -69,14 +69,21 @@ class LanguageSelectionFormMixin: supported_languages = [ (None, _('Use the language preference set in the browser')) ] + + def _get_local_name(language_code, language_name): + try: + return get_language_info(language_code)['name_local'] + except KeyError: + return language_name + 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)): - supported_languages.append( - (language_code, - get_language_info(language_code)['name_local'])) + supported_languages.append((language_code, + _get_local_name( + language_code, language_name))) self.fields['language'].choices = supported_languages diff --git a/plinth/web_framework.py b/plinth/web_framework.py index 3bcc0c465..cb203e1c9 100644 --- a/plinth/web_framework.py +++ b/plinth/web_framework.py @@ -25,6 +25,7 @@ import stat import django.conf import django.core.management import django.core.wsgi +from django.conf import global_settings from django.contrib.messages import constants as message_constants from . import cfg, log, module_loader @@ -114,6 +115,7 @@ def init(): FORCE_SCRIPT_NAME=cfg.server_dir, INSTALLED_APPS=applications, IPWARE_META_PRECEDENCE_ORDER=('HTTP_X_FORWARDED_FOR', ), + LANGUAGES=get_languages(), LOGGING=log.get_configuration(), LOGIN_URL='users:login', LOGIN_REDIRECT_URL='index', @@ -161,6 +163,21 @@ def init(): os.chmod(cfg.store_file, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP) +def get_languages(): + """Return list of languages to show in the interface. + + Add additional languages that FreedomBox support but Django doesn't. + + """ + def gettext_noop(string): + """Django's actual translation methods need Django to be setup.""" + return string + + return sorted(list(global_settings.LANGUAGES) + [ + ('gu', gettext_noop('Gujarati')), + ]) + + def get_wsgi_application(): """Return Django wsgi application.""" return django.core.wsgi.get_wsgi_application()