{#========================================== Spincast Locale Resolver plugin ==========================================#} {% extends "../../layout.html" %} {% block sectionClasses %}plugins hasBreadCrumb plugins-spincast-locale-resolver{% endblock %} {% block meta_title %}Plugins - Spincast Locale Resolver{% endblock %} {% block meta_description %}Spincast Locale Resolver plugin helps find the Locale/Langage to use.{% endblock %} {% block scripts %} {% endblock %} {% block body %}

Overview

This plugin provides an implementation of the LocaleResolver interface, which is used to find the best locale/language in the code. The implementation, LocaleResolverDefault, try to find the best Locale to use this way :

  • If it is not inside a HTTP request scope, it uses the default Locale. This default Locale is configurable using SpincastConfig#getDefaultLocale().
  • If a Cookie specified what Locale to use, this Locale is returned. The cookie of this cookie is configurable using SpincastConfig#getCookieNameLocale().
  • Use the "Accept-Language" HTTP header of the request to determine the best Locale to use.

Quick Examples

  • You can inject the LocaleResolver component anywhere you need it, but you also have a direct access to the best Locale to use inside a Route Handler :

    public void myHandler(AppRequestContext context) {
        Locale localeToUse = context.getLocaleToUse();
        //...
    }

Installation

If you use the spincast-default artifact, this plugin is already installed so you have nothing more to do!

If you start from scratch using the spincast-core artifact, you can use the plugin by adding this artifact to your project:

<dependency>
    <groupId>org.spincast</groupId>
    <artifactId>spincast-plugins-locale-resolver</artifactId>
    <version>{{spincast.spincastCurrrentVersion}}</version>
</dependency>

You then install the plugin's Guice module, by passing it to the Guice.createInjector(...) method:

Injector guice = Guice.createInjector(
        new SpincastCoreGuiceModule(args),
        new SpincastLocaleResolverPluginGuiceModule(AppRequestContext.class, 
                                                    AppWebsocketContext.class)
        // other modules...
        );

... or by using the install(...) method from your custom Guice module:

public class AppModule extends SpincastCoreGuiceModule {

    @Override
    protected void configure() {
        super.configure();
        install(new SpincastLocaleResolverPluginGuiceModule(getRequestContextType(), 
                                                            getWebsocketContextType()));
        // other modules...
    }
    
    // ...
}

Javadoc

{% endblock %}