Skip to content

Commit

Permalink
Added params support for translations
Browse files Browse the repository at this point in the history
  • Loading branch information
Shweit committed Sep 4, 2024
1 parent 8970317 commit 0be0089
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ java -Xmx1024M -Xms1024M -jar paper-1.21.jar nogui

## Future Features
- **Custom Poll Durations:** Set start and end times for polls.
- **Multiple Language Support:** Add localization support for multiple languages.
- **Advanced Poll Analytics:** Provide detailed statistics and insights on poll results.

## Contributing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.*;

public final class CreatePollCommand implements CommandExecutor, TabExecutor {
private final Gson gson = new Gson();
Expand Down Expand Up @@ -96,8 +94,13 @@ public boolean onCommand(final CommandSender sender, final Command command, fina

int finalId = id;
Bukkit.getOnlinePlayers().forEach(p -> {
p.sendMessage(ChatColor.GREEN + "A new poll has been created by " + player.getName() + ".");
p.sendMessage(ChatColor.GREEN + "To view the poll, type /vote " + finalId);
Map<String, String> params = new HashMap<>();
params.put("playername", p.getName());
p.sendMessage(ChatColor.GREEN + LangUtil.getTranslation("new_poll_created", params));

params = new HashMap<>();
params.put("pollid", String.valueOf(finalId));
p.sendMessage(ChatColor.GREEN + LangUtil.getTranslation("new_poll_created_subtitle", params));
});

return true;
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/shweit/pollmaster/utils/LangUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,17 @@ public static void loadLanguageFile() {
public static String getTranslation(String key) {
return translations.getOrDefault(key, key); // Fallback to key itself if translation is not found
}

// Retrieve translation with placeholders replaced by values from a Map
public static String getTranslation(String key, Map<String, String> params) {
String message = getTranslation(key);

if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
message = message.replace("${" + entry.getKey() + "}", entry.getValue());
}
}

return message;
}
}
2 changes: 2 additions & 0 deletions src/main/resources/lang/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ vote_multiple_allowed: "Mehrfache Abstimmungen sind erlaubt."
update_available_player_join: "Eine neue Version des PollMaster-Plugins ist verfügbar."
current_version: "Aktuelle Version: "
new_version: "Neue Version: "
new_poll_created: "Eine neue Umfrage wurde von ${playername} erstellt"
new_poll_created_subtitle: "Um diese Umfrage anzusehen, gib /vote ${pollid} ein"
4 changes: 3 additions & 1 deletion src/main/resources/lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ your_vote: "Your vote"
vote_multiple_allowed: "Multiple votes are allowed."
update_available_player_join: "A new version of the PollMaster plugin is available."
current_version: "Current version: "
new_version: "New version: "
new_version: "New version: "
new_poll_created: "A new poll has been created by ${playername}"
new_poll_created_subtitle: "To view this poll, type /vote ${pollid}"

0 comments on commit 0be0089

Please sign in to comment.