I would like to allow the user to change the language, but I can't find why it isn't complying

26 views
Skip to first unread message

João Matos

unread,
Mar 15, 2019, 7:44:00 AM3/15/19
to web2py-users
Hello,

I copied the code from admin\views\layout.html to myapp\views\default\user.html and made 2 small changes to only allow 'en' and 'pt' and to the path.

    {{if hasattr(T,'get_possible_languages_info'):}}
      <select name="adminlanguage" onchange="var date = new Date();cookieDate=date.setTime(date.getTime()+(100*24*60*60*1000));document.cookie='adminLanguage='+this.options[this.selectedIndex].id+'; expires='+cookieDate+'; path='/user/login?_next=/';window.location.reload()">
       {{for langinfo in sorted([(code,info[1]) for code, info in iteritems(T.get_possible_languages_info()) if code != 'default' and code in ['pt', 'en']]):}}
          <option {{=T.accepted_language==langinfo[0] and 'selected' or ''}} {{='id='+langinfo[0]}} >{{=langinfo[1]}}</option>
        {{pass}}
      </select>
    {{pass}}

The page does not refresh and does not change language. The selection sticks.

If I change the path to /, like it is in the admin\views\layout.html, the page refreshes but the selection always returns to 'en' and the page is always in 'en'.

I tried this code (modified) in db.py from another article in the forum:

if 'adminlanguage' in request.cookies and request.cookies['adminlanguage'] is not None:
    print('in cookie')
    T.force(request.cookies['adminlanguage'].value)
else:
    print('force en')
    T.force('en')

but I always get 'force en' in the console.

I've tried with IE and Firefox and checked the option to allow cookies and even added the addresses of my machine (both http://192.168.255.1 and http://192.168.255.1:8000) to always allow cookies section.

Nothing worked.

Thanks,

JM

Leonel Câmara

unread,
Mar 15, 2019, 8:02:38 AM3/15/19
to web2py-users
This is how I do it in one of my websites

in model:

SUPPORTED_LANGUAGES = ('en', 'pt') # or just load this from config

def pick_lang():
    twoletter
= session.lang or T.accepted_language[:2]
   
if twoletter in SUPPORTED_LANGUAGES:
        T
.force(twoletter)
       
return twoletter
   
else:
        T
.force('en')
       
return 'en'
LANG
= pick_lang()


Then default.py

def language():
   
""" Switch the site language """
   
if request.args(0) in SUPPORTED_LANGUAGES:
        session
.lang = request.args(0)
    redirect
(request.vars._next)


In layout.html on the footer

        <ul class="nav nav-pills language-switcher order-3 col-4">
         
<li role="presentation" {{if LANG == 'pt':}}class="active"{{pass}}>
           
<a href="{{=URL('default', 'language', args='pt', vars={'_next':URL(args=request.args, vars=request.vars)})}}">PT</a>
         
</li>
         
<li role="presentation" {{if LANG == 'en':}}class="active"{{pass}}>
           
<a href="{{=URL('default', 'language', args='en', vars={'_next':URL(args=request.args, vars=request.vars)})}}">EN</a>
         
</li>
       
</ul>





João Matos

unread,
Mar 15, 2019, 8:47:53 AM3/15/19
to web2py-users
It worked, thanks.
Reply all
Reply to author
Forward
0 new messages