0

I am currently using LocaleContextHolder.getLocale() to get the locale.

But the issue is, sometimes it returns language-code without country-code.

For e.g. : sometimes en_US, and sometimes en only

What could be the possible reason ?

FYI : Running Environment is : Java11, spring5.3.9.

How can I make sure that the getLocale() method should return lang-code_country-code (en_US) ?

Any help would be greatly appreciated !

Simplifying the problem statement

When using LocaleContextHolder.getLocale() within parallel stream then LocaleContextHolder.getLocale() giving different locale for child thread. For e.g. for main thread en_us and for other threads en.

contacts.parallelStream().forEach(contact -> {
Locale locale =  LocaleContextHolder.getLocale();
//some work....
});

output threads

nio-8080 : en_US, onPool-worker : en, ......, onPool-worker : en

Vikas Gope
  • 29
  • 6
  • It returns what the user has set, so if that is only a language without a country it will return just that. – M. Deinum Oct 05 '21 at 12:46
  • @M.Deinum, has LocaleContextHolder being set up somewhere ? or How can I know what the user has set as locale ? – Vikas Gope Oct 05 '21 at 12:53
  • 1
    What is set as al ocale is transfered to the `LocaleContextHolder`. For this a `LocaleResolver` is used. – M. Deinum Oct 05 '21 at 13:00
  • Thanks @M.Deinum, currently I am using Locale.US as default one. But sometimes It returns en_US and sometimes en only – Vikas Gope Oct 05 '21 at 13:39

0 Answers0