These tutorials target Micronaut Framework 3. Read, Guides for Micronaut Framework 4.

Localize your application

Respond to a localized version of your application with LocalizedMessageSource.

Authors: Sergio del Amo

Micronaut Version: 3.9.2

1. Getting Started

In this guide, we will create a Micronaut application written in Groovy.

2. What you will need

To complete this guide, you will need the following:

  • Some time on your hands

  • A decent text editor or IDE

  • JDK 1.8 or greater installed with JAVA_HOME configured appropriately

3. Solution

We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.

4. Writing the Application

Create an application using the Micronaut Command Line Interface or with Micronaut Launch.

mn create-app example.micronaut.micronautguide \
    --features= \
    --build=gradle
    --lang=groovy
If you don’t specify the --build argument, Gradle is used as the build tool.
If you don’t specify the --lang argument, Java is used as the language.

The previous command creates a Micronaut application with the default package example.micronaut in a directory named micronautguide.

If you use Micronaut Launch, select Micronaut Application as application type and add null features.

If you have an existing Micronaut application and want to add the functionality described here, you can view the dependency and configuration changes from the specified features and apply those changes to your application.

4.1. Properties

Create a default messages.properties file:

src/main/resources/i18n/messages.properties
hello.world=Hello World

Create a messages_es.properties file for Spanish locale:

src/main/resources/i18n/messages_es.properties
hello.world=Hola Mundo

4.2. Message Source

Create a MessageSource that uses the previous properties files:

src/main/groovy/example/micronaut/MessageSourceFactory.groovy
package example.micronaut

import groovy.transform.CompileStatic
import io.micronaut.context.MessageSource
import io.micronaut.context.annotation.Factory
import io.micronaut.context.i18n.ResourceBundleMessageSource
import jakarta.inject.Singleton

@CompileStatic
@Factory (1)
class MessageSourceFactory {
    @Singleton (2)
    MessageSource createMessageSource() {
        return new ResourceBundleMessageSource('i18n.messages')
    }
}
1 A class annotated with the @Factory annotated is a factory. It provides one or more methods annotated with a bean scope annotation (e.g. @Singleton). Read more about Bean factories.
2 Use jakarta.inject.Singleton to designate a class as a singleton.

4.3. Controller

src/main/groovy/example/micronaut/HelloWorldController.groovy
package example.micronaut

import groovy.transform.CompileStatic
import io.micronaut.context.LocalizedMessageSource
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Produces

@CompileStatic
@Controller (1)
class HelloWorldController {

    private final LocalizedMessageSource messageSource

    HelloWorldController(LocalizedMessageSource messageSource) { (2)
        this.messageSource = messageSource
    }

    @Produces(MediaType.TEXT_PLAIN) (3)
    @Get (4)
    Optional<String> index() { (5)
        return messageSource.getMessage('hello.world') (6)
    }
}
1 The class is defined as a controller with the @Controller annotation mapped to the path /.
2 Use constructor injection to inject a bean of type LocalizedMessageSource.
3 Set the response content-type to text/plain with the @Produces annotation.
4 LocalizedMessageSource contains methods to fetch a message with a code, a default message, or variables.

4.4. Tests

src/test/groovy/example/micronaut/HelloWorldControllerSpec.groovy
package example.micronaut

import io.micronaut.http.HttpHeaders
import io.micronaut.http.HttpRequest
import io.micronaut.http.client.BlockingHttpClient
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Specification

@MicronautTest (1)
class HelloWorldControllerSpec extends Specification {

    @Inject
    @Client('/') (2)
    HttpClient httpClient

    void useOfLocalizedMessageSource() {
        when:
        BlockingHttpClient client = httpClient.toBlocking()
        HttpRequest<?> request = HttpRequest.GET('/')
                .header(HttpHeaders.ACCEPT_LANGUAGE, 'es') (3)

        then:
        'Hola Mundo' == client.retrieve(request)

        when:
        request = HttpRequest.GET('/')

        then:
        'Hello World' == client.retrieve(request)
    }
}
1 Annotate the class with @MicronautTest so the Micronaut framework will initialize the application context and the embedded server. More info.
2 Inject the HttpClient bean and point it to the embedded server.
3 Creating HTTP Requests is easy thanks to the Micronaut framework fluid API.

5. Testing the Application

To run the tests:

./gradlew test

Then open build/reports/tests/test/index.html in a browser to see the results.

6. Learn More

The application changes the locale with the Accept-Language HTTP Header. Learn more about Locale Resolution.