This module allows an application to maintain a custom Locale
in a context that is bound to the current thread,
allowing a configured Locale to be propagated.
- Add it to your classpath.
<dependency> <groupId>nl.talsmasoftware.context.managers</groupId> <artifactId>context-manager-locale</artifactId> <version>[see Maven badge above]</version> </dependency>
- Make sure to use the
ContextAwareExecutorServiceas your threadpool. - Set the current Locale for some block of code:
private void runWithLocale(Locale locale, Runnable someCode) { try (Context ctx = CurrentLocaleHolder.set(locale)) { someCode.run(); } }
- Use the LocaleContext anywhere in your application:
private void someMethod() { Optional<Locale> optionalLocale = CurrentLocaleHolder.get(); Locale locale = CurrentLocaleHolder.getOrDefault(); }