or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

api-versioning.mdasync.mdconfiguration.mdcontent-negotiation.mdcontroller-annotations.mdcore-framework.mdcors.mddata-binding.mdexception-handling.mdflash-attributes.mdfunctional-endpoints.mdi18n.mdindex.mdinterceptors.mdmultipart.mdrequest-binding.mdresource-handling.mdresponse-handling.mduri-building.mdview-resolution.md
tile.json

i18n.mddocs/

Internationalization

Locale resolution and message source integration for building internationalized applications with support for multiple languages and locales.

Capabilities

LocaleResolver

Strategy interface for resolving the locale of a request.

/**
 * Interface for web-based locale resolution strategies that allows for both locale
 * resolution via the request and locale modification via HTTP request and response.
 */
public interface LocaleResolver {
    /**
     * Resolve the current locale via the given request.
     *
     * @param request the request to resolve the locale for
     * @return the current locale (never null)
     */
    Locale resolveLocale(HttpServletRequest request);

    /**
     * Set the current locale to the given one.
     *
     * @param request the request to be used for locale modification
     * @param response the response to be used for locale modification
     * @param locale the new locale, or null to clear the locale
     */
    void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale);
}

LocaleContextResolver

Extension of LocaleResolver that adds support for a rich locale context.

/**
 * Extension of LocaleResolver, adding support for a rich locale context
 * (potentially including locale and time zone information).
 */
public interface LocaleContextResolver extends LocaleResolver {
    /**
     * Resolve the current locale context via the given request.
     *
     * @param request the request to resolve the locale context for
     * @return the current locale context (never null)
     */
    LocaleContext resolveLocaleContext(HttpServletRequest request);

    /**
     * Set the current locale context to the given one.
     *
     * @param request the request to be used for locale modification
     * @param response the response to be used for locale modification
     * @param localeContext the new locale context, or null to clear the locale context
     */
    void setLocaleContext(HttpServletRequest request, HttpServletResponse response,
                         LocaleContext localeContext);
}

LocaleChangeInterceptor

Interceptor that allows for changing the current locale on every request.

/**
 * Interceptor that allows for changing the current locale on every request,
 * via a configurable request parameter (default parameter name: "locale").
 */
public class LocaleChangeInterceptor extends HandlerInterceptorAdapter {
    /**
     * Default name of the locale specification parameter: "locale".
     */
    public static final String DEFAULT_PARAM_NAME = "locale";

    /**
     * Set the name of the parameter that contains a locale specification
     * in a locale change request. Default is "locale".
     *
     * @param paramName the parameter name
     */
    public void setParamName(String paramName) {}

    /**
     * Return the name of the parameter that contains a locale specification
     * in a locale change request.
     *
     * @return the parameter name
     */
    public String getParamName() {}

    /**
     * Set the HTTP methods for which locale changes are supported.
     * Default is GET, POST, and PUT.
     *
     * @param httpMethods the HTTP method names
     */
    public void setHttpMethods(String... httpMethods) {}

    /**
     * Return the configured HTTP methods.
     *
     * @return the HTTP methods
     */
    public String[] getHttpMethods() {}

    /**
     * Set whether to ignore an invalid value for the locale parameter.
     * Default is false, throwing an exception in case of an invalid locale.
     *
     * @param ignoreInvalidLocale whether to ignore invalid locales
     */
    public void setIgnoreInvalidLocale(boolean ignoreInvalidLocale) {}

    /**
     * Return whether to ignore an invalid value for the locale parameter.
     *
     * @return whether invalid locales are ignored
     */
    public boolean isIgnoreInvalidLocale() {}

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                            Object handler) throws ServletException {}
}

Usage Example:

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver resolver = new SessionLocaleResolver();
        resolver.setDefaultLocale(Locale.US);
        return resolver;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
        interceptor.setParamName("lang");
        return interceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }
}

@Controller
public class HomeController {

    @Autowired
    private MessageSource messageSource;

    @GetMapping("/")
    public String home(Model model, Locale locale) {
        String greeting = messageSource.getMessage("greeting", null, locale);
        model.addAttribute("greeting", greeting);
        return "home";
    }
}

CookieLocaleResolver

