Skip to content

Commit

Permalink
BE Crowdin 支持
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Sep 24, 2024
1 parent 6c2a263 commit 3daa75a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@
<targetPath>native</targetPath>
<filtering>false</filtering>
</resource>
<resource>
<directory>crowdin</directory>
<targetPath>crowdin</targetPath>
<filtering>false</filtering>
</resource>
</resources>
</build>

Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/ghostchu/peerbanhelper/text/TextManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public void load() {
languageFilesManager.deploy("en_us", loadBuiltInFallback());
// second, load the bundled language files
loadBundled().forEach(languageFilesManager::deploy);
loadBundledCrowdin().forEach(languageFilesManager::deploy);
// then, load the translations from Crowdin
// and don't forget fix missing
languageFilesManager.fillMissing(loadBuiltInFallback());
Expand Down Expand Up @@ -230,6 +231,34 @@ private Map<String, YamlConfiguration> loadBundled() {
return availableLang;
}

/**
* Loading translations from bundled resources
*
* @return The bundled translations, empty hash map if nothing can be load.
*/
@NotNull
@SneakyThrows
private Map<String, YamlConfiguration> loadBundledCrowdin() {
Map<String, YamlConfiguration> availableLang = new HashMap<>();
URL url = Main.class.getClassLoader().getResource("");
if (url == null) {
return availableLang;
}
PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(Main.class.getClassLoader());
var res = resourcePatternResolver.getResources("classpath:crowdin/**/*.yml");
for (Resource re : res) {
String langName = URLUtil.getParentName(re.getURI());
try {
YamlConfiguration configuration = new YamlConfiguration();
configuration.loadFromString(new String(re.getContentAsByteArray(), StandardCharsets.UTF_8));
availableLang.put(langName.toLowerCase(Locale.ROOT).replace("-", "_"), configuration);
} catch (IOException | InvalidConfigurationException e) {
log.warn("Failed to load bundled Crowdin translation.", e);
}
}
return availableLang;
}

/**
* Generate the override files storage path
*
Expand Down

0 comments on commit 3daa75a

Please sign in to comment.