Interface TranslationFallbackPolicy
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
This policy is intentionally separate from TranslationFailureHandler: fallback policy controls which
locale candidates are attempted, while the failure handler controls the final response after fallback stops or all candidates
are exhausted.
A failure while evaluating a reachable expression-fragment predicate
or interpolating its selected/default fragment is a TranslationFailureReason.RESOLUTION_FAILURE. The safe
default fallbackOnMissingTranslationOrNoMatchingAlternative() stops at that failure;
fallbackOnAnyFailure() may continue to another locale candidate. Expression-selected generated fragments
always have a default translation, so their selection cannot itself produce
TranslationFailureReason.NO_MATCHING_ALTERNATIVE.
Implementations may be invoked concurrently and must be thread-safe when shared by a Strings instance.
- Since:
- 3.0.0
- Author:
- Mark Allen
-
Method Summary
Modifier and TypeMethodDescriptionFalls back after every failed locale attempt, including runtime resolution failures.Falls back for missing translations and unmatched whole-message alternatives, but stops on resolution failures.Never falls back after a locale attempt fails.shouldTryNextLocale(@NonNull TranslationFailureReason reason, @NonNull Locale attemptedLocale, @Nullable Throwable cause) Determines whether resolution should continue with the next locale candidate.
-
Method Details
-
shouldTryNextLocale
@NonNull Boolean shouldTryNextLocale(@NonNull TranslationFailureReason reason, @NonNull Locale attemptedLocale, @Nullable Throwable cause) Determines whether resolution should continue with the next locale candidate.- Parameters:
reason- reason the current locale attempt failed, not nullattemptedLocale- locale whose localized strings were attempted, not nullcause- runtime cause forTranslationFailureReason.RESOLUTION_FAILURE, otherwise null- Returns:
- whether to try the next candidate, not null
-
fallbackOnMissingTranslationOrNoMatchingAlternative
Falls back for missing translations and unmatched whole-message alternatives, but stops on resolution failures.This is the recommended safe default. It stops, for example, when an evaluated expression-fragment predicate is invalid or its selected/default fragment cannot be interpolated.
- Returns:
- the recommended safe default policy, not null
-
fallbackOnAnyFailure
Falls back after every failed locale attempt, including runtime resolution failures.This may continue to another locale after an evaluated expression-fragment predicate or selected/default fragment fails. If no later locale resolves, the configured
TranslationFailureHandlerreceives the resolution failure.This preserves Lokalized 2.x behavior.
- Returns:
- the permissive fallback policy, not null
-
neverFallback
Never falls back after a locale attempt fails.- Returns:
- the fail-fast fallback policy, not null
-