gra*_*s0r 1 grails select internationalization
我只是阅读了很多并用谷歌搜索,但我现在很沮丧。
我有一个 Country 域模型
class Country{
String countryCode //maybe EN, DE, CH...
}
Run Code Online (Sandbox Code Playgroud)
现在我想在 . 我在文档(和谷歌)中读到,可以使用“id”从翻译消息属性文件中选择它。就像是:
country.code.1=America
country.code.2=England
country.code.3=Germany
Run Code Online (Sandbox Code Playgroud)
但这不是我想要的。我想要一些类似的东西:
country.code.US=America
country.code.EN=England
country.code.DE=Germany
Run Code Online (Sandbox Code Playgroud)
所以,我从 stackoverflow 找到了一个可能的解决方案:在 Grails 中翻译一个 HTML select 元素,这对我来说意味着我必须这样说:
<g:select name="country"
from="${allCountries}"
value="${country}"
optionKey="id"
optionValue="${ {countryCode->g.message(code:'country.code.'+countryCode)} }"/>
Run Code Online (Sandbox Code Playgroud)
但我的结果在下拉列表中:“country.code.grails.Country : 1”(对于每个国家,依此类推)
如果我将 gsp-g:select 实现的最后一行更改为:
[...]optionValue="${ {countryCode->g.message(code:'country.code.US')}
Run Code Online (Sandbox Code Playgroud)
正如你看到的硬编码!这有效:-D
希望你能得到我并能帮助我,非常感谢!
有3种可能:
不要将 id 发送到控制器,contryCode而是使用代替id:
<g:select name="contryByCountryCode"
from="${countryCodes}"
valueMessagePrefix="com.yourcompany"/>
Run Code Online (Sandbox Code Playgroud)
将产生:
<select name="contryByCountryCode" id="contryByCountryCode" >
<option value="US">United States<option>
...
</select>
Run Code Online (Sandbox Code Playgroud)
如果您配置了正确的消息。在后端,您需要执行以下操作:
def country = Country.findByCountryCode(params.contryByCountryCode)
Run Code Online (Sandbox Code Playgroud)手动执行:
<select name="contryByCountryCode" id="contryByCountryCode" >
<g:each in="${countryCodes}" var="country">
<option value="${country.id}">
${message(code:"prefix" + country.countryCode)}
<option>
</g:each>
</select>
Run Code Online (Sandbox Code Playgroud)修补程序g:select以防万一optionValue并messagePrefix已定义;-)