开发者

Grails i18n - How to switch from messages to codes

The following code will display the label associated with the code my.message.code in the i18n properties files:

<g:message code="my.message.code" />

This is very easy, convenient and works well but I would like to enable on demand a switch so it displays my.error.code instead in a running application.

The purpose of such a functionality would be to enabl开发者_Go百科e users of the Grails application to ask modifications for a particular code, it would enable an easy way to review and batch modify the application by translators.

Is it possible with Grails?

Thanks.


You could create a custom tag to do that through a taglib (I believe you can override the g namespace and message tag if you needed):

      def messageWithCode = {attrs, body ->
         if (attrs.showCode) {//You will need to pass a boolean (or retrieve from a 
// config file) to indicate whether to show the message or the code
            out << attrs.code
          } else {
            out << g.message(attrs)  
          }
       }

Also, grails will always display the code when it cannot find the message associated with it.


you can override the standard message tag. Not a clean solution, but it will work:

fetch the source from http://grails.org/doc/latest/ref/Tags/message.html#message , create your own taglib (with standard namespace 'g') and copy and paste the source to your taglib. This will avoid having to change all your gsp files.

Now change the code in such a way that it will display the message code (as example) if the locale has a special value like 'code'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