Search code examples
grailsinternationalizationgsp

Grails internationaliization with custom bundles


My Grails (2.4.2) app was created with a bunch of "default/standard" resource bundles:

myapp/
    grails-app/
        i18n/
            messages.properties
            messages_fr.properties

I would now like to create my own "custom" resource bundle, that is, define properties in a file outside of these standard messages*.properties files that myapp was created with.

According to the i18n documentation, all bundles need to be prefixed with messages and suffixed .properties. So I added two new props files, one for English and one for French:

myapp/
    grails-app/
        i18n/
            messages.properties
            messages_fr.properties
            messages_myapp.properties
            messages_myapp_fr.properties

For one, I'm not 100% sure I'm interpreting the docs correctly. So if anything about my 2 new props files jumps out at you as being incorrect, please start by letting me know!

Having said that, in all the example from those docs, I don't see where you specify the bundle to use. All of the examples look like this:

<g:message code="fizz.buzz.foo" />

But what if I have a fizz.buzz.foo property defined in both messages_blah.properties and messages_bar.properties?

So I ask: How do I add my own custom resource bundles, and how do I properly refer to them from inside a GSP?


Solution

  • To answer your question you have to understand what Grails (well, Spring really) is doing to accomplish this.

    You are on the right path with the multiple files. What you have outlined there matches the documentation and will work.

    However, under the covers what is really being done is they are being combined into a single bundle (per language). So there is no need to tell Grails/Spring which bundle to use.

    Finally, what happens when the same key is defined multiple times? The first one matched wins. I seem to recall that the order in which the bundles are combined is in file name order, though you should be able to test this pretty quickly.

    Hope this helps, and best of luck!