Last active
December 20, 2015 03:29
-
-
Save sergiomichels/6063706 to your computer and use it in GitHub Desktop.
Transform messages in JSON that can be parsed in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.wordpress.sergiosmind.lang | |
| import grails.converters.JSON | |
| import org.springframework.context.support.ReloadableResourceBundleMessageSource.PropertiesHolder | |
| class I18NService { | |
| /** Dependency Injection for ExposedKeysMessageSource */ | |
| def messageSource | |
| /** | |
| * Get's all messages for the Locale and encode to a JSON String. | |
| */ | |
| String messagesToJavaScript(Locale locale) { | |
| def messages = [:] | |
| List<PropertiesHolder> holders = messageSource.getAllProperties(locale) | |
| for(PropertiesHolder holder : holders) { | |
| Properties props = holder.getProperties() | |
| Set keys = props.keySet() | |
| for(String key : keys) { | |
| messages[key] = props.get(key) | |
| } | |
| } | |
| return ((messages as JSON).encodeAsJavaScript()) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment