views:

211

answers:

1

Hi there im having problem with language mappings. The way i want it to work is that language is encoded in the url like /appname/de/mycontroller/whatever

If you go to /appname/mycontroller/action it should check your session and if there is no session pick language based on browser preference and redirect to the language prefixed site. If you have session then it will display english. English does not have en prefix (to make it harder).

So i created mappings like this:

class UrlMappings {
    static mappings = {
        "/$lang/$controller/$action?/$id?"{
            constraints {
                lang(matches:/pl|en/)
            }
        }
        "/$lang/store/$category" {
            controller = "storeItem"
            action = "index"
            constraints {
                lang(matches:/pl|en/)
            }
        }
        "/$lang/store" {
            controller = "storeItem"
            action = "index"
            constraints {
                lang(matches:/pl|en/)
            }
        }


        "/$controller/$action?/$id?"{
            lang="en"
            constraints {
            }
        }
        "/store/$category" {
            lang="en"
            controller = "storeItem"
            action = "index"
        }
        "/store" {
            lang="en"
            controller = "storeItem"
            action = "index"
        }


      "/"(view:"/index")
      "500"(view:'/error')
    }
}

Its not fully working and langs are hardcoded just for now. I think i did something wrong. Some of the reverse mappings work but some dont add language.

If i use link tag and pass params:[lang:'pl'] then it works but if i add params:[lang:'pl', page:2] then it does not. In the second case both lang and page number become parameters in the query string. What is worse they dont affect the locale so page shows in english.

Can anyone please point me to the documentation what are the rules of reverse mappings or even better how to implement such language prefix in a good way ?

THANKS

+1  A: 

The biggest problem you have to deal with is having no prefix for English. Most of your mapping seems totally ok. I would recommend you to work with named mappings.

But first I would recommend you to work with filters for setting a language parameter for every user.

def filters = {
    languageCheck(controller: '*', action: '*') {
        before = {
            if( params.lang == null) {
                redirect( controller: params.controller, action: params.action, params:[ "lang": request.locale.language.toString() ] + params )
            }
        }
    }
}

If a user with missing language parameter enters your site the language is set by the filter and he is redirected to the controller with language parameter. Be careful if you are using a security framework which is also redirecting. You can only redirect once. In that case you have to exclude those urls or controllers from the filter.

Now we will look at your mapping problem. Since Grails 1.2 there are so called Named URL Mappings. E.g.

name storeCategory: "/$lang/store/$category" {
        controller = "storeItem"
        action = "index"
        constraints {
            lang(matches:/pl|en/)
        }
    }

Inside your GSP you can refer to your named mapping with

<g:link mapping="storeCategory" params="[lang:'en', category:'new']">Category</g:link>

This should solve your problem. You can find all about named mappings in the Grails Reference

Maximilian Schweitzer
awesome : -) thanks named mappings seem to be the way to go for me.
Art79