簡體   English   中英

在Grails中,如何獲取locale的ConfigObject of messages.properties?

[英]In Grails, how can I get a ConfigObject of messages.properties for locale?

在Grails中,我想獲得對當前語言環境的已加載消息屬性文件的ConfigObject引用。 或者某種方式可以輕松地讀取消息屬性(對於當前語言環境)的全部內容。 我想將其轉換為JSON並將其發送回客戶端以用於通過javascript查找字符串。

本質上我想做這樣的事情:

def props = new java.util.Properties()
props.load(... the right message bundle ...);
def messages = new ConfigSlurper().parse(props)
render messages as JSON

我假設有更優雅的方式來做到這一點。 messageSource接口僅允許您獲取特定鍵的消息。 我想要整個資源包,所以我可以將它轉換為JSON。

我找到了一個可行的解決方案,只需根據當前的語言環境從正確的消息屬性包中直接加載屬性。

看起來我只能使用相對於應用程序根目錄的路徑加載文件。 這適用於本地運行嵌入式tomcat和戰爭('grails run-app'和'grails run-war')但我沒有測試部署到容器以了解路徑是否將被正確解析。

這是我的測試控制器:

import grails.converters.*
import org.springframework.context.i18n.LocaleContextHolder as LCH 

class I18nController {

    def index = {
        def locale = LCH.getLocale().toString();
        def langSuffix = ( locale == "en" ) ? "" : "_${locale}" 
        def props = new java.util.Properties()
        props.load( new FileInputStream( "grails-app/i18n/messages${langSuffix}.properties" ) )
        render ( new ConfigSlurper().parse(props) ) as JSON
    }

}

可以訪問如下:

http://localhost:8080/myapp/i18n
http://localhost:8080/myapp/i18n?lang=es
http://localhost:8080/myapp/i18n?lang=en

我知道這是舊的,但我來到這里尋找完全相同的事情。 使用LocaleContextHolder以獲得所需的語言環境是一個很好的起點,但我決定使用RequestContextUtils 在我的實現中,我想使用java自己的語言環境解析策略。 所以這里(目前使用grails 2.1.2):

// Controller
import org.springframework.web.servlet.support.RequestContextUtils
import grails.converters.JSON


class I18nController {
    def strings() {
    ResourceBundle clientMessages = ResourceBundle.getBundle("com.example.ClientMessages",
        RequestContextUtils.getLocale(request),
        Thread.currentThread().contextClassLoader)
    render clientMessages as JSON
    }
}

當你使用默認的JSON編組器序列化這個東西時,它不是你想要的。 所以將它添加到init閉包內的BootStrap.groovy

    // JSON Marshaller to serialize ResourceBundle to string table.
    JSON.registerObjectMarshaller(ResourceBundle) { bundle ->
        def returnObject = [:]
        bundle.keys.each {
            returnObject."${it}" = bundle.getString(it)
        }
        returnObject
    }

最后,將您要發送的資源放入類路徑中的javascript客戶端。 在我的示例中,這些將放在src / java / com / example / ClientMessages.properties中。

size.small=Small
size.wide=Wide
size.large=Large

在客戶端,轉到myapp/i18n/strings你會看到像這樣的JSON:

{"size.small":"Small","size.wide":"Wide","size.large":"Large"}

因此,使用此解決方案,您將所有和僅要發送的字符串放到javascript端進行查找,並將其他所有內容放在grails i18n文件夾中。 需要注意的是,此處的字符串不適用於g:message,反之亦然。 如果有人能找到解決辦法在i18n中為此目的單獨輸出一個基本名稱,我希望看到它。

MessageSource的實現類型是org.codehaus.groovy.grails.context.support.PluginAwareResourceBundleMessageSource 也許這個類(或其中一個父類)有方法,這將允許您獲得對整個Properties集的引用。

以下看起來可能有用(雖然我還沒有測試過):

// Get a reference to the message Source either via dependency injection or looking-up
// the bean in the application context
def messageSource
Properties messages = messageSource.getProperties("messages.properties").properties
// Now convert the Properties instance to JSON using your favorite Java-JSON library

這不是一個很好的解決方案,因為getProperties(filename)方法受到保護,所以你不應該調用它,但你可以因為Groovy中的一個錯誤。 它還對mesageSource的實現類型做了一些隱含的假設。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM