From 8b28bee4c1de6f1190091f5c17c0cf0f948c7071 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Thu, 12 May 2011 14:57:58 -0500 Subject: [PATCH] Added some locale determination tools --- mediagoblin/tests/test_util.py | 26 ++++++++++++++++ mediagoblin/util.py | 56 ++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/mediagoblin/tests/test_util.py b/mediagoblin/tests/test_util.py index 5bc31fd6..ff40a677 100644 --- a/mediagoblin/tests/test_util.py +++ b/mediagoblin/tests/test_util.py @@ -69,3 +69,29 @@ I hope you like unit tests JUST AS MUCH AS I DO!""" 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' diff --git a/mediagoblin/util.py b/mediagoblin/util.py index 63f0f9c5..2b7948c8 100644 --- a/mediagoblin/util.py +++ b/mediagoblin/util.py @@ -183,3 +183,59 @@ def send_email(from_addr, to_addrs, subject, message_body): 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) -- 2.25.1