Translate Controller names and Function names

118 views
Skip to first unread message

Francisco Costa

unread,
Jan 27, 2015, 7:12:42 AM1/27/15
to web...@googlegroups.com
I'm using the Parameter-based rewrite system and I would like to find a strategy to **translate controller names and function names**.

I have 2 domains and I use only one app for both, but I would like to have the controllers and functions translated for the non-english one.

Something like this:

 - http://domain.com/article/new (english - default language)
 - http://domain.pt/artigo/novo    (portuguese - translation)

What would be the best way to achieve this?

PS: I also would like to have some redirection if someone typed the wrong domain extension:


Francisco Costa

unread,
Jan 27, 2015, 8:51:31 AM1/27/15
to
I have this in models till now:

views_translate = {
   
# Controllers Names
   
'default'   : 'principal',
   
'principal' : 'default',
   
'articles'  : 'artigos',
   
'artigos'   : 'articles',
   
# Function Names
   
'index'     : 'inicio',
   
'inicio'    : 'index',
   
'new'       : 'novo',
   
'novo'      : 'new',
   
'search'    : 'procurar',
   
'procurar'  : 'search',
}


controllers_translate
= {
   
'default'   : T('default', lazy=False),
   
'principal' : T('default', lazy=False),
   
'articles'  : T('articles', lazy=False),
   
'artigos'   : T('articles', lazy=False),
}


functions_translate
= {
   
'index'     : T('index', lazy=False),
   
'inicio'    : T('index', lazy=False),
   
'new'       : T('new', lazy=False),
   
'novo'      : T('new', lazy=False),
   
'search'    : T('search', lazy=False),
   
'procurar'  : T('search', lazy=False),
}


#Translate Views
if T.accepted_language != 'en' and request.extension and request.extension == 'html':
    response
.view = '%s/%s.html' % (views_translate[request.controller], views_translate[request.function])


#Redirect controllers and functions if necessary
if not T.accepted_language == 'en':
   
if request.controller != controllers_translate[request.controller] or request.function != functions_translate[request.function]:
        redirect
(URL(scheme='https', host=True, r=request, c=controllers_translate[request.controller], f=functions_translate[request.function], args=request.args, vars=request.vars), how=301)
else:
   
if request.controller != controllers_translate[request.controller] or request.function != functions_translate[request.function]:
        redirect
(URL(scheme='https', host=True, r=request, c=controllers_translate[request.controller], f=functions_translate[request.function], args=request.args, vars=request.vars), how=301)



But on the views I also have to assign translation for every URL() function:

{{=URL(r=request, c=T('articles', lazy=False), f=T('search', lazy=False))}}


I also had to link the controller:
$ ln default.py principal.py
$ ln articles
.py artigos.py


and created a translated function pointing to the main one at the bottom of each controller:

def inicio():
   
return index()

def novo():
   
return new()

def procurar():
   
return search()


Is there a better way to do this?

Niphlod

unread,
Jan 27, 2015, 9:05:26 AM1/27/15
to web...@googlegroups.com
IMHO you're better suited for routes. As long at the mapping is fixed, it's exactly what they've been engineered for. I'd use the pattern-based system


On Tuesday, January 27, 2015 at 2:51:31 PM UTC+1, Francisco Costa wrote:
I have this in models till now:

views_translate = {
   
# Controllers Names
   
'default'   : 'principal',
   
'principal' : 'default',

   
'articles'  : 'artigo',
   
'artigo'    : 'articles',

   
# Function Names
   
'index'     : 'inicio',
   
'inicio'    : 'index',
   
'new'       : 'novo',
   
'novo'      : 'new',
   
'search'    : 'procurar',
   
'procurar'  : 'search',
}


controllers_translate
= {
   
'default'   : T('default', lazy=False),
   
'principal' : T('default', lazy=False),
   
'articles'  : T('articles', lazy=False),

   
'artigo'    : T('articles', lazy=False),

}


functions_translate
= {
   
'index'     : T('index', lazy=False),
   
'inicio'    : T('index', lazy=False),
   
'new'       : T('new', lazy=False),
   
'novo'      : T('new', lazy=False),
   
'search'    : T('search', lazy=False),
   
'procurar'  : T('search', lazy=False),
}


#Translate Views
if T.accepted_language != 'en' and request.extension and request.extension == 'html':
    response
.view = '%s/%s.html' % (views_translate[request.controller], views_translate[request.function])


#Redirect controllers and functions if necessary
if not T.accepted_language == 'en':
   
