Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. package fr.hypercraft.mineguns.managers;
  2.  
  3. import fr.hypercraft.mineguns.MinegunsPlugin;
  4. import org.bukkit.ChatColor;
  5.  
  6. import java.io.File;
  7. import java.net.MalformedURLException;
  8. import java.net.URL;
  9. import java.net.URLClassLoader;
  10. import java.text.MessageFormat;
  11. import java.util.Locale;
  12. import java.util.MissingResourceException;
  13. import java.util.ResourceBundle;
  14. import java.util.regex.Pattern;
  15.  
  16. public class MessageManager {
  17. private static final Pattern PATTERN_DBLQUOTES = Pattern.compile("''");
  18. private static ResourceBundle messagesDefault;
  19. private static ResourceBundle messagesSetting;
  20.  
  21. public static void loadLocale(MinegunsPlugin plugin, Locale locale) {
  22. plugin.createDefaultConfiguration(new File(plugin.getDataFolder(), "messages_fr.properties"), "messages_fr.properties");
  23. try {
  24. URL[] urls = {plugin.getDataFolder().toURI().toURL()};
  25. messagesDefault = ResourceBundle.getBundle("messages", Locale.FRENCH);
  26. messagesSetting = ResourceBundle.getBundle("messages", locale, new URLClassLoader(urls));
  27. } catch (MalformedURLException e) {
  28. e.printStackTrace();
  29. } catch (MissingResourceException e) {
  30. plugin.getLogger().severe("Failed to load locale...");
  31. e.printStackTrace();
  32. }
  33. plugin.getLogger().info("Loaded locale");
  34. }
  35.  
  36. public static String msg(String identifier) {
  37. try {
  38. if (messagesSetting != null) {
  39. return ChatColor.translateAlternateColorCodes('&', PATTERN_DBLQUOTES.matcher(messagesSetting.getString(identifier)).replaceAll("'"));
  40. }
  41. if (messagesDefault != null) {
  42. return ChatColor.translateAlternateColorCodes('&', PATTERN_DBLQUOTES.matcher(messagesDefault.getString(identifier)).replaceAll("'"));
  43. }
  44. } catch (Exception e) {
  45. e.printStackTrace();
  46. }
  47. return "{" + identifier + "}";
  48. }
  49.  
  50. public static String msg(String identifier, Object... args) {
  51. try {
  52. if (messagesSetting != null) {
  53. MessageFormat formatter = new MessageFormat(ChatColor.translateAlternateColorCodes('&', messagesSetting.getString(identifier)));
  54. return formatter.format(args);
  55. }
  56. if (messagesDefault != null) {
  57. MessageFormat formatter = new MessageFormat(ChatColor.translateAlternateColorCodes('&', messagesDefault.getString(identifier)));
  58. return formatter.format(args);
  59. }
  60. } catch (Exception e) {
  61. e.printStackTrace();
  62. }
  63. return "{" + identifier + "}:" + args;
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement