Skip to content

Commit 076d192

Browse files
committed
key-value language message substitution. close #11
1 parent 38300ca commit 076d192

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

src/main/java/cat/nyaa/nyaacore/LanguageRepository.java

+58
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.HashMap;
1616
import java.util.IllegalFormatConversionException;
1717
import java.util.Map;
18+
import java.util.TreeMap;
1819

1920
/**
2021
* To be extended by each plugin
@@ -178,7 +179,17 @@ private static void loadLanguageSection(Map<String, String> map, ConfigurationSe
178179
}
179180
}
180181

182+
/**
183+
* @deprecated rename to getFormatted
184+
*/
181185
public String get(@LangKey String key, Object... para) {
186+
return getFormatted(key, para);
187+
}
188+
189+
/**
190+
* Get the language item then format with `para` by {@link String#format(String, Object...)}
191+
*/
192+
public String getFormatted(@LangKey String key, Object... para) {
182193
String val = map.get(key);
183194
if (val == null && key.startsWith("internal.") && internalMap.containsKey(getLanguage())) {
184195
val = internalMap.get(getLanguage()).get(key);
@@ -211,6 +222,53 @@ public String get(@LangKey String key, Object... para) {
211222
}
212223
}
213224

225+
/**
226+
* Called this way: `getSubstituted(languageKey, "key1", value1, "key2", value2, ...)`
227+
* {@link Object#toString()} will be called on non-string objects
228+
* `null` key-value pair will be ignored
229+
*/
230+
public String getSubstituted(@LangKey String key, Object... param) {
231+
if (key == null || param == null || (param.length%2) != 0) throw new IllegalArgumentException();
232+
Map<Object, Object> map = new HashMap<>();
233+
for (int i = 0;i<param.length/2;i++) {
234+
if (param[i*2] != null && param[i*2+1]!=null) {
235+
map.put(param[i*2], param[i*2+1]);
236+
}
237+
}
238+
return getSubstituted(key, map);
239+
}
240+
241+
/**
242+
* Get the language item specified by `key`
243+
* Then substitute all `{{paraName}}` with `paraValue`
244+
* CAVEAT: Replacement occur in arbitrary order.
245+
* @param key language item key
246+
* @param paraMap parameters map, no null key/value allowed
247+
* @return substituted language item
248+
*/
249+
public String getSubstituted(@LangKey String key, Map<?,?> paraMap) {
250+
String val = map.get(key);
251+
if (val == null && key.startsWith("internal.") && internalMap.containsKey(getLanguage())) {
252+
val = internalMap.get(getLanguage()).get(key);
253+
}
254+
if (val == null && key.startsWith("internal.")) {
255+
val = internalMap.get(DEFAULT_LANGUAGE).get(key);
256+
}
257+
if (val == null) {
258+
getPlugin().getLogger().warning("Missing language key: " + key);
259+
StringBuilder keyBuilder = new StringBuilder("MISSING_LANG<" + key + ">");
260+
for (Map.Entry<?,?> e : paraMap.entrySet()) {
261+
keyBuilder.append("#<").append(e.getKey().toString()).append(":").append(e.getValue().toString()).append(">");
262+
}
263+
return keyBuilder.toString();
264+
} else {
265+
for (Map.Entry<?,?> e : paraMap.entrySet()) {
266+
val = val.replace("{{" + e.getKey().toString() + "}}", e.getValue().toString());
267+
}
268+
return val;
269+
}
270+
}
271+
214272
public boolean hasKey(String key) {
215273
if (map.containsKey(key) || internalMap.get(DEFAULT_LANGUAGE).containsKey(key)) return true;
216274
if (internalMap.containsKey(getLanguage()) && internalMap.get(getLanguage()).containsKey(key)) return true;

0 commit comments

Comments
 (0)