Question

If you're using language prefix in URL mappings such as

/$lang/$controller/$action?/$id?

and the URLs you are generating contain parameters in addition to the lang parameter, you'll get the following URL, for an additional param such as offset:

/book/list?offset=10&lang=en

but this breaks my mappings. Instead, I want the URL to look like this:

/en/book/list?offset=10

How can I achieve that?

Was it helpful?

Solution

Create the following bean (assuming that your lang param is called "lang"):

class LangAwareUrlMappingsHolderFactoryBean extends UrlMappingsHolderFactoryBean {

    @Override
    public UrlMappingsHolder getObject() throws Exception {
        def obj = super.object
        obj.DEFAULT_CONTROLLER_PARAMS = [UrlMapping.CONTROLLER, UrlMapping.ACTION,     "lang"] as Set
        obj
    }   
}

And adjust the resources.groovy:

"org.grails.internal.URL_MAPPINGS_HOLDER"(LangAwareUrlMappingsHolderFactoryBean) { bean ->
    bean.lazyInit = true
}

And you'll get

/en/book/list?offset=10

instead of

/book/list?offset=10&lang=en
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top