From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: MelnCat Date: Sat, 1 Oct 2022 17:08:23 -0700 Subject: [PATCH] Language API diff --git a/src/main/java/org/purpurmc/purpur/language/Language.java b/src/main/java/org/purpurmc/purpur/language/Language.java new file mode 100644 index 0000000000000000000000000000000000000000..cbdad4cf09c170064a45644efdf7aa0b28608301 --- /dev/null +++ b/src/main/java/org/purpurmc/purpur/language/Language.java @@ -0,0 +1,60 @@ +package org.purpurmc.purpur.language; + +import net.kyori.adventure.translation.Translatable; +import org.jspecify.annotations.NullMarked; +import org.jspecify.annotations.Nullable; + +/** + * Represents a language that can translate translation keys + */ +@NullMarked +public abstract class Language { + private static @Nullable Language language; + + /** + * Returns the default language of the server + */ + @Nullable + public static Language getLanguage() { + return language; + } + + public static void setLanguage(Language language) { + if (Language.language != null) { + throw new UnsupportedOperationException("Cannot redefine singleton Language"); + } + Language.language = language; + } + + /** + * Checks if a certain translation key is translatable with this language + * @param key The translation key + * @return Whether this language can translate the key + */ + abstract public boolean has(String key); + + /** + * Checks if a certain translation key is translatable with this language + * @param key The translation key + * @return Whether this language can translate the key + */ + public boolean has(Translatable key) { + return has(key.translationKey()); + } + + /** + * Translates a translation key to this language + * @param key The translation key + * @return The translated key, or the translation key if it couldn't be translated + */ + abstract public String getOrDefault(String key); + + /** + * Translates a translation key to this language + * @param key The translation key + * @return The translated key, or the translation key if it couldn't be translated + */ + public String getOrDefault(Translatable key) { + return getOrDefault(key.translationKey()); + } +}