In Grails, how can I get ConfigObject of messages.properties for locale?

In Grails, I would like to get the ConfigObject link of a file of uploaded message files for the current locale. Or some way is easy to read in the message properties (for the current locale) in it entirely. I want to convert it to JSON and send it back to the client, which will be used to search for strings through javascript.

In essence, I want to do something like this:

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

I guess there is a more graceful way to do this. The messageSource interface allows you to receive messages only for a specific key. I want an entire resource kit to convert it to JSON.

+3
source share
3 answers

I found a workable solution to simply load properties directly from the corresponding set of message properties based on the current locale.

It looks like I can just upload the file along the path relative to the root of the application. This worked for local use with both the built-in tomcat and as a war ("grails run-app" and "grails run-war"), but I did not test it in the container to see if the path would be resolved correctly.

Here is my test controller:

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
    }

}

You can access, for example:

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

, , , . LocaleContextHolder , RequestContextUtils. JavaScript. , ( 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 , , . BootStrap.groovy init:

    // 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 , .

0

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.

-1
source

Source: https://habr.com/ru/post/1760651/


All Articles