Skip to content

Commit

Permalink
Implement multi-linguistic server support.
Browse files Browse the repository at this point in the history
Now server owners can specify different name, description and reward text for each challenge and level via locales file.

Add showcase example.
  • Loading branch information
BONNe committed Sep 20, 2021
1 parent 477766c commit 01e6306
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 48 deletions.
54 changes: 46 additions & 8 deletions src/main/java/world/bentobox/challenges/panel/CommonPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,16 @@ protected List<String> generateChallengeDescription(Challenge challenge, @Nullab

final String reference = Constants.DESCRIPTIONS + "challenge.";

// Get description in single string
String description = Util.translateColorCodes(String.join("\n",
challenge.getDescription()));
// Get description from custom translations
String description = this.user.getTranslationOrNothing(
"challenges.challenges." + challenge.getUniqueId() + ".description");

if (description.isEmpty())
{
// Get data from object in single string.
description = Util.translateColorCodes(String.join("\n", challenge.getDescription()));
}

// Non-memory optimal code used for easier debugging and nicer code layout for my eye :)
// Get status in single string
String status = this.generateChallengeStatus(isCompletedOnce,
Expand Down Expand Up @@ -678,8 +685,16 @@ private String generateRepeatReward(Challenge challenge)
return "";
}

String rewardText = this.user.getTranslationOrNothing(
"challenges.challenges." + challenge.getUniqueId() + ".repeat-reward-text");

if (rewardText.isEmpty())
{
rewardText = Util.translateColorCodes(String.join("\n", challenge.getRepeatRewardText()));
}

return this.user.getTranslationOrNothing(reference + "lore",
"[text]", Util.translateColorCodes(String.join("\n", challenge.getRepeatRewardText())),
"[text]", rewardText,
"[items]", items,
"[experience]", experience,
"[money]", money,
Expand Down Expand Up @@ -771,8 +786,16 @@ private String generateReward(Challenge challenge)
return "";
}

String rewardText = this.user.getTranslationOrNothing(
"challenges.challenges." + challenge.getUniqueId() + ".reward-text");

if (rewardText.isEmpty())
{
rewardText = Util.translateColorCodes(String.join("\n", challenge.getRewardText()));
}

return this.user.getTranslationOrNothing(reference + "lore",
"[text]", Util.translateColorCodes(String.join("\n", challenge.getRewardText())),
"[text]", rewardText,
"[items]", items,
"[experience]", experience,
"[money]", money,
Expand Down Expand Up @@ -834,8 +857,16 @@ protected List<String> generateLevelDescription(LevelStatus levelStatus, User us
// Get rewards in single string
String rewards = !levelStatus.isUnlocked() ? "" : this.generateReward(level);

String description = this.user.getTranslationOrNothing(
"challenges.levels." + level.getUniqueId() + ".description");

if (description.isEmpty())
{
description = Util.translateColorCodes(String.join("\n", level.getUnlockMessage()));
}

String returnString = this.user.getTranslation(reference + "lore",
"[text]", Util.translateColorCodes(levelStatus.getLevel().getUnlockMessage()),
"[text]", description,
"[waiver]", waiver,
"[rewards]", rewards,
"[status]", status);
Expand Down Expand Up @@ -966,16 +997,23 @@ private String generateReward(ChallengeLevel level)
return "";
}

String rewardText = this.user.getTranslationOrNothing(
"challenges.levels." + level.getUniqueId() + ".reward-text");

if (rewardText.isEmpty())
{
rewardText = Util.translateColorCodes(String.join("\n", level.getRewardText()));
}

return this.user.getTranslationOrNothing(reference + "lore",
"[text]", Util.translateColorCodes(String.join("\n", level.getRewardText())),
"[text]", rewardText,
"[items]", items,
"[experience]", experience,
"[money]", money,
"[commands]", commands);
}



// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
Expand Down
105 changes: 65 additions & 40 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1141,46 +1141,71 @@ challenges:
invalid-challenge: "&c Challenge [challenge] contains invalid data. It will not be loaded from database!"
no-library-entries: "&c Cannot find any library entries. Nothing to show."
not-hooked: "&c Challenges Addon could not find any GameMode."
# Showcase for manual material translation
materials:
# Names should be lowercase.
cobblestone: "Cobblestone"
# Also supports descriptions.
stone:
name: "Stone"
description: ""
item-stacks:
# Non-specific item meta translations.
# TYPE is the item type
# META is a content of item meta.
generic: "[type] [meta]"
# Non-specific meta translations. Will replace [meta]
meta:
upgraded: "Upgraded"
extended: "Extended"
potion-meta: "&e [type] [upgraded] [extended]"
# Be aware, enchants are always listed below item in separate line.
enchant-meta: " &7 - &e [type] [level]"
skull-meta: ": &e [player-name]"
book-meta: "&e [title] [author]"
# Custom Enchantment Translation.
enchant:
menting: "Mending"
unbreaking: "Unbreaking"
# Custom Potion Translation.
potion-effect:
water_breathing: "Water Breathing"
# You can also create specific item translations
# Like translate all potions.
potion:
# This will overwrite generic translation.
name: "[type] [upgraded] [extended]"
# Type is either specific translation or potion effect.
water_breathing: "Potion of Water Breathing"
stone_shovel:
# This will mean that only stone shovels will not show
# meta information.
name: "[type]"
# # Showcase for manual material translation
# materials:
# # Names should be lowercase.
# cobblestone: "Cobblestone"
# # Also supports descriptions.
# stone:
# name: "Stone"
# description: ""
# item-stacks:
# # Non-specific item meta translations.
# # TYPE is the item type
# # META is a content of item meta.
# generic: "[type] [meta]"
# # Non-specific meta translations. Will replace [meta]
# meta:
# upgraded: "Upgraded"
# extended: "Extended"
# potion-meta: "&e [type] [upgraded] [extended]"
# # Be aware, enchants are always listed below item in separate line.
# enchant-meta: " &7 - &e [type] [level]"
# skull-meta: ": &e [player-name]"
# book-meta: "&e [title] [author]"
# # Custom Enchantment Translation.
# enchant:
# menting: "Mending"
# unbreaking: "Unbreaking"
# # Custom Potion Translation.
# potion-effect:
# water_breathing: "Water Breathing"
# # You can also create specific item translations
# # Like translate all potions.
# potion:
# # This will overwrite generic translation.
# name: "[type] [upgraded] [extended]"
# # Type is either specific translation or potion effect.
# water_breathing: "Potion of Water Breathing"
# stone_shovel:
# # This will mean that only stone shovels will not show
# # meta information.
# name: "[type]"
#
# # Showcase how to support multi-linguistic challenges
# challenges:
# # Database ID name.
# example_challenge_id:
# name: "&2 Translated Name"
# description: |-
# &7 Translated Custom
# &7 description
# reward-text: |-
# &7 Translated Reward
# &7 text
# repeat-reward-text: |-
# &7 Translated Repeat
# &7 Reward text
# levels:
# # Database ID name.
# example_level_id:
# name: "&2 Translated Name"
# description: |-
# &7 Translated Custom
# &7 description
# reward-text: |-
# &7 Translated Reward
# &7 text
protection:
flags:
CHALLENGES_ISLAND_PROTECTION:
Expand Down

0 comments on commit 01e6306

Please sign in to comment.