<s> import re <EOL> from bottle import request <EOL> from babel import support <EOL> from . config import Config <EOL> REQUEST_ACCEPT_LANGUAGE_RE = re . compile ( r'''<STR_LIT>''' , re . VERBOSE ) <EOL> def extra_client_expected_langs ( ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> pieces = REQUEST_ACCEPT_LANGUAGE_RE . split ( <EOL> request . headers . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if pieces [ - <NUM_LIT:1> ] : <EOL> return [ ] <EOL> for i in range ( <NUM_LIT:0> , len ( pieces ) - <NUM_LIT:1> , <NUM_LIT:3> ) : <EOL> first , lang , priority = pieces [ i : i + <NUM_LIT:3> ] <EOL> if lang == '<STR_LIT:*>' : <EOL> return [ ] <EOL> if first : <EOL> return [ ] <EOL> priority = priority and float ( priority ) or <NUM_LIT:1.0> <EOL> result . append ( ( lang , priority ) ) <EOL> result . sort ( key = lambda k : k [ <NUM_LIT:1> ] , reverse = True ) <EOL> return result <EOL> def get_ui_lc ( ) : <EOL> """<STR_LIT>""" <EOL> config = Config ( ) <EOL> domain_lc = request . url . split ( '<STR_LIT>' ) [ <NUM_LIT:1> ] . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> if domain_lc not in config . supported_languages : <EOL> domain_lc = None <EOL> ui_lc = domain_lc <EOL> session = request . environ . get ( '<STR_LIT>' , { } ) <EOL> if not domain_lc and session . get ( '<STR_LIT>' , None ) : <EOL> ui_lc = session . get ( '<STR_LIT>' , None ) <EOL> return ui_lc <EOL> def get_language_list ( ) : <EOL> """<STR_LIT>""" <EOL> config = Config ( ) <EOL> lang_code = getattr ( config , '<STR_LIT>' , '<STR_LIT>' ) <EOL> expected_langs = extra_client_expected_langs ( ) <EOL> lang_codes = [ ] <EOL> for lang , priority in expected_langs : <EOL> lang_country = lang . split ( '<STR_LIT:->' ) <EOL> if len ( lang_country ) == <NUM_LIT:1> : <EOL> lang_codes . append ( lang ) <EOL> continue <EOL> country = lang_country [ <NUM_LIT:1> ] <EOL> lang_codes . append ( '<STR_LIT>' % ( lang_country [ <NUM_LIT:0> ] , country ) ) <EOL> lang_codes . append ( '<STR_LIT>' % ( lang_country [ <NUM_LIT:0> ] , country . swapcase ( ) ) ) <EOL> if lang_code is not None : <EOL> lang_codes += [ lang_code ] <EOL> ui_lc = get_ui_lc ( ) <EOL> if ui_lc is not None : <EOL> lang_codes = [ ui_lc ] + lang_codes <EOL> return lang_codes <EOL> def locale_selector_func ( ) : <EOL> lc_list = get_language_list ( ) <EOL> if lc_list : <EOL> return lc_list [ <NUM_LIT:0> ] <EOL> return '<STR_LIT>' <EOL> def get_translations ( ) : <EOL> """<STR_LIT>""" <EOL> config = Config ( ) <EOL> domain = getattr ( config , '<STR_LIT>' , '<STR_LIT>' ) <EOL> locale_dir = getattr ( config , '<STR_LIT>' ) <EOL> translations = request . environ . get ( '<STR_LIT>' , None ) <EOL> if translations is None : <EOL> translations = support . Translations . load ( locale_dir , [ get_locale ( ) ] , domain = domain ) <EOL> request . environ [ '<STR_LIT>' ] = translations <EOL> return translations <EOL> def get_locale ( ) : <EOL> """<STR_LIT>""" <EOL> config = Config ( ) <EOL> locale = request . environ . get ( '<STR_LIT>' , None ) <EOL> if locale is None : <EOL> preferred_lc = locale_selector_func ( ) <EOL> if not preferred_lc : <EOL> locale = getattr ( config , '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> locale = preferred_lc <EOL> request . environ [ '<STR_LIT>' ] = locale <EOL> return locale <EOL> def gettext ( string , ** variables ) : <EOL> """<STR_LIT>""" <EOL> t = get_translations ( ) <EOL> if variables : <EOL> if t is None : <EOL> return string % variables <EOL> return t . ugettext ( string ) % variables <EOL> if t is None : <EOL> return string <EOL> return t . ugettext ( string ) <EOL> _ = gettext <EOL> def ngettext ( singular , plural , num , ** variables ) : <EOL> """<STR_LIT>""" <EOL> variables . setdefault ( '<STR_LIT>' , num ) <EOL> t = get_translations ( ) <EOL> if variables : <EOL> if t is None : <EOL> return ( singular if num == <NUM_LIT:1> else plural ) % variables <EOL> return t . ungettext ( singular , plural , num ) % variables <EOL> if t is None : <EOL> return ( singular if num == <NUM_LIT:1> else plural ) <EOL> return t . ungettext ( singular , plural , num ) <EOL> def pgettext ( context , string , ** variables ) : <EOL> """<STR_LIT>""" <EOL> t = get_translations ( ) <EOL> if variables : <EOL> if t is None : <EOL> return string % variables <EOL> return t . upgettext ( context , string ) % variables <EOL> if t is None : <EOL> return string <EOL> return t . upgettext ( context , string ) <EOL> def npgettext ( context , singular , plural , num , ** variables ) : <EOL> """<STR_LIT>""" <EOL> variables . setdefault ( '<STR_LIT>' , num ) <EOL> t = get_translations ( ) <EOL> if variables : <EOL> if t is None : <EOL> return ( singular if num == <NUM_LIT:1> else plural ) % variables <EOL> return t . unpgettext ( context , singular , plural , num ) % variables <EOL> if t is None : <EOL> return ( singular if num == <NUM_LIT:1> else plural ) <EOL> return t . unpgettext ( context , singular , plural , num ) </s>
