////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
////

By default, the user locale is detected from the incoming `Accept-Language` header. You can provide users the capability to switch locales by simply passing a parameter called `lang` to Grails as a request parameter:

[source,groovy]
----
/book/list?lang=es
----

Grails will automatically switch the user's locale and subsequent requests will use the switched locale.

To offer a language selector, the i18n plugin discovers the locales your application is actually translated into by scanning the classpath for `messages_&#42;.properties` bundles (plus the default locale, configurable via `grails.i18n.default.locale`). This list is exposed as the `AvailableLocaleResolver` bean, ordered by each locale's own name (autonym), and published to the servlet context under the `availableLocales` attribute.

The `<g:localeSelect available="true"/>` tag renders a `<select>` limited to those locales. To build a custom switcher, use the tag's body form: it resolves and orders the locales and flags the active (`loc.active`) and configured-default (`loc.default`) entries, while your markup decides how each is rendered:

[source,xml]
----
<g:localeSelect available="true" pinDefault="true" var="loc">
    <a href="?lang=${loc.tag}"${loc.active ? ' aria-current="page"' : ''}>${loc.autonym}</a>
</g:localeSelect>
----

Alternatively, iterate the published `availableLocales` list directly.

By default every `&#42;.properties` bundle on the classpath is considered, so locales contributed by plugins &mdash; whose bundles are namespaced (for example `spring-security-core_fr.properties`) &mdash; are listed alongside your application's own. Candidate suffixes are validated against the ISO language codes (and the ISO country codes when a country is present), so non-i18n properties files (such as `application.properties`) are ignored. To restrict discovery to your application's own `messages_&#42;.properties` bundles, set `grails.i18n.availableLocales.includePlugins=false`:

[source,yaml]
.grails-app/conf/application.yml
----
grails:
    i18n:
        availableLocales:
            includePlugins: false   # list only the application's own translations
----

Applications created with `grails create-app` include a ready-made Bootstrap language dropdown in the default layout, built with the `g:localeSelect` body form shown above.

By default, Grails uses {springapi}org/springframework/web/servlet/i18n/SessionLocaleResolver.html[SessionLocaleResolver] as the `localeResolver` bean.

You can select a different resolver strategy with the `grails.i18n.localeResolver` configuration property, without declaring a bean:

[source,yaml]
.grails-app/conf/application.yml
----
grails:
    i18n:
        localeResolver: acceptHeader # session (default), cookie, acceptHeader or fixed
----

`session` and `cookie` are mutable, so the `?lang=` switch works with them: the incoming `Accept-Language` header sets the initial locale, and a `?lang=` request parameter then overrides it and is remembered for subsequent requests (in the session or a cookie). `acceptHeader` (the locale follows the incoming `Accept-Language` header on every request) and `fixed` are read-only, so the `?lang=` switch has no effect and is silently ignored — the header, or for `fixed` the `grails.i18n.default.locale` value (falling back to the JVM default locale), always wins.

You can change the default locale easily:

[source,groovy]
.grails-app/conf/spring/resources.groovy
----
import org.springframework.web.servlet.i18n.SessionLocaleResolver

beans = {
    localeResolver(SessionLocaleResolver) {
        defaultLocale= new Locale('es')
    }
}
----

Other `localeResolver` are available. For example, you could use save the switched locale in a Cookie:

[source,groovy]
.grails-app/conf/spring/resources.groovy
----
import org.springframework.web.servlet.i18n.CookieLocaleResolver

beans = {
    localeResolver(CookieLocaleResolver) {
        defaultLocale= new Locale('es')
    }
}
----

Or fix the locale:

[source,groovy]
.grails-app/conf/spring/resources.groovy
----
import org.springframework.web.servlet.i18n.FixedLocaleResolver

beans = {
    localeResolver(FixedLocaleResolver, new Locale('de'))
}
----
