assert mbox_message.get_payload(decode=True) == """HAYYY GUYS!
I hope you like unit tests JUST AS MUCH AS I DO!"""
+
+
+def test_locale_to_lower_upper():
+ """
+ Test cc.i18n.util.locale_to_lower_upper()
+ """
+ assert util.locale_to_lower_upper('en') == 'en'
+ assert util.locale_to_lower_upper('en_US') == 'en_US'
+ assert util.locale_to_lower_upper('en-us') == 'en_US'
+
+ # crazy renditions. Useful?
+ assert util.locale_to_lower_upper('en-US') == 'en_US'
+ assert util.locale_to_lower_upper('en_us') == 'en_US'
+
+
+def test_locale_to_lower_lower():
+ """
+ Test cc.i18n.util.locale_to_lower_lower()
+ """
+ assert util.locale_to_lower_lower('en') == 'en'
+ assert util.locale_to_lower_lower('en_US') == 'en-us'
+ assert util.locale_to_lower_lower('en-us') == 'en-us'
+
+ # crazy renditions. Useful?
+ assert util.locale_to_lower_lower('en-US') == 'en-us'
+ assert util.locale_to_lower_lower('en_us') == 'en-us'
else:
return mhost.sendmail(from_addr, to_addrs, message.as_string())
+
+###################
+# Translation tools
+###################
+
+
+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 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('target_lang'):
+ target_lang = request.matchdict['target_lang']
+ 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 make_locale_lower_upper_style(target_lang)