from cuchulu.libraries.i18n import I18n
from bottle import get,response,request
#from cuchulu.libraries.sessions import get_session
from cuchulu.libraries.sessionplugin import get_session, session_plugin
from cuchulu.libraries.urls import redirect
import re

@get('/change_lang/<lang>', name="change_lang")
@session_plugin
def index(lang):
    
    if lang in I18n.dict_i18n:
        
        s=get_session()
        
        s['lang']=lang
        
        s.save()
        
        redirect_url=request.headers.get('Referer')
        
        if redirect_url!=None:
            
            #if not re.match('.*\/change_lang\/.*', request.path):
        
            redirect(redirect_url)
        
    return {'error': 1, 'message': 'No referer for redirect'}
    


