Grails - URL映射中的语言前缀 [英] Grails - Language prefix in url mappings

查看:120
本文介绍了Grails - URL映射中的语言前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有语言映射的问题。我希望它的工作方式是语言编码的URL像/ appname / de / mycontroller /无论什么



如果你去/ appname / mycontroller / action它应检查您的会话,如果没有基于浏览器偏好的会话选择语言并重定向到语言前缀站点。如果你有会话,它会显示英文。所以我创建了这样的映射:



<$ p($ p

$ p $ class UrlMappings {
static mappings = {
/ $ lang / $ controller / $ action?/ $ id?{
constraints {
lang(匹配:/ 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 /)
}
}

$ b $/ $控制器/ $动作?/ $ id?{
lang =en
约束{
}
}
/ store / $ category{
lang =en
controller =storeItem
action =index
}
/ store{
lang =en
controller =storeItem
action =index
}


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


$ / code>

它没有完全工作,langs现在只是硬编码。我想我做错了什么。如果我使用链接标记和传递参数:[lang:'pl']然后它可以工作,但如果我添加params:[lang:'pl',page:2]然后它没有。在第二种情况下,lang和页码都成为查询字符串中的参数。更糟糕的是,他们不会影响语言环境,所以页面显示为英语。



任何人都可以请我指向文档什么是反向映射的规则,甚至更好地实现这样的语言前缀在一个很好的方式?



THANKS

解决方案

你必须处理的最大问题是没有英文的前缀。你的大多数映射看起来完全没问题。我建议您使用命名映射。



但首先我建议您使用过滤器来为每个用户设置语言参数。

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

如果缺少语言参数的用户进入您的站点,语言由过滤器设置,然后用语言参数重定向到控制器。如果您使用的也是重定向的安全框架,请小心。您只能重定向一次。在这种情况下,您必须从过滤器中排除这些网址或控制器。



现在我们来看看您的映射问题。由于Grails 1.2有所谓的命名URL映射。例如

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

在您的GSP中,您可以使用

 < g:link mapping =storeCategoryparams =[lang:'en',category:'new']>类别< / g:link> 

这应该可以解决您的问题。您可以在 Grails Reference


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

解决方案

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

这篇关于Grails - URL映射中的语言前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