6.7 Locale Resolution

Micronaut supports several strategies for resolving locales for a given request. The getLocale method is available on the request, however it only supports parsing the Accept-Language header. For other use cases where the locale can be in a cookie, the user’s session, or should be set to a fixed value, the HttpLocaleResolver can be used to determine the current locale.

The LocaleResolver API does not need to be used directly. Simply define a parameter to a controller method of type java.util.Locale and the locale will be resolved and injected automatically.

There are several configuration options to control how the locale should be resolved:

Unresolved directive in <stdin> - include::/home/runner/work/micronaut-core/micronaut-core/build/generated/configurationProperties/io.micronaut.http.server.HttpServerConfiguration$LocaleResolutionConfiguration.adoc[]

Locales can be configured in the “en_GB” format, or in the BCP 47 (Language tag) format. If multiple methods are configured, the fixed locale takes precedence, followed by session/cookie, then header.

If any of the built-in methods do not meet your use case, simply create a bean of type HttpLocaleResolver and set its order (through the getOrder) method relative to the existing resolvers.