【问题标题】:grails g:select i18ngrails g:选择 i18n
【发布时间】:2011-11-26 18:44:39
【问题描述】:

我刚刚阅读了很多内容并在 Google 上搜索过,但我现在很沮丧。

我有一个 Country 域模型

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

现在我想在 .我在documentation(和google)中读到,可以使用“id”从翻译消息属性文件中选择它。比如:

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

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

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

所以,我从 stackoverflow 中找到了一个可能的解决方案:translate a HTML select element in Grails 这对我来说意味着我必须这样说:

<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 实现的最后一行更改为:

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

如您所见,硬编码!这很有效:-D

希望你能得到我并能帮助我,非常感谢!

【问题讨论】:

    标签: grails select internationalization


    【解决方案1】:

    有3种可能:

    1. 不要将 id 发送给控制器,而是使用 contryCode 而不是 id

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

      将产生:

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

      如果您配置了正确的消息。在后端,您需要执行以下操作:

      def country = Country.findByCountryCode(params.contryByCountryCode)
      
    2. 手动操作:

      <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. 修补 g:select 以在 optionValuemessagePrefix 被定义的情况下工作 ;-)

    【讨论】:

    • 否 - 不起作用。我的下拉列表中充满了 countryCode 的数据库值,例如“US”、“DE”等:-(
    • grails 将通过查找从valueMessagePrefix + "." + value 派生的密钥的消息来解析标签
    猜你喜欢
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多