Multi language site

98 views
Skip to first unread message

olivier hubert

unread,
Mar 18, 2015, 12:50:22 PM3/18/15
to web...@googlegroups.com
I try to made a simple multi language site with routes, but I can't.
The routing are correct because I can use the url like this http://127.0.0.1:8000/testme/en/ and translate application are serve.
But translate application is always in french!

translate application is a copy of welcome application with the modifications below.

Web2Py version : 2.9.12-stable+timestamp.2015.01.17.06.11.03 (Running on Rocket 1.2.6, Python 2.7.8)

/web2py/routes.py
------------------------------

routes_in = (
  ('/testme/$language/(?P<any>.*)','/translate/default/$any?_language=$language'), 
)

/translate/models/0.py
-----------------------------

print request.vars
if request.vars._language: 
    T.force(request.vars._language)

Terminal window return:
------------------
<Storage {}>

Gael Princivalle

unread,
Mar 20, 2015, 3:04:24 PM3/20/15
to web...@googlegroups.com
Salut Olivier.

I use that router and for me it works fine:
routers = dict(
BASE = dict(
        domains = {
    "www.mydomain.com" : "my_app",
}
),
my_app = dict(languages=['en', 'fr'], default_language='fr'),
)

The original website (words in the .html files) have to be in english.

After you must edit fr language file for applying translations.
For example:
T('A beautiful journey')
Load one time the .html page.
Search for 'A beautiful journey' in the fr language file.
Translate and save.

For bigger contents make double fields in tables:
product_description_en
product_description_fr

And in the .html file test which is the request.uri_language for displaying one or the other field.

Hopes it could help, bye. 

Gael Princivalle

unread,
Mar 20, 2015, 3:30:21 PM3/20/15
to web...@googlegroups.com
And if you add at the end of the db.py file:
if request.uri_language: T.force(request.uri_language)

it will set the good language.

olivier hubert

unread,
Mar 21, 2015, 4:21:09 PM3/21/15
to web...@googlegroups.com
Thanks for the help Gael (merci) :)

Finally I try another method as routers.
Massimo and other talk about that in another post and a sample can be found in the admin page.

I add in my view a select with all language supported by my application. 
My application need to be compatible with ie9 so I suppress the expiration date otherwise IE9 doesn't create the cookie.

{{if hasattr(T,'get_possible_languages_info'):}}
<select style="margin-top: -5px;" name="userlanguage" onchange="document.cookie='userlanguage='+this.options[this.selectedIndex].id+'; path=/';window.location.reload()">
{{for langinfo in sorted([(code,info[1]) for code,info in T.get_possible_languages_info().iteritems() if code != 'default']):}}
<option {{=T.accepted_language==langinfo[0] and 'selected' or ''}} {{='id='+langinfo[0]}} >{{=langinfo[1]}}</option>
{{pass}}
</select>
{pass}}

in a new file 0.py in my models folder

# set the language
if 'userlanguage' in request.cookies and not (request.cookies['userlanguage'] is None):
T.force(request.cookies['userlanguage'].value)

I have now a language selector compatible with all browser and especially IE9...

Et encore merci pour ton aide :)

Olivier
Reply all
Reply to author
Forward
0 new messages