From 43835e50fa627c1ed485b96ce0404d18b3ffb1db Mon Sep 17 00:00:00 2001 From: Adolfo Fitoria Date: Tue, 3 Apr 2012 11:55:08 -0600 Subject: added language selection into livesettings, needs more testing --- askbot/conf/skin_general_settings.py | 36 +++++++++++++++++++++++++++-- askbot/middleware/locale.py | 26 +++++++++++++++++++++ askbot/setup_templates/settings.py | 1 + askbot/setup_templates/settings.py.mustache | 1 + askbot/skins/utils.py | 14 +++++------ 5 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 askbot/middleware/locale.py diff --git a/askbot/conf/skin_general_settings.py b/askbot/conf/skin_general_settings.py index ccecdaba..2d476b62 100644 --- a/askbot/conf/skin_general_settings.py +++ b/askbot/conf/skin_general_settings.py @@ -30,6 +30,38 @@ settings.register( ) ) +LANGUAGE_CHOICES = ( + ('en', _("English")), + ('es', _("Spanish")), + ('ru', _("Russian")), + ('ca', _("Catalan")), + ('de', _("German")), + ('pt', _("Portuguese")), + ('pt_BR', _("Brazilian Portuguese")), + ('fi', _("Finnish")), + ('fr', _("French")), + ('hi', _("Hindi")), + ('hu', _("Hungarian")), + ('it', _("Italian")), + ('ja', _("Japanese")), + ('ko', _("Korean")), + ('sr', _("Serbian")), + ('tr', _("Turkish")), + ('vi', _("Vietnamese")), + ('zh_CN', _("Chinese")), + ('zh_TW', _("Chinese (Taiwan)")), + ) + +settings.register( + values.StringValue( + GENERAL_SKIN_SETTINGS, + 'ASKBOT_LANGUAGE', + default = 'en', + choices = LANGUAGE_CHOICES, + description = _('Select Language'), + ) +) + settings.register( values.BooleanValue( GENERAL_SKIN_SETTINGS, @@ -198,7 +230,7 @@ settings.register( description = _('Apply custom style sheet (CSS)'), help_text = _( 'Check if you want to change appearance ' - 'of your form by adding custom style sheet rules ' + 'of your form by adding custom style sheet rules ' '(please see the next item)' ), default = False @@ -214,7 +246,7 @@ settings.register( 'To use this function, check ' '"Apply custom style sheet" option above. ' 'The CSS rules added in this window will be applied ' - 'after the default style sheet rules. ' + 'after the default style sheet rules. ' 'The custom style sheet will be served dynamically at ' 'url "<forum url>/custom.css", where ' 'the "<forum url> part depends (default is ' diff --git a/askbot/middleware/locale.py b/askbot/middleware/locale.py new file mode 100644 index 00000000..c92e977a --- /dev/null +++ b/askbot/middleware/locale.py @@ -0,0 +1,26 @@ +"Taken from django.middleware.locale: this is the locale selecting middleware that will look at accept headers" + +from django.utils.cache import patch_vary_headers +from django.utils import translation +from askbot.conf import settings + +class LocaleMiddleware(object): + """ + This is a very simple middleware that parses a request + and decides what translation object to install in the current + thread context. This allows pages to be dynamically + translated to the language the user desires (if the language + is available, of course). + """ + + def process_request(self, request): + language = settings.ASKBOT_LANGUAGE + translation.activate(language) + request.LANGUAGE_CODE = translation.get_language() + + def process_response(self, request, response): + patch_vary_headers(response, ('Accept-Language',)) + if 'Content-Language' not in response: + response['Content-Language'] = translation.get_language() + translation.deactivate() + return response diff --git a/askbot/setup_templates/settings.py b/askbot/setup_templates/settings.py index b1d7dd06..0aabcbe0 100644 --- a/askbot/setup_templates/settings.py +++ b/askbot/setup_templates/settings.py @@ -98,6 +98,7 @@ TEMPLATE_LOADERS = ( MIDDLEWARE_CLASSES = ( #'django.middleware.gzip.GZipMiddleware', + 'askbot.middleware.locale.LocaleMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', #'django.middleware.cache.UpdateCacheMiddleware', 'django.middleware.common.CommonMiddleware', diff --git a/askbot/setup_templates/settings.py.mustache b/askbot/setup_templates/settings.py.mustache index 855e6294..99023524 100644 --- a/askbot/setup_templates/settings.py.mustache +++ b/askbot/setup_templates/settings.py.mustache @@ -97,6 +97,7 @@ TEMPLATE_LOADERS = ( MIDDLEWARE_CLASSES = ( #'django.middleware.gzip.GZipMiddleware', + 'askbot.middleware.locale.LocaleMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', #'django.middleware.cache.UpdateCacheMiddleware', 'django.middleware.common.CommonMiddleware', diff --git a/askbot/skins/utils.py b/askbot/skins/utils.py index a07b1fa9..dee14e56 100644 --- a/askbot/skins/utils.py +++ b/askbot/skins/utils.py @@ -3,7 +3,7 @@ the lookup resolution process for templates and media works as follows: * look up item in selected skin * if not found look in 'default' -* raise an exception +* raise an exception """ import os import logging @@ -56,7 +56,7 @@ def get_available_skins(selected=None): #re-insert default as a last item skins['default'] = default_dir - skins['common'] = common_dir + skins['common'] = common_dir return skins @@ -71,7 +71,7 @@ def get_path_to_skin(skin): return skin_dirs.get(skin, None) def get_skin_choices(): - """returns a tuple for use as a set of + """returns a tuple for use as a set of choices in the form""" skin_names = list(reversed(get_available_skins().keys())) return zip(skin_names, skin_names) @@ -86,7 +86,7 @@ def resolve_skin_for_media(media=None, preferred_skin = None): def get_media_url(url, ignore_missing = False): """returns url prefixed with the skin name - of the first skin that contains the file + of the first skin that contains the file directories are searched in this order: askbot_settings.ASKBOT_DEFAULT_SKIN, then 'default', then 'commmon' if file is not found - returns None @@ -156,7 +156,7 @@ def get_media_url(url, ignore_missing = False): url = django_settings.STATIC_URL + use_skin + '/media/' + url url = os.path.normpath(url).replace('\\', '/') - + if resource_revision: url += '?v=%d' % resource_revision @@ -174,7 +174,7 @@ def update_media_revision(skin = None): if skin in get_skin_choices(): skin_path = get_path_to_skin(skin) else: - raise MediaNotFound('Skin %s not found' % skin) + raise MediaNotFound('Skin %s not found' % skin) else: skin = 'default' skin_path = get_path_to_skin(askbot_settings.ASKBOT_DEFAULT_SKIN) @@ -193,6 +193,6 @@ def update_media_revision(skin = None): if current_hash != askbot_settings.MEDIA_RESOURCE_REVISION_HASH: askbot_settings.update('MEDIA_RESOURCE_REVISION', resource_revision + 1) - askbot_settings.update('MEDIA_RESOURCE_REVISION_HASH', current_hash) + askbot_settings.update('MEDIA_RESOURCE_REVISION_HASH', current_hash) logging.debug('MEDIA_RESOURCE_REVISION changed') askbot_settings.MEDIA_RESOURCE_REVISION -- cgit v1.2.3-1-g7c22