|
| 1 | +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
| 2 | +From: MelnCat <melncatuwu@gmail.com> |
| 3 | +Date: Sat, 1 Oct 2022 17:08:23 -0700 |
| 4 | +Subject: [PATCH] Language API |
| 5 | + |
| 6 | + |
| 7 | +diff --git a/src/main/java/org/purpurmc/purpur/language/Language.java b/src/main/java/org/purpurmc/purpur/language/Language.java |
| 8 | +new file mode 100644 |
| 9 | +index 0000000000000000000000000000000000000000..38483d908ed830e97883733bee2370f87060f4c7 |
| 10 | +--- /dev/null |
| 11 | ++++ b/src/main/java/org/purpurmc/purpur/language/Language.java |
| 12 | +@@ -0,0 +1,60 @@ |
| 13 | ++package org.purpurmc.purpur.language; |
| 14 | ++ |
| 15 | ++import net.kyori.adventure.translation.Translatable; |
| 16 | ++import org.jetbrains.annotations.NotNull; |
| 17 | ++ |
| 18 | ++/** |
| 19 | ++ * Represents a language that can translate translation keys |
| 20 | ++ */ |
| 21 | ++public abstract class Language { |
| 22 | ++ private static Language language; |
| 23 | ++ |
| 24 | ++ /** |
| 25 | ++ * Returns the default language of the server |
| 26 | ++ */ |
| 27 | ++ @NotNull |
| 28 | ++ public static Language getLanguage() { |
| 29 | ++ return language; |
| 30 | ++ } |
| 31 | ++ |
| 32 | ++ public static void setLanguage(@NotNull Language language) { |
| 33 | ++ if (Language.language != null) { |
| 34 | ++ throw new UnsupportedOperationException("Cannot redefine singleton Language"); |
| 35 | ++ } |
| 36 | ++ Language.language = language; |
| 37 | ++ } |
| 38 | ++ |
| 39 | ++ /** |
| 40 | ++ * Checks if a certain translation key is translatable with this language |
| 41 | ++ * @param key The translation key |
| 42 | ++ * @return Whether this language can translate the key |
| 43 | ++ */ |
| 44 | ++ abstract public boolean has(@NotNull String key); |
| 45 | ++ |
| 46 | ++ /** |
| 47 | ++ * Checks if a certain translation key is translatable with this language |
| 48 | ++ * @param key The translation key |
| 49 | ++ * @return Whether this language can translate the key |
| 50 | ++ */ |
| 51 | ++ public boolean has(@NotNull Translatable key) { |
| 52 | ++ return has(key.translationKey()); |
| 53 | ++ } |
| 54 | ++ |
| 55 | ++ /** |
| 56 | ++ * Translates a translation key to this language |
| 57 | ++ * @param key The translation key |
| 58 | ++ * @return The translated key, or the translation key if it couldn't be translated |
| 59 | ++ */ |
| 60 | ++ @NotNull |
| 61 | ++ abstract public String getOrDefault(@NotNull String key); |
| 62 | ++ |
| 63 | ++ /** |
| 64 | ++ * Translates a translation key to this language |
| 65 | ++ * @param key The translation key |
| 66 | ++ * @return The translated key, or the translation key if it couldn't be translated |
| 67 | ++ */ |
| 68 | ++ @NotNull |
| 69 | ++ public String getOrDefault(@NotNull Translatable key) { |
| 70 | ++ return getOrDefault(key.translationKey()); |
| 71 | ++ } |
| 72 | ++} |
0 commit comments