grails g:select i18n

坚强是说给别人听的谎言 提交于 2019-12-11 11:06:35

问题


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!


回答1:


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 ;-)



来源:https://stackoverflow.com/questions/8280715/grails-gselect-i18n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!