grails g:选择i18n [英] grails g:select i18n

查看:241
本文介绍了grails g:选择i18n的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个国家域名模式

  class Country {
String countryCode // maybe EN,DE,CH ...
}

现在我想在里面翻译一下。我阅读了文档(和google)使用id可以从翻译消息属性文件中选择它。例如:

  country.code.1 =美国
country.code.2 =英格兰
国家.code.3 =德国

但这不是我想要的。我想要这样的东西:

  country.code.US = America 
country.code.EN = England
country.code.DE =德国

所以,我从stackoverflow发现了一个可能的解决方案: a href =https://stackoverflow.com/questions/6948644/translate-a-html-select-element-in-grails>翻译Grails中的HTML select元素
这意味着我必须这样做:

 < g:select name =country
from = $ {allCountries}
value =$ {country}
optionKey =id
optionValue =$ {{countryCode-> g.message(code:'country.code 。'+ countryCode)}}/>

但是我的结果在下拉菜单中:country.code.grails.Country:1(和如果我将gsp-g:select实现的最后一行更改为:



pre $ [...] optionValue =$ {{countryCode-> g.message(code:'country.code.US')}

你看到硬编码了!而且这个工程:-D



并可以帮助我,非常感谢你!

解决方案

有$ 3 $的可能性:


  1. 使用 contryCode 而不是 id

     < g:select name =contryByCountryCode
    from =$ { countryCodes}
    valueMessagePrefix = com.yourcompany/>

    会产生:

     <选择名称= contryByCountryCode ID = contryByCountryCode > 
    将选项value =US>美国< option>
    ...
    < / select>

    如果您配置了正确的信息。在后台,你需要做的是这样的:

      DEF国家= Country.findByCountryCode(params.contryByCountryCode)


  2. 手动操作:

     < select name =contryByCountryCodeid =contryByCountryCode> 
    < option value =$ {country.id}>
    $ {message(code:prefix+ country.countryCode)}
    < option>
    < / g:每个>
    < / select>


  3. 补丁 g:select to在 optionValue messagePrefix 的情况下工作; - )



I just read a lot and googled but I'm frustrated right now.

I have a Country domain model

class Country{
   String countryCode //maybe EN, DE, CH...
}

Now I want a translation inside the . I read in the documentation (and with google) that it is possible with the "id" to select it from the translation message property files. Something like:

country.code.1=America
country.code.2=England
country.code.3=Germany

But this is not what I want. I want to have something like:

country.code.US=America
country.code.EN=England
country.code.DE=Germany

So, I found a possible solution from stackoverflow: translate a HTML select element in Grails that would mean for me I have to put it like this:

<g:select name="country"
          from="${allCountries}"
          value="${country}"
          optionKey="id"
          optionValue="${ {countryCode->g.message(code:'country.code.'+countryCode)}  }"/>

But my result is inside the dropdown: "country.code.grails.Country : 1" (and so on for each country)

If I change the last line of the gsp-g:select implementation to:

[...]optionValue="${ {countryCode->g.message(code:'country.code.US')}

as you see hardcoded! And THIS works :-D

Hope you got me and can help me, thank you very much!

解决方案

There are 3 possibilities:

  1. Instead of sending the id to the controller, use the contryCode instead of id:

    <g:select name="contryByCountryCode" 
        from="${countryCodes}" 
        valueMessagePrefix="com.yourcompany"/>
    

    Will produce:

    <select name="contryByCountryCode" id="contryByCountryCode" >
        <option value="US">United States<option>
        ...
    </select>
    

    If you have proper messages configured. In the backend you need to do something like:

    def country = Country.findByCountryCode(params.contryByCountryCode)
    

  2. Do it manually:

    <select name="contryByCountryCode" id="contryByCountryCode" >
        <g:each in="${countryCodes}" var="country">
        <option value="${country.id}">
            ${message(code:"prefix" + country.countryCode)}
        <option>
        </g:each>
    </select>
    

  3. Patch g:select to work in case optionValue and messagePrefix is defined ;-)

这篇关于grails g:选择i18n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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