Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -19,7 +19,7 @@ public interface Translation {

void setDefaultLanguage(Language language);

void setFallbackStrategy(Function<String, String> fallbackFunction);
void setFallbackStrategy(FallbackStrategy fallbackStrategy);

void addTranslation(Language language, String key, String value);

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ java {

allprojects {
group = "com.luminiadev.polyglot"
version = "1.0.3-SNAPSHOT"
version = "1.0.4-SNAPSHOT"
}

subprojects {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.
Expand All @@ -27,7 +27,7 @@ public class BaseTranslation implements Translation {
private final Map<Class<? extends TrParameters>, TrParameterFormatter> formatters;

private Language defaultLanguage;
private Function<String, String> fallbackStrategy;
private FallbackStrategy fallbackStrategy;

public BaseTranslation(TranslationContext context, TranslationProvider provider) {
this.context = context;
Expand Down Expand Up @@ -164,7 +164,7 @@ public void setDefaultLanguage(Language language) {
}

@Override
public void setFallbackStrategy(Function<String, String> fallbackStrategy) {
public void setFallbackStrategy(FallbackStrategy fallbackStrategy) {
this.fallbackStrategy = fallbackStrategy;
}

Expand Down
Loading