问题
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:
Instead of sending the id to the controller, use the
contryCode
instead ofid
:<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)
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>
Patch
g:select
to work in caseoptionValue
andmessagePrefix
is defined ;-)
来源:https://stackoverflow.com/questions/8280715/grails-gselect-i18n