#NEWLINE# @ weblab_api . route_webclient ( '/locales.json' ) #NEWLINE# def locales( ) : #NEWLINE# #INDENT# lang = request . args . get ( 'lang' ) #NEWLINE# if ( not lang ) : #NEWLINE# #INDENT# lang = 'en' #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# babel . Locale . parse ( lang ) #NEWLINE# #UNINDENT# except ( babel . core . UnknownLocaleError , ValueError ) as e : #NEWLINE# #INDENT# return ( 'Invalid language' , 400 ) #NEWLINE# #UNINDENT# lang_contents = PER_LANG . get ( lang ) #NEWLINE# if ( lang_contents is None ) : #NEWLINE# #INDENT# fname = data_filename ( 'weblab/core/templates/webclient/locales.json' ) #NEWLINE# try : #NEWLINE# #INDENT# modification_time = os . stat ( fname ) . st_mtime #NEWLINE# last_modified = datetime . datetime . fromtimestamp ( modification_time ) #NEWLINE# #UNINDENT# except Exception as e : #NEWLINE# #INDENT# print ( ( 'Could not calculate the time for %s' % fname ) ) #NEWLINE# traceback . print_exc ( ) #NEWLINE# last_modified = datetime . datetime . now ( ) #NEWLINE# #UNINDENT# messages_directory = data_filename ( 'weblab/core/translations' ) #NEWLINE# messages_file = data_filename ( 'weblab/core/translations/{0}/LC_MESSAGES/messages.mo' . format ( lang ) ) #NEWLINE# if os . path . exists ( messages_file ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# messages_modification_time = os . stat ( fname ) . st_mtime #NEWLINE# messages_last_modified = datetime . datetime . fromtimestamp ( messages_modification_time ) #NEWLINE# #UNINDENT# except Exception as e : #NEWLINE# #INDENT# messages_last_modified = datetime . datetime . now ( ) #NEWLINE# #UNINDENT# last_modified = max ( last_modified , messages_last_modified ) #NEWLINE# #UNINDENT# def ng_gettext( text ) : #NEWLINE# #INDENT# "Wrapper of gettext. It uses the messages_file to load particular translations (e.g. if 'es' is requested, it uses the translations for Spanish)." #NEWLINE# translation = babel . support . Translations . load ( messages_directory , lang ) #NEWLINE# translated_text = translation . gettext ( text ) #NEWLINE# return translated_text #NEWLINE# #UNINDENT# contents = render_template ( 'webclient/locales.json' , ng_gettext = ng_gettext ) #NEWLINE# etag = hashlib . new ( 'sha1' , contents ) . hexdigest ( ) #NEWLINE# lang_contents = { #NEWLINE# #INDENT# 'last_modified' : last_modified . replace ( microsecond = 0 ) , #NEWLINE# 'contents' : contents , #NEWLINE# 'etag' : etag , #NEWLINE# #UNINDENT# } #NEWLINE# PER_LANG [ lang ] = lang_contents #NEWLINE# #UNINDENT# if ( ( request . if_none_match is not None ) and request . if_none_match . contains ( lang_contents [ 'etag' ] ) ) : #NEWLINE# #INDENT# return Response ( status = 304 ) #NEWLINE# #UNINDENT# if ( ( request . if_modified_since is not None ) and ( request . if_modified_since >= lang_contents [ 'last_modified' ] ) ) : #NEWLINE# #INDENT# return Response ( status = 304 ) #NEWLINE# #UNINDENT# response = Response ( lang_contents [ 'contents' ] ) #NEWLINE# response . mimetype = 'application/json' #NEWLINE# response . last_modified = lang_contents [ 'last_modified' ] #NEWLINE# response . set_etag ( lang_contents [ 'etag' ] ) #NEWLINE# return response