Home | Trees | Indices | Help |
|
---|
|
1 """General i18n utility functions.""" 2 3 import urllib 4 5 import simplejson 6 import cherrypy 7 8 from turbogears import config 9 from turbogears.release import version as tg_version 10 from turbogears.util import parse_http_accept_header, request_available 1113 version = "TurboGears/%s" % tg_version1416 """Translate text via the translate.google.com service. 17 18 The source language is given by 'from_lang' and the target language as 19 'to_lang'. 'text' must be a unicode or UTF-8 encoded string. 20 21 """ 22 if isinstance(text, unicode): 23 has_nbsp = u'\xa0' in text 24 text = text.encode('utf-8') 25 else: 26 has_nbsp = False 27 params = urllib.urlencode(dict(v='1.0', 28 langpair='%s|%s' % (from_lang, to_lang), q=text)) 29 try: 30 result = TGURLopener().open('http://ajax.googleapis.com' 31 '/ajax/services/language/translate', params).read() 32 except IOError: 33 text = None 34 else: 35 try: 36 result = simplejson.loads(result) 37 except ValueError: 38 text = None 39 else: 40 try: 41 text = result['responseData']['translatedText'] 42 except (KeyError, TypeError): 43 text = None 44 else: 45 if text and not has_nbsp: 46 text = text.replace(u'\xa0', ' ') 47 return text4850 if len(lang) > 2: 51 country = lang[3:].upper() 52 lang = lang[:2] + "_" + country 53 return lang5456 """Returns a list of languages, by order of preference, based on an 57 HTTP Accept-Language string.See W3C RFC 2616 58 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html) for specification. 59 """ 60 langs = parse_http_accept_header(accept) 61 for index, lang in enumerate(langs): 62 langs[index] = lang_in_gettext_format(lang) 63 return langs6466 """ 67 Returns user locale, using _get_locale or app-specific locale lookup function. 68 """ 69 if not locale: 70 get_locale_f = config.get("i18n.get_locale", _get_locale) 71 locale = get_locale_f() 72 return locale7375 """Default function for returning locale. First looks in session for locale key, 76 then checks the HTTP Accept-Language header, and finally checks the config default 77 locale setting. This can be replaced by your own function by setting cherrypy 78 config setting i18n.get_locale to your function name. 79 """ 80 if not request_available(): 81 return config.get("i18n.default_locale", "en") 82 83 if config.get("session_filter.on", False): 84 locale_key = config.get("i18n.session_key", "locale") 85 locale = cherrypy.session.get(locale_key) 86 if locale: 87 return locale 88 browser_accept_lang = _get_locale_from_accept_header() 89 return browser_accept_lang or config.get("i18n.default_locale", "en")9092 """ 93 Checks HTTP Accept-Language header to find preferred language if any. 94 """ 95 try: 96 header = cherrypy.request.headers.get("Accept-Language") 97 if header: 98 accept_languages = get_accept_languages(header) 99 if accept_languages: 100 return accept_languages[0] 101 except AttributeError: 102 pass103 111
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Thu Jul 14 21:45:42 2011 | http://epydoc.sourceforge.net |