Skip to content

Commit 54ab127

Browse files
committed
Customizable internal language section, close #6
1 parent 87cc204 commit 54ab127

File tree

1 file changed

+38
-42
lines changed

1 file changed

+38
-42
lines changed

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

+38-42
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.bukkit.configuration.file.YamlConfiguration;
77
import org.bukkit.plugin.java.JavaPlugin;
88
import org.librazy.nyaautils_lang_checker.LangKey;
9+
import org.yaml.snakeyaml.Yaml;
910

1011
import java.io.File;
1112
import java.io.IOException;
@@ -69,13 +70,37 @@ public abstract class LanguageRepository {
6970
*/
7071
protected abstract String getLanguage();
7172

73+
private static NyaaCoreLoader corePlugin = null;
74+
75+
// helper function to load language map
76+
private static void loadResourceMap(JavaPlugin plugin, String codeName,
77+
Map<String, String> targetMap, boolean ignoreInternal, boolean ignoreNormal) {
78+
if (plugin == null || codeName == null || targetMap == null) throw new IllegalArgumentException();
79+
InputStream stream = plugin.getResource("lang/" + codeName + ".yml");
80+
if (stream != null) {
81+
YamlConfiguration section = YamlConfiguration.loadConfiguration(new InputStreamReader(stream));
82+
loadLanguageSection(targetMap, section, "", ignoreInternal, ignoreNormal);
83+
}
84+
}
85+
86+
// helper function to load language map
87+
private static void loadLocalMap(JavaPlugin plugin, String codeName,
88+
Map<String, String> targetMap, boolean ignoreInternal, boolean ignoreNormal) {
89+
if (plugin == null || codeName == null || targetMap == null) throw new IllegalArgumentException();
90+
if (Boolean.parseBoolean(System.getProperty("nyaautils.i18n.refreshLangFiles", "false"))) return;
91+
File langFile = new File(plugin.getDataFolder(), codeName + ".yml");
92+
if (langFile.exists() && langFile.isFile()) {
93+
YamlConfiguration section = YamlConfiguration.loadConfiguration(langFile);
94+
loadLanguageSection(targetMap, section, "", ignoreInternal, ignoreNormal);
95+
}
96+
}
97+
7298
/**
7399
* Load the internal map
74100
* should only be called once from {@link NyaaCoreLoader#onLoad()}
75101
*
76102
* @param plugin the NyaaCore plugin
77103
*/
78-
private static NyaaCoreLoader corePlugin = null;
79104
public static void initInternalMap(NyaaCoreLoader plugin) {
80105
if (internalMap.size() != 0 || corePlugin != null) {
81106
plugin.getLogger().warning("Multiple internalMap initiation");
@@ -86,18 +111,10 @@ public static void initInternalMap(NyaaCoreLoader plugin) {
86111
String codeName = lang.codeName;
87112
Map<String, String> map = new HashMap<>();
88113
internalMap.put(codeName, map);
89-
InputStream stream;
90-
YamlConfiguration section;
91-
stream = plugin.getResource("lang/" + DEFAULT_LANGUAGE + ".yml");
92-
if (stream != null) {
93-
section = YamlConfiguration.loadConfiguration(new InputStreamReader(stream));
94-
loadLanguageSection(map, section, "", false, true);
95-
}
96-
stream = plugin.getResource("lang/" + codeName + ".yml");
97-
if (stream != null) {
98-
section = YamlConfiguration.loadConfiguration(new InputStreamReader(stream));
99-
loadLanguageSection(map, section, "", false, true);
100-
}
114+
loadResourceMap(plugin, DEFAULT_LANGUAGE, map, false, true);
115+
loadLocalMap(plugin, DEFAULT_LANGUAGE, map, false, true);
116+
loadResourceMap(plugin, codeName, map, false, true);
117+
loadLocalMap(plugin, codeName, map, false, true);
101118
plugin.getLogger().info(String.format("NyaaCore internalMap loaded: %s", codeName));
102119
}
103120
}
@@ -112,36 +129,15 @@ public void load() {
112129
if (codeName == null) codeName = DEFAULT_LANGUAGE;
113130
map.clear();
114131
// load languages
115-
InputStream stream;
116-
YamlConfiguration section;
117-
stream = corePlugin.getResource("lang/" + DEFAULT_LANGUAGE + ".yml");
118-
if (stream != null) {
119-
section = YamlConfiguration.loadConfiguration(new InputStreamReader(stream));
120-
loadLanguageSection(map, section, "", true, false);
121-
}
122-
stream = corePlugin.getResource("lang/" + codeName + ".yml");
123-
if (stream != null) {
124-
section = YamlConfiguration.loadConfiguration(new InputStreamReader(stream));
125-
loadLanguageSection(map, section, "", true, false);
126-
}
132+
loadResourceMap(corePlugin, DEFAULT_LANGUAGE, map, true, false);
133+
loadLocalMap(corePlugin, DEFAULT_LANGUAGE, map, true, false);
134+
loadResourceMap(corePlugin, codeName, map, true, false);
135+
loadLocalMap(corePlugin, codeName, map, true, false);
127136

128-
stream = plugin.getResource("lang/" + DEFAULT_LANGUAGE + ".yml");
129-
if (stream != null) {
130-
section = YamlConfiguration.loadConfiguration(new InputStreamReader(stream));
131-
loadLanguageSection(map, section, "", false, false);
132-
}
133-
stream = plugin.getResource("lang/" + codeName + ".yml");
134-
if (stream != null) {
135-
section = YamlConfiguration.loadConfiguration(new InputStreamReader(stream));
136-
loadLanguageSection(map, section, "", false, false);
137-
}
138-
139-
if ("false".equals(System.getProperty("nyaautils.i18n.refreshLangFiles", "false"))) { // Do not refresh, so still loading from dataFolder
140-
File localLangFile = new File(plugin.getDataFolder(), codeName + ".yml");
141-
if (localLangFile.exists() && localLangFile.isFile()) {
142-
loadLanguageSection(map, YamlConfiguration.loadConfiguration(localLangFile), "", false, false);
143-
}
144-
}
137+
loadResourceMap(getPlugin(), DEFAULT_LANGUAGE, map, false, false);
138+
loadLocalMap(getPlugin(), DEFAULT_LANGUAGE, map, false, false);
139+
loadResourceMap(getPlugin(), codeName, map, false, false);
140+
loadLocalMap(getPlugin(), codeName, map, false, false);
145141

146142
// save (probably) modified language file back to disk
147143
File localLangFile = new File(plugin.getDataFolder(), codeName + ".yml");

0 commit comments

Comments
 (0)