Grails Programmer : How to change the default locale of your Grails 3 app?

$ grails --version
| Grails Version: 3.1.1
| Groovy Version: 2.4.5
| JVM Version: 1.8.0_45
$ grails create-app myapplocale
| Application created at /Users/groovycalamari/Documents/test/myapplocale
$ cd myapplocale
$ grails run-app
Grails application running at http://localhost:8080 in environment: development

The default welcome screen is shown:

welcometograils

Add a message code at the bottom of grails-app/i18n/messages.properties

grails.welcome=Welcome to Grails

Add a message code at the bottom of the Spanish properties file: grails-app/i18n/messages_es.properties

grails.welcome=Bienvenido a Grails

Edit the file grails-app/views/index.gsp. Replace the line:

<h1>Welcome to Grails</h1>

with:

<h1><g:message code="grails.welcome"/></h1>

If you start the app now, you will still the same html as before.

Edit the file grails-app/conf/spring/resources.groovy and set Spanish ‘es’ as the default locale.

import org.springframework.web.servlet.i18n.SessionLocaleResolver;

// Place your Spring DSL code here
beans = {
    localeResolver(SessionLocaleResolver) { 
        defaultLocale= new java.util.Locale('es'); 
    }
}

grails run-app again and you will see the Spanish welcome message:

Welcome_to_Grails

Do you like to read about Groovy/Grails development? Yes, then Subscribe to Groovy Calamari a weekly curated email newsletter about the Groovy ecosystem which I write 

Leave a Reply

Your email address will not be published. Required fields are marked *