LocaleResolver implementation that uses a cookie sent back to the user.

/**
 * LocaleResolver implementation that uses a cookie sent back to the user
 * in case of a custom setting, with a fallback to the specified default locale
 * or the request's accept-header locale.
 */
public class CookieLocaleResolver extends CookieGenerator implements LocaleContextResolver {
    /**
     * The default cookie name used if not explicitly set.
     */
    public static final String DEFAULT_COOKIE_NAME = CookieLocaleResolver.class.getName() + ".LOCALE";

    /**
     * Set a fixed Locale that this resolver will return if no cookie found.
     *
     * @param defaultLocale the default locale
     */
    public void setDefaultLocale(Locale defaultLocale) {}

    /**
     * Return the fixed Locale that this resolver will return if no cookie found, if any.
     *
     * @return the default locale
     */
    public Locale getDefaultLocale() {}

    /**
     * Set a fixed TimeZone that this resolver will return if no cookie found.
     *
     * @param defaultTimeZone the default time zone
     */
    public void setDefaultTimeZone(TimeZone defaultTimeZone) {}

    /**
     * Return the fixed TimeZone that this resolver will return if no cookie found, if any.
     *
     * @return the default time zone
     */
    public TimeZone getDefaultTimeZone() {}

    /**
     * Set whether to reject cookies with invalid content.
     * Default is true.
     *
     * @param rejectInvalidCookies whether to reject invalid cookies
     */
    public void setRejectInvalidCookies(boolean rejectInvalidCookies) {}

    /**
     * Return whether to reject cookies with invalid content.
     *
     * @return whether invalid cookies are rejected
     */
    public boolean isRejectInvalidCookies() {}

    @Override
    public Locale resolveLocale(HttpServletRequest request) {}

    @Override
    public LocaleContext resolveLocaleContext(HttpServletRequest request) {}

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {}

    @Override
    public void setLocaleContext(HttpServletRequest request, HttpServletResponse response,
                                LocaleContext localeContext) {}
}

SessionLocaleResolver

LocaleResolver implementation that uses a locale attribute in the user's session.

/**
 * LocaleResolver implementation that uses a locale attribute in the user's session
 * in case of a custom setting, with a fallback to the specified default locale
 * or the request's accept-header locale.
 */
public class SessionLocaleResolver extends AbstractLocaleContextResolver {
    /**
     * Name of the session attribute that holds the Locale.
     * Only used internally by this implementation.
     */
    public static final String LOCALE_SESSION_ATTRIBUTE_NAME = SessionLocaleResolver.class.getName() + ".LOCALE";

    /**
     * Name of the session attribute that holds the TimeZone.
     * Only used internally by this implementation.
     */
    public static final String TIME_ZONE_SESSION_ATTRIBUTE_NAME = SessionLocaleResolver.class.getName() + ".TIME_ZONE";

    @Override
    public Locale resolveLocale(HttpServletRequest request) {}

    @Override
    public LocaleContext resolveLocaleContext(HttpServletRequest request) {}

    @Override
    public void setLocaleContext(HttpServletRequest request, HttpServletResponse response,
                                LocaleContext localeContext) {}
}

Usage Example:

@Configuration
public class LocaleConfig {

    @Bean
    public LocaleResolver localeResolver() {
        CookieLocaleResolver resolver = new CookieLocaleResolver();
        resolver.setDefaultLocale(Locale.US);
        resolver.setCookieName("user-locale");
        resolver.setCookieMaxAge(3600);
        return resolver;
    }

    // Or use session-based
    @Bean
    public LocaleResolver sessionLocaleResolver() {
        SessionLocaleResolver resolver = new SessionLocaleResolver();
        resolver.setDefaultLocale(Locale.US);
        return resolver;
    }

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource source = new ResourceBundleMessageSource();
        source.setBasenames("messages");
        source.setDefaultEncoding("UTF-8");
        return source;
    }
}

Types

LocaleContext

Interface for determining the current locale context.

public interface LocaleContext {
    Locale getLocale();
}

TimeZoneAwareLocaleContext

Extension of LocaleContext that adds support for time zone information.

public interface TimeZoneAwareLocaleContext extends LocaleContext {
    TimeZone getTimeZone();
}