Search code examples
grailsinternationalization

Force Grails to use message.properties with correct Language Code?


I use message.propertiesfor i18n. I have the the following property files.

message.properties
message_de.properties

This works fine when I do something like this:

Locale locale = new Locale("de")
def test = g.message(code:'some.code', locale: locale)

The problem is when I use a Locale like this:

Locale locale = new Locale("de_DE")

In the former case Grails searches for a property file message_de_DE.properties which does not exist so it falls back to message.properties.

How can I force Grails to do the following?

  1. Check if there is a properties file which matches the requested Language code and Country code of the Locale.
  2. Check if there is a properties file which matches the requested Language code
  3. If 1. and 2. fails fall back to message.properties.

Solution

  • Just changing

        Locale locale = new Locale("de_DE")
    

    to

        Locale locale = new Locale("de", "DE")
    

    should achieve what you want.

    AFAIK Grails by default does what you mentioned in your points, there is no need to force.

    The single argument constructor of Locale expects the argument to be a language name, but de_DE is not a valid language name (This may be the reason grails falls back to message.properties). You have to use the other variant of the constructors which take language name as the first argument and country name as the second. The doc.