|
7 | 7 | import net.kyori.adventure.text.Component;
|
8 | 8 | import net.kyori.adventure.text.TextComponent;
|
9 | 9 | import net.kyori.adventure.text.TranslatableComponent;
|
| 10 | +import net.kyori.adventure.text.TranslationArgument; |
10 | 11 | import net.kyori.adventure.text.format.Style;
|
11 | 12 | import net.kyori.adventure.text.format.TextColor;
|
12 | 13 | import net.kyori.adventure.text.format.TextDecoration;
|
|
15 | 16 | import java.io.InputStream;
|
16 | 17 | import java.io.InputStreamReader;
|
17 | 18 | import java.util.ArrayList;
|
| 19 | +import java.util.Arrays; |
18 | 20 | import java.util.List;
|
19 | 21 | import java.util.Map;
|
20 | 22 |
|
@@ -121,38 +123,52 @@ public static List<Attribute> getFormats(Map<TextDecoration, TextDecoration.Stat
|
121 | 123 | }
|
122 | 124 |
|
123 | 125 | public static String translate(TranslatableComponent message) {
|
124 |
| - if (lang == null) { |
125 |
| - try { |
126 |
| - InputStream resource = Utils.class.getResourceAsStream("/files/lang.json"); |
127 |
| - lang = new JsonParser().parse(new InputStreamReader(resource)).getAsJsonObject(); |
128 |
| - } catch (Exception e) { |
129 |
| - Log.crit(e); |
| 126 | + try { |
| 127 | + if (lang == null) { |
| 128 | + try { |
| 129 | + InputStream resource = Utils.class.getResourceAsStream("/files/lang.json"); |
| 130 | + if (resource == null) { |
| 131 | + throw new Exception("Failed to load lang resource."); |
| 132 | + } |
| 133 | + lang = new JsonParser().parse(new InputStreamReader(resource)).getAsJsonObject(); |
| 134 | + } catch (Exception e) { |
| 135 | + Log.crit(e); |
| 136 | + } |
130 | 137 | }
|
131 |
| - } |
132 |
| - //Log.info(message.toString()); |
| 138 | + //Log.info(message.toString()); |
133 | 139 |
|
134 |
| - String key = message.key(); |
135 |
| - String base = key; |
| 140 | + String key = message.key(); |
| 141 | + String base = key; |
136 | 142 |
|
137 |
| - JsonElement value = lang.get(key); |
138 |
| - if (value != null) { |
139 |
| - base = value.getAsString(); |
140 |
| - } |
| 143 | + JsonElement value = lang.get(key); |
| 144 | + if (value != null) { |
| 145 | + base = value.getAsString(); |
| 146 | + } |
141 | 147 |
|
142 |
| - ArrayList<String> placeholders = new ArrayList<>(); |
| 148 | + ArrayList<String> placeholders = new ArrayList<>(); |
143 | 149 |
|
144 |
| - if (message.args().size() > 0) { |
145 |
| - for (Component component : message.args()) { |
146 |
| - if (component instanceof TranslatableComponent) { |
147 |
| - placeholders.add(translate((TranslatableComponent) component)); |
148 |
| - } else if (component instanceof TextComponent) { |
149 |
| - placeholders.add(getFullText((TextComponent) component, false)); |
| 150 | + if (message.arguments().size() > 0) { |
| 151 | + for (TranslationArgument arg : message.arguments()) { |
| 152 | + Component component = arg.asComponent(); |
| 153 | + if (component instanceof TranslatableComponent) { |
| 154 | + placeholders.add(translate((TranslatableComponent) component)); |
| 155 | + } else if (arg instanceof TextComponent) { |
| 156 | + placeholders.add(getFullText((TextComponent) component, false)); |
| 157 | + } |
150 | 158 | }
|
| 159 | + //Log.info(Arrays.toString(placeholders.toArray())); |
| 160 | + try { |
| 161 | + return String.format(base, placeholders.toArray()); |
| 162 | + } catch (Exception e) { |
| 163 | + Log.error("Error formatting '"+base+"' with placeholders " + Arrays.toString(placeholders.toArray())); |
| 164 | + } |
| 165 | + } else { |
| 166 | + return base; |
151 | 167 | }
|
152 |
| - //Log.info(Arrays.toString(placeholders.toArray())); |
153 |
| - return String.format(base, placeholders.toArray()); |
154 |
| - } else { |
155 |
| - return base; |
| 168 | + } catch (Exception e) { |
| 169 | + Log.error(e); |
156 | 170 | }
|
| 171 | + |
| 172 | + return ""; |
157 | 173 | }
|
158 | 174 | }
|
0 commit comments