chelder
chelder

Reputation: 3987

How to change the default message.properties

Let's say we have the following line of code:

<p>    <g:message code="nav.usuario.show"  />    </p>

If we are using an italian computer, Grails will look at messages_it.properties first. If grails desn't find nav.usuario.show=textLabel there, will try to find it in messages.properties. I want to change this behavior to look at message_es.properties instead the default messages.properties (but only if the label is not in the current locale language)

I tried the following code, but I didn't see any change. resources.groovy:

beans = {   
    localeResolver(org.springframework.web.servlet.i18n.SessionLocaleResolver) {
        defaultLocale = new Locale("it","")
        java.util.Locale.setDefault(defaultLocale)
    }   
}

Upvotes: 1

Views: 4924

Answers (3)

chelder
chelder

Reputation: 3987

Simply copy the code of the language you want by default (for example messages_es.properties) to the default messages.properties file.

If you want to keep the English language, you have to create a new file with a name like messages_en.properties. Move the code of messages.properties there.

Upvotes: 5

dmahapatro
dmahapatro

Reputation: 50245

beans = {   
    localeResolver(org.springframework.web.servlet.i18n.SessionLocaleResolver)   
}

Then setup a filter to change the default locale based on the request.

//Filter
class LocaleFilters {
    def localeResolver

    def filters = {
        localize(controller: '*') {
            before = {
                 Locale.setDefault(localeResolver.resolveLocale(request))
                 return true
            }
        }
    }
}

If the local context is Spanish then the default sets to _es, so on an so forth for other locales based on which locale the application is accessed.

Upvotes: 0

Chris
Chris

Reputation: 8109

If you want to fix the locale, put the following lines into the init closure of your BootStrap.groovy:

TimeZone.setDefault(TimeZone.getTimeZone("CET"))
Locale.setDefault(new Locale("it"));

or

Locale.setDefault(new Locale("es"));

Upvotes: 0

Related Questions