Advertisement
Exception_Prototype

Untitled

Oct 14th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.65 KB | None | 0 0
  1. public class Locale {
  2.  
  3.     private final JavaPlugin plugin;
  4.     private final HashMap<String, MessageFormat> messageCache = new HashMap<>();
  5.     private final Properties locale = new Properties();
  6.     private File localeFile;
  7.  
  8.     public Locale(JavaPlugin plugin) {
  9.         this.plugin = plugin;
  10.     }
  11.  
  12.     /**
  13.      * Инициализация класса. Должно вызываться в первую очередь. В противном
  14.      * случае вы будете получать Key "key" does not exists!
  15.      */
  16.     public void init() {
  17.         this.locale.clear();
  18.         String loc = this.plugin.getConfig().getString("locale", "ru_RU");
  19.         this.localeFile = new File(this.plugin.getDataFolder(), loc + ".properties");
  20.  
  21.         if (this.saveLocale(loc)) {
  22.             try (FileReader fr = new FileReader(this.localeFile)) {
  23.                 this.locale.load(fr);
  24.             } catch (IOException ex) {
  25.                 Logging.severe("Failed to load " + loc + " locale!", ex);
  26.             }
  27.         } else {
  28.             try {
  29.                 this.locale.load(this.plugin.getResource("ru_RU.properties"));
  30.             } catch (IOException ex) {
  31.                 Logging.severe("Failed to load ru_RU locale!", ex);
  32.             }
  33.         }
  34.     }
  35.  
  36.     /**
  37.      * Получение сообщения из конфигурации Пример сообщения: "There is so many
  38.      * players." Пример вызова: getString("key");
  39.      *
  40.      * @param key ключ сообщения
  41.      * @return сообщение, иначе null
  42.      */
  43.     public String getString(final String key) {
  44.         return this.getString(key, false, "");
  45.     }
  46.  
  47.     /**
  48.      * Получение сообщения с аргументами из конфигурации Пример сообщения:
  49.      * "There is {0} players: {1}." Пример вызова: getString("key", "2", "You,
  50.      * Me");
  51.      *
  52.      * @param key  ключ сообщения
  53.      * @param args аргументы сообщения
  54.      * @return сообщение, иначе null
  55.      */
  56.     public String getString(final String key, final String... args) {
  57.         return this.getString(key, false, args);
  58.     }
  59.  
  60.     /**
  61.      * Получение сообщения из конфигурации с возможностью фильтрации цвета
  62.      * Пример сообщения: "\u00a76There is so many players." Пример вызова:
  63.      * getString("key", false);
  64.      *
  65.      * @param key          ключ сообщения
  66.      * @param removeColors если true, то цвета будут убраны
  67.      * @return сообщение, иначе null
  68.      */
  69.     public String getString(final String key, final boolean removeColors) {
  70.         return this.getString(key, removeColors, "");
  71.     }
  72.  
  73.     /**
  74.      * Получение сообщения с аргументами из конфигурации с возможностью
  75.      * фильтрации цвета Пример сообщения: "\u00a76There is \u00a7c{0}
  76.      * \u00a76players:\u00a7c {1}." Пример вызова: getString("key", false, "2",
  77.      * "You, Me");
  78.      *
  79.      * @param key          ключ сообщения
  80.      * @param removeColors если true, то цвета будут убраны
  81.      * @param args         аргументы сообщения
  82.      * @return сообщение, иначе null
  83.      */
  84.     public String getString(final String key, final boolean removeColors, final String... args) {
  85.         String out = this.locale.getProperty(key);
  86.         if (out == null) {
  87.             return ChatColor.RED + "Key \"" + key + "\" not found!";
  88.         }
  89.  
  90.         MessageFormat mf = this.messageCache.get(out);
  91.         if (mf == null) {
  92.             mf = new MessageFormat(out);
  93.             this.messageCache.put(out, mf);
  94.         }
  95.  
  96.         out = mf.format(args);
  97.  
  98.         if (removeColors) {
  99.             out = ChatColor.stripColor(out);
  100.         }
  101.  
  102.         return CHAT_PREFIX + out;
  103.     }
  104.  
  105.     private boolean saveLocale(final String name) {
  106.         if (this.localeFile.exists()) {
  107.             return true;
  108.         }
  109.  
  110.         InputStream is = this.plugin.getResource(name + ".properties");
  111.         if (is == null) {
  112.             Logging.severe("Locale '" + name + "' does not exists!");
  113.             return false;
  114.         }
  115.  
  116.         try {
  117.             FileUtils.copyInputStreamToFile(is, localeFile);
  118.         } catch (IOException ex) {
  119.             Logging.severe("Failed to save '" + name + ".properties'!", ex);
  120.             return false;
  121.         }
  122.         return true;
  123.     }
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement