-
Notifications
You must be signed in to change notification settings - Fork 2
Translator
The translation API is very simple, it's only a single Translator
interface allowing you to translate text to other languages, using completable futures.
import arc.util.Log;
import com.xpdustry.flex.FlexAPI;
import com.xpdustry.flex.translator.RateLimitedException;
import com.xpdustry.flex.translator.UnsupportedLanguageException;
import mindustry.gen.Player;
import java.util.Locale;
final Player player = /* get player */;
final var text = "Hello";
final var sourceLocale = Locale.ENGLISH;
final var targetLocale = Locale.FRENCH;
FlexAPI.get().getTranslator().translate(text, sourceLocale, targetLocale).whenComplete((result, throwable) -> {
// The translation succeeded, send the result to the player
if (result != null) {
player.sendMessage(result); // Bonjour
return;
}
// The translation failed, if its not one of the expected exceptions from the translator, log it
if (!(throwable instanceof RateLimitedException || throwable instanceof UnsupportedLanguageException)) {
Log.err(throwable);
}
// Send the original text to the player
player.sendMessage(text);
});
The API is simple, but the back-end is very configurable.
The None
translator is the default translator, it will always fail.
"None"
DeepL provides high quality translations and a free tier of 500 000 characters. Ideal for small to medium sized servers.
# Your DeepL API key
deepl-api-key: "xxx"
The basic Google translation API, has 500 000 free characters too and covers way more languages than DeepL.
# Your Google API key
google-basic-api-key: "xxx"
A self-hosted translator, recommended as a fallback. Provides medium quality translations, which can do the job. But has poor language auto detection capabilities.
# Your LibreTranslate API endpoint
lt-endpoint: "https://translation.example.com/"
# Your LibreTranslate API key (optional if you use a instance)
lt-api-key: "xxx"
A wrapper translator caching the results of its inner translator, heavily recommended to use.
# How much time a successful translation should be cached (optional)
success-retention: "10m"
# How much time a failed translation should be cached (optional)
failure-retention: "10s"
# The maximum size of cached translations (optional)
maximum-size: 1000
# The wrapped translator whose results are cached
caching-translator: "..."
A wrapper translator switching between multiple translators, ideal if you want to abuse free tiers 😈. Has also a fallback translator if all primary translators fail.
# The list of sub-translators
translators:
- "..."
- "..."
# The fallback translator (optional)
fallback: "None"
Here the configuration used by chaotic neutral, it uses all the above back-end in a way that maximizes efficiency.
caching-translator:
translators:
- deepl-api-key: "xxx"
- google-basic-api-key: "xxx"
fallback:
lt-endpoint: "https://translate.xpdustry.com/"
lt-api-key: "xxx"