Have some multiline strings that are presented to the user and stored as Heredocs. So rather than a 'normal' (Java) property file, a groovy-based one (see here) to be consumed by ConfigSlurper was used and works great. Sorry if this is a dumb question, but can that be easily internationalized? If so, can you outline how that is accomplished?
Asked
Active
Viewed 237 times
0
-
Have you tried [i18n in Groovy](http://groovy.codehaus.org/Internationalization) yet? – dmahapatro Sep 12 '13 at 19:17
-
If I do that (which very well might be the best option!), I think I have to do this for multi-line properties: `prop1=first line of prop1 \ second line of prop1\ third line of prop1` – JoeG Sep 13 '13 at 14:29
-
Sorry, can't make the above comment 'look' right on multiple lines - hopefully you understand that would be a 3 line example... – JoeG Sep 13 '13 at 14:34
2 Answers
1
My solution: In your ConfigSlurper
you should store keys to the internalized strings. Inject messageSource
and localResolver
in your controller/service, get key from your ConfigSlurper
and find localized string in your i18n messages.property file. Example (not sure that code is correct, but it's the main idea):
def config = new ConfigSlurper().parse(new File('src/Config.groovy').toURL())
//localized string1 value
def msg = messageSource.getMessage(config.data1.string1, null, localeResolver.defaultLocale)

rxn1d
- 1,236
- 6
- 18
-
If I follow this correctly, then I would not be able to use heredoc's but would need regular property files with the multi-line values with the trailing backslashes. Is that correct? – JoeG Sep 13 '13 at 14:16
0
As far as I know the ConfigSlurper
does not have special support for i18n.
You may achieve it by using the leveraging its support for environments by creating an environment closure per locale. For example:
environments {
english {
sample {
hello = "hello"
}
}
spanish {
sample {
hello = "hola"
}
}
}
When creating the ConfigSlurper
you will need to pass the desired language:
def config = new ConfigSlurper("spanish")

Dror Bereznitsky
- 20,048
- 3
- 48
- 57
-
I think this would 'work', but isn't very desirable as the settings for all the languages (locales) would need to be in the same file... – JoeG Sep 13 '13 at 14:24
-
you can split them between multiple files and merge them all into a single configuration (supported by ConfigSlurper) – Dror Bereznitsky Sep 15 '13 at 06:13
-
Hi - I am selecting this as the 'answer' even though I am not very happy with it - I would hope that the property extensions Groovy adds implied by ConfigSlurper would be extended to provide more natural support of I18N... – JoeG Sep 16 '13 at 11:46