diff --git a/api/src/main/java/com/luminiadev/polyglot/api/Translation.java b/api/src/main/java/com/luminiadev/polyglot/api/Translation.java index 56ea5d9..f8778f7 100644 --- a/api/src/main/java/com/luminiadev/polyglot/api/Translation.java +++ b/api/src/main/java/com/luminiadev/polyglot/api/Translation.java @@ -3,9 +3,9 @@ import com.luminiadev.polyglot.api.language.Language; import com.luminiadev.polyglot.api.parameter.TrParameters; import com.luminiadev.polyglot.api.parameter.formatter.TrParameterFormatter; +import com.luminiadev.polyglot.api.util.FallbackStrategy; import java.util.Set; -import java.util.function.Function; public interface Translation { @@ -19,7 +19,7 @@ public interface Translation { void setDefaultLanguage(Language language); - void setFallbackStrategy(Function fallbackFunction); + void setFallbackStrategy(FallbackStrategy fallbackStrategy); void addTranslation(Language language, String key, String value); diff --git a/api/src/main/java/com/luminiadev/polyglot/api/util/FallbackStrategy.java b/api/src/main/java/com/luminiadev/polyglot/api/util/FallbackStrategy.java new file mode 100644 index 0000000..425c9f3 --- /dev/null +++ b/api/src/main/java/com/luminiadev/polyglot/api/util/FallbackStrategy.java @@ -0,0 +1,65 @@ +package com.luminiadev.polyglot.api.util; + +/** + * Fallback strategy interface. Defines how to resolve missing keys. + */ +@FunctionalInterface +public interface FallbackStrategy { + + /** + * Resolves a value for the given key. + * + * @param key the key + * @return resolved value + */ + String get(String key); + + /** + * Returns the key itself. + * + * @return strategy returning the key + */ + static FallbackStrategy keyToKey() { + return key -> key; + } + + /** + * Returns a fixed default value. + * + * @param defaultValue default value + * @return strategy returning the default value + */ + static FallbackStrategy defaultResult(String defaultValue) { + return key -> defaultValue; + } + + /** + * Returns an empty string. + * + * @return strategy returning empty string + */ + static FallbackStrategy emptyResult() { + return key -> ""; + } + + /** + * Returns the key with a prefix. + * + * @param prefix prefix to add + * @return strategy returning prefixed key + */ + static FallbackStrategy prefix(String prefix) { + return key -> prefix + key; + } + + /** + * Returns the key with a suffix. + * + * @param suffix suffix to add + * @return strategy returning suffixed key + */ + static FallbackStrategy suffix(String suffix) { + return key -> key + suffix; + } +} + diff --git a/build.gradle.kts b/build.gradle.kts index db45c7a..6df16e3 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -12,7 +12,7 @@ java { allprojects { group = "com.luminiadev.polyglot" - version = "1.0.3-SNAPSHOT" + version = "1.0.4-SNAPSHOT" } subprojects { diff --git a/core/src/main/java/com/luminiadev/polyglot/core/BaseTranslation.java b/core/src/main/java/com/luminiadev/polyglot/core/BaseTranslation.java index 6f45289..5b0a135 100644 --- a/core/src/main/java/com/luminiadev/polyglot/core/BaseTranslation.java +++ b/core/src/main/java/com/luminiadev/polyglot/core/BaseTranslation.java @@ -1,5 +1,6 @@ package com.luminiadev.polyglot.core; +import com.luminiadev.polyglot.api.util.FallbackStrategy; import com.luminiadev.polyglot.api.Translation; import com.luminiadev.polyglot.api.context.TranslationContext; import com.luminiadev.polyglot.api.language.Language; @@ -15,7 +16,6 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; -import java.util.function.Function; /** * Base implementation of the translation. @@ -27,7 +27,7 @@ public class BaseTranslation implements Translation { private final Map, TrParameterFormatter> formatters; private Language defaultLanguage; - private Function fallbackStrategy; + private FallbackStrategy fallbackStrategy; public BaseTranslation(TranslationContext context, TranslationProvider provider) { this.context = context; @@ -164,7 +164,7 @@ public void setDefaultLanguage(Language language) { } @Override - public void setFallbackStrategy(Function fallbackStrategy) { + public void setFallbackStrategy(FallbackStrategy fallbackStrategy) { this.fallbackStrategy = fallbackStrategy; }