if request.controller != controllers_translate[request.controller] or request.function != functions_translate[request.function]:
        redirect
(URL(scheme='https', host=True, r=request, c=controllers_translate[request.controller], f=functions_translate[request.function], args=request.args, vars=request.vars), how=301)
else:
   
if request.controller != controllers_translate[request.controller] or request.function != functions_translate[request.function]:
        redirect
(URL(scheme='https', host=True, r=request, c=controllers_translate[request.controller], f=functions_translate[request.function], args=request.args, vars=request.vars), how=301)



But on the views I also have to assign translation for every URL() function:

{{=URL(r=request, c=T('articles', lazy=False), f=T('search', lazy=False))}}



is there a better way to do this?

Francisco Costa

unread,
Jan 27, 2015, 9:24:34 AM1/27/15
to web...@googlegroups.com
unfortunately it seams you can't use both rewrite systems, and I have other apps using the Parameter-based system

Jonathan Lundell

unread,
Jan 27, 2015, 9:35:02 AM1/27/15
to web...@googlegroups.com
I think that Niphlod is probably right, though I might be inclined to stick with your programmatic logic rather than use pattern-based routing. And maybe write a wrapper for URL() so that all the logic is in the same place and driven off the same tables.

Francisco Costa

unread,
Jan 27, 2015, 9:39:44 AM1/27/15
to web...@googlegroups.com
Tx Jonathan, how would a wrapper for the URL() look like?

Leonel Câmara

unread,
Jan 27, 2015, 10:58:53 AM1/27/15
to
This is a good question, maybe there should be more direct web2py support for it, as usability wise it's a nice feature.

A wrapper is just a function that calls the other for you, like a decorator. In your case you could have something like this in your models.

def TURL(**kwargs):
    if 'c' in kwargs:
        kwargs['c'] = T(kwargs['c'], lazy=False)
    if 'f' in kwargs:
        kwargs['f'] = T(kwargs['f'], lazy=False)
    return URL(**kwargs)

Then you could use it in the views
{{=TURL(r=request, c='articles', f='search')}}


Could Francisco use pattern based routing just for this app if he puts it in the application's folder routes.py?

Derek

unread,
Jan 27, 2015, 11:38:33 AM1/27/15
to web...@googlegroups.com
Honestly, I have never seen a need for this. Any time I visit a foreign language site, the URL mapping is in english. Chinese, Japanese, Russian, all seem to keep the URL pretty much the same regardless of the language. I can usually hunt my way around a foreign language website by the links.For example, on a Russian site, i'll hover over 'novosti' to find the url '/news/' etc etc.

Carlos Costa

unread,
Jan 27, 2015, 12:14:48 PM1/27/15
to web...@googlegroups.com
Very interesting question. It must have some effect on SEO.
I have seen this post but it does not seem to solve exactly this 

Although if you have a different URL for different content it will be indexed.
If the two apps are working in two different domains they will be indexed accordingly.

I tried to do what you want by interception the request object but it doe not work.

One way is to use routes.py and write one rule per URL.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--


Carlos J. Costa
Cientista da Computação  | BS Computer Science
Esp. Gestão em Telecom   |
PgC Telecom Mangement
<º))><

Francisco Costa

unread,
Jan 27, 2015, 2:29:38 PM1/27/15
to web...@googlegroups.com

On Tuesday, 27 January 2015 17:14:48 UTC, Carlos Costa wrote:
Very interesting question. It must have some effect on SEO.
I have seen this post but it does not seem to solve exactly this 

Yes, that's why I'm trying to figure this out.

This would be a valuable feature for web2py framework. Where can you add this to the roadmap?


Jonathan Lundell

unread,
Jan 28, 2015, 9:50:51 AM1/28/15
to web...@googlegroups.com
There's also the alternative of embedding a language code in the URL and not translating the URL elements. The parameter-based routing system already supports that, and it would give you separate search-engine indexing.


On Tuesday, January 27, 2015 at 4:12:42 AM UTC-8, Francisco Costa wrote:

Francisco Costa

unread,
Jan 28, 2015, 10:24:06 AM1/28/15
to web...@googlegroups.com


On Wednesday, 28 January 2015 14:50:51 UTC, Jonathan Lundell wrote:
There's also the alternative of embedding a language code in the URL and not translating the URL elements. The parameter-based routing system already supports that, and it would give you separate search-engine indexing.


Tx Jonathan, but in my case I will use 2 different domains, and I would like to have the full url translated without have to have 2 different applications 
Reply all
Reply to author
Forward
0 new messages