Ask the Question
Hello SDK team,
Internal SAP employee here (if that matters). Our setup is a service using Cloud SDK, CAP, and DWC.
We are trying to extract the current locale, however, as soon as we are in a ResilienceDecorator.executeCallable method, the HttpRequest and its locale is no longer available (which makes sense, I guess).
For the moment, we found a few solutions:
Here, we manually put the locale inside the resiliented function:
@Override
public <T> Callable<T> decorateCallable(Callable<T> callable) {
var locale = LocaleContextHolder.getLocale();
return () -> {
LocaleContextHolder.setLocale(locale);
try {
return callable.call();
} finally {
LogContext.initializeContext();
}
};
}
Alternatively, we could tackle the RequestHeaderAccessor, and then extract the locale from the header:
var acceptLanguageRawString = RequestHeaderAccessor.getHeaderContainer().getHeaderValues("accept-language").stream()
.findFirst()
.orElse(null);
if (StringUtils.isBlank(acceptLanguageRawString)) {
return Locale.getDefault();
}
return Locale.LanguageRange.parse(acceptLanguageRawString).stream()
.sorted((r1, r2) -> Double.compare(r2.getWeight(), r1.getWeight()))
.map(Locale.LanguageRange::getRange)
.map(Locale::forLanguageTag)
.findFirst()
.orElse(Locale.getDefault());
What, do you think, would be the best approach? Do you know even a better way to get the current locale?
Best regards
Marcel
Ask the Question
Hello SDK team,
Internal SAP employee here (if that matters). Our setup is a service using Cloud SDK, CAP, and DWC.
We are trying to extract the current locale, however, as soon as we are in a ResilienceDecorator.executeCallable method, the HttpRequest and its locale is no longer available (which makes sense, I guess).
For the moment, we found a few solutions:
Here, we manually put the locale inside the resiliented function:
Alternatively, we could tackle the RequestHeaderAccessor, and then extract the locale from the header:
What, do you think, would be the best approach? Do you know even a better way to get the current locale?
Best regards
Marcel