@Override
public Optional<String> getLanguage(Context context, Optional<Result> result) {
Cookie defaultCookie = generateNinjaLanguageCookie();
// Step 1: Determine language from result.
// Result always has priority over context and will overwrite context.
if (result.isPresent()) {
Cookie cookie = result.get().getCookie(defaultCookie.getName());
if (cookie != null) {
if (cookie.getValue() != null
&& !cookie.getValue().isEmpty()) {
//forced language is:
return Optional.of(cookie.getValue());
}
}
}
// Step 2 => we did not find the language in the result
// We try to determine it from the context.
Cookie cookie = context.getCookie(defaultCookie.getName());
if (cookie != null) {
if (cookie.getValue() != null
&& !cookie.getValue().isEmpty()) {
//forced language is:
return Optional.of(cookie.getValue());
}
}
// Step 3: Determine language from Accept-Language header.