2012-10-11 22 views

Trả lời

9

Cuối cùng tôi tìm thấy một câu trả lời - ghi đè mặc định Grails messageSource:

class ExtendedPluginAwareResourceBundleMessageSource extends PluginAwareResourceBundleMessageSource { 
    Map<String, String> listMessageCodes(Locale locale) { 
     Properties properties = getMergedProperties(locale).properties 
     Properties pluginProperties = getMergedPluginProperties(locale).properties 
     return properties.plus(pluginProperties) 
    } 
} 

Trong grails-app/conf/mùa xuân/resources.groovy:

beans = { 
    messageSource(ExtendedPluginAwareResourceBundleMessageSource) { 
     basenames = "WEB-INF/grails-app/i18n/messages" 
    } 
} 

mã điều khiển tương ứng:

class MessageController { 
    def messageSource 

    def index = { 
     def messageMap = messageSource.listMessageCodes(request.locale) 

     render (contentType: "text/xml") { 
      messageMap {key, message -> 
       .. 
      } 
     } 
    } 
} 
+0

Tôi đã thử điều này và nó không hoạt động, nguồn tin nhắn mở rộng đã không được sử dụng. Có thể liên quan đến thông báo sau tôi nhận được khi khởi động 'java.lang.RuntimeException: Nạp lại tác nhân đã thoát qua ngoại lệ, vui lòng nêu lên một lỗi jira | tại org.springsource.loaded.agent.ClassPreProcessorAgentAdapter.transform (ClassPreProcessorAgentAdapter.java:104) 'Vì vậy, nếu lúc đầu điều này không hoạt động', đừng lo, chỉ cần thử lại sau và nó sẽ hoạt động –

+0

' request.locale' didn 't đúng cách làm việc cho tôi. Tôi đã sử dụng 'RequestContextUtils.getLocale (yêu cầu)' thay thế. – haisi

0

Cách tiếp cận bạn đang dùng không giống là có thể dựa trên tài liệu API cho PluginAwareResourceBundleMessageSource. Điều này sẽ giúp bạn có được đóng đến một giải pháp

class MessageController { 
    def messageSource 

    def index = { 
     Locale locale = new Locale('en'); 
     List codes = ['default.paginate.prev','default.paginate.next','default.boolean.true','default.boolean.false'] 
     def messageMap = messagesForCodes(codes,locale) 
     render (contentType: "text/xml") { 
      messageMap {key, message -> 
       .. 
      } 
     } 
    } 

    private def messagesForCodes(codes, locale){ 
     Map messages = [:] 
     codes.each{code-> 
      messages[code] = messageSource.getMessage(code,null,locale) 
     } 
     messages 
    } 
} 
+0

Cảm ơn bạn đã trả lời tôi. Nhưng vấn đề là tôi không muốn biết tất cả các mã trước đây. – Adrian

Các vấn đề liên quan