diff options
Diffstat (limited to 'mediagoblin/util.py')
-rw-r--r-- | mediagoblin/util.py | 110 |
1 files changed, 102 insertions, 8 deletions
diff --git a/mediagoblin/util.py b/mediagoblin/util.py index 63f0f9c5..1f568ed3 100644 --- a/mediagoblin/util.py +++ b/mediagoblin/util.py @@ -15,6 +15,8 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. from email.MIMEText import MIMEText +import gettext +import pkg_resources import smtplib import sys @@ -33,25 +35,41 @@ def _activate_testing(): TESTS_ENABLED = True -def get_jinja_env(user_template_path=None): +def get_jinja_loader(user_template_path=None): """ - Set up the Jinja environment, possibly allowing for user + Set up the Jinja template loaders, possibly allowing for user overridden templates. (In the future we may have another system for providing theming; for now this is good enough.) """ if user_template_path: - loader = jinja2.ChoiceLoader( + return jinja2.ChoiceLoader( [jinja2.FileSystemLoader(user_template_path), jinja2.PackageLoader('mediagoblin', 'templates')]) else: - loader = jinja2.PackageLoader('mediagoblin', 'templates') + return jinja2.PackageLoader('mediagoblin', 'templates') - return jinja2.Environment( - loader=loader, autoescape=True, + +def get_jinja_env(template_loader, locale): + """ + Set up the Jinja environment, + + (In the future we may have another system for providing theming; + for now this is good enough.) + """ + setup_gettext(locale) + + template_env = jinja2.Environment( + loader=template_loader, autoescape=True, extensions=['jinja2.ext.i18n']) + template_env.install_gettext_callables( + mgoblin_globals.translations.gettext, + mgoblin_globals.translations.ngettext) + + return template_env + def setup_user_in_request(request): """ @@ -172,7 +190,7 @@ def send_email(from_addr, to_addrs, subject, message_body): if TESTS_ENABLED: EMAIL_TEST_INBOX.append(message) - elif mgoblin_globals.email_debug_mode: + if getattr(mgoblin_globals, 'email_debug_mode', False): print u"===== Email =====" print u"From address: %s" % message['From'] print u"To addresses: %s" % message['To'] @@ -180,6 +198,82 @@ def send_email(from_addr, to_addrs, subject, message_body): print u"-- Body: --" print message.get_payload(decode=True) + return mhost.sendmail(from_addr, to_addrs, message.as_string()) + + +################### +# Translation tools +################### + + +TRANSLATIONS_PATH = pkg_resources.resource_filename( + 'mediagoblin', 'translations') + + +def locale_to_lower_upper(locale): + """ + Take a locale, regardless of style, and format it like "en-us" + """ + if '-' in locale: + lang, country = locale.split('-', 1) + return '%s_%s' % (lang.lower(), country.upper()) + elif '_' in locale: + lang, country = locale.split('_', 1) + return '%s_%s' % (lang.lower(), country.upper()) else: - return mhost.sendmail(from_addr, to_addrs, message.as_string()) + return locale.lower() + + +def locale_to_lower_lower(locale): + """ + Take a locale, regardless of style, and format it like "en_US" + """ + if '_' in locale: + lang, country = locale.split('_', 1) + return '%s-%s' % (lang.lower(), country.lower()) + else: + return locale.lower() + + +def get_locale_from_request(request): + """ + Figure out what target language is most appropriate based on the + request + """ + request_form = request.GET or request.POST + + if request_form.has_key('lang'): + return locale_to_lower_upper(request_form['lang']) + + accept_lang_matches = request.accept_language.best_matches() + + # Your routing can explicitly specify a target language + if request.matchdict.has_key('locale'): + target_lang = request.matchdict['locale'] + elif request.session.has_key('target_lang'): + target_lang = request.session['target_lang'] + # Pull the first acceptable language + elif accept_lang_matches: + target_lang = accept_lang_matches[0] + # Fall back to English + else: + target_lang = 'en' + + return locale_to_lower_upper(target_lang) + + +def setup_gettext(locale): + """ + Setup the gettext instance based on this locale + """ + # Later on when we have plugins we may want to enable the + # multi-translations system they have so we can handle plugin + # translations too + + # TODO: fallback nicely on translations from pt_PT to pt if not + # available, etc. + this_gettext = gettext.translation( + 'mediagoblin', TRANSLATIONS_PATH, [locale], fallback=True) + mgoblin_globals.setup_globals( + translations=this_gettext) |