<s> """<STR_LIT>""" <EOL> import os , sys <EOL> from pytz import timezone , UTC <EOL> from babel import Locale <EOL> from babel . support import LazyProxy , Translations <EOL> from kalapy . conf import settings <EOL> TRANSLATIONS = { } <EOL> def get_translations ( ) : <EOL> """<STR_LIT>""" <EOL> locale = str ( get_locale ( ) ) <EOL> translations = TRANSLATIONS . get ( locale ) <EOL> if translations is None : <EOL> translations = load_translations ( '<STR_LIT>' , locale ) <EOL> if isinstance ( translations , Translations ) : <EOL> for package in settings . INSTALLED_PACKAGES : <EOL> tr = load_translations ( package , locale ) <EOL> if isinstance ( tr , Translations ) : <EOL> translations . merge ( tr ) <EOL> TRANSLATIONS [ locale ] = translations <EOL> return translations <EOL> def load_translations ( import_name , locale ) : <EOL> """<STR_LIT>""" <EOL> if import_name not in sys . modules : <EOL> return None <EOL> path = os . path . abspath ( os . path . dirname ( sys . modules [ import_name ] . __file__ ) ) <EOL> path = os . path . join ( path , '<STR_LIT>' ) <EOL> return Translations . load ( path , [ locale ] ) <EOL> def get_locale ( ) : <EOL> """<STR_LIT>""" <EOL> from kalapy . web import request <EOL> try : <EOL> locale = getattr ( request , '<STR_LIT>' , None ) <EOL> except : <EOL> locale = None <EOL> if locale is None : <EOL> try : <EOL> locale = Locale . parse ( request . accept_languages . best , sep = '<STR_LIT:->' ) <EOL> request . babel_locale = locale <EOL> except : <EOL> locale = Locale . parse ( settings . DEFAULT_LOCALE ) <EOL> return locale <EOL> def get_timezone ( ) : <EOL> """<STR_LIT>""" <EOL> from kalapy . web import request <EOL> try : <EOL> tzinfo = getattr ( request , '<STR_LIT>' , None ) <EOL> except : <EOL> tzinfo = None <EOL> if tzinfo is None : <EOL> tzinfo = timezone ( settings . DEFAULT_TIMEZONE ) <EOL> if request : <EOL> request . babel_tzinfo = tzinfo <EOL> return tzinfo <EOL> def gettext ( string , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> def lazy ( s , ** kw ) : <EOL> return get_translations ( ) . ugettext ( s ) % kw <EOL> return LazyProxy ( lazy , string , ** kwargs ) <EOL> def ngettext ( string , plural , num , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> def lazy ( s , p , n , ** kw ) : <EOL> kw . setdefault ( '<STR_LIT>' , n ) <EOL> return get_translations ( ) . ungettext ( s , p , n ) % kw <EOL> return LazyProxy ( lazy , string , plural , num , ** kwargs ) </s>
