Skip to content

Commit

Permalink
Made it better by only defining the translation of the difference
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamesuta committed Feb 7, 2024
1 parent bb85501 commit a2e44e0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
13 changes: 11 additions & 2 deletions src/main/java/com/kamesuta/bungeepteropower/BungeePteroPower.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void onEnable() {
" BungeePteroPower v" + getDescription().getVersion() + " by Kamesuta\n");

// Load messages.yml
fallbackMessages = Messages.loadFromResource("en");
fallbackMessages = Messages.loadFromResource("en", null);
// Load config and translations
reload();

Expand Down Expand Up @@ -89,8 +89,17 @@ public void reload() {
// Load config.yml
config = new Config();

// Try to load messages.yml from resource
// This way, you only need to define the translation of the differences from the default language
Messages resourceMessages;
try {
resourceMessages = Messages.loadFromResource(config.language, fallbackMessages);
} catch (Exception e) {
// This is used when you add a language to the config that the plugin does not support by default
resourceMessages = fallbackMessages;
}
// Load messages.yml
messages = Messages.load(config.language, fallbackMessages);
messages = Messages.load(config.language, resourceMessages);
}

@Override
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/com/kamesuta/bungeepteropower/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ private Messages(Configuration messages, Messages parent) {
* Load messages.yml
*
* @param language Language
* If the language is not found, the parent messages will be used
* @param parent Parent messages
* @return Messages
*/
public static Messages load(String language, Messages parent) {
Expand All @@ -59,18 +61,20 @@ public static Messages load(String language, Messages parent) {
}

/**
* Load fallback messages.yml from resource
* Load messages.yml from resource
*
* @param language Language
* If the language is not found, the parent messages will be used
* @param parent Parent messages
* @return Messages
*/
public static Messages loadFromResource(String language) {
public static Messages loadFromResource(String language, Messages parent) {
try {
// Load messages.yml
try (InputStream in = plugin.getResourceAsStream("messages_" + language + ".yml")) {
Configuration messages = ConfigurationProvider.getProvider(YamlConfiguration.class).load(in);

return new Messages(messages, null);
return new Messages(messages, parent);
}
} catch (IOException e) {
logger.severe("Failed to load fallback messages_" + language + ".yml");
Expand All @@ -86,7 +90,7 @@ public static Messages loadFromResource(String language) {
* @return Translated message
*/
public String getMessage(String key, Object... args) {
String rawMessage = this.messages.getString(key);
String rawMessage = this.messages.getString(key, null);
if (rawMessage != null) {
return String.format(rawMessage, args);
} else if (parent != null) {
Expand Down

0 comments on commit a2e44e0

Please sign in to comment.