Localize your application

Respond to a localized version of your application with LocalizedMessageSource.

Authors: Sergio del Amo

Micronaut Version: 3.7.0

1. Getting Started

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

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=kotlin
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. Enable annotation Processing

If you use Java or Kotlin and IntelliJ IDEA, make sure to enable annotation processing.


4.2. Properties

Create a default messages.properties file:

hello.world=Hello World

Create a messages_es.properties file for Spanish locale:

hello.world=Hola Mundo

4.3. Message Source

Create a MessageSource that uses the previous properties files:

package example.micronaut

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

@Factory (1)
class MessageSourceFactory {

    @Singleton (2)
    fun createMessageSource(): MessageSource =
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.4. Controller

package example.micronaut

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
import java.util.Optional

@Controller (1)
class HelloWorldController(private val messageSource: LocalizedMessageSource) { (2)

    @Produces(MediaType.TEXT_PLAIN) (3)
    fun index(): Optional<String> { (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.5. Tests

package example.micronaut

import io.micronaut.http.HttpHeaders
import io.micronaut.http.HttpRequest
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import jakarta.inject.Inject
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

@MicronautTest (1)
class HelloWorldControllerTest {

    @field:Client("/") (2)
    lateinit var httpClient: HttpClient

    fun useOfLocalizedMessageSource() {
        val client = httpClient.toBlocking()
        var request: HttpRequest<*>? = HttpRequest.GET<Any>("/")
            .header(HttpHeaders.ACCEPT_LANGUAGE, "es") (3)
        assertEquals("Hola Mundo", client.retrieve(request))
        request = HttpRequest.GET<Any>("/")
        assertEquals("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.