Grails - Language prefix in url mappings

2020-05-06 03:22发布

问题:

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:

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



回答2:

I'd like to add this: if you're having additional params that should be appended using ?param=value, you'll be in trouble if you don't explicitly add them to your URL mappings. This is because the URL mapping resolver respects the controller and action parameter as parameters with a special meaning and will only match mappings that have the exact same set of parameters for generating links.

However, when you're using pagination, you will be getting trouble.

So in addition to the above, do the following:

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/controller/action?offset=10

instead of

/controller/action?offset=10&lang=en