Resources.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package com.boman.system.common;
  2. /**
  3. * @author shiqian
  4. * @description
  5. * @date 2021年03月22日 13:54
  6. **/
  7. import java.util.HashMap;
  8. import java.util.Locale;
  9. import java.util.Map;
  10. import java.util.ResourceBundle;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.context.annotation.PropertySource;
  14. @PropertySource(
  15. value = {"classpath:application.properties", "classpath:config/email.properties", "classpath:config/thirdParty.properties", "classpath:config/ebossCloud.properties", "classpath:i18n/messages*.properties"},
  16. ignoreResourceNotFound = true
  17. )
  18. public final class Resources {
  19. private static final Logger log = LoggerFactory.getLogger(Resources.class);
  20. private static final Map<String, ResourceBundle> MESSAGES = new HashMap();
  21. public Resources() {
  22. }
  23. public static String getMessage(String key, Object... params) {
  24. return getMessage(key, (Locale)null, params);
  25. }
  26. public static String getMessage(String key, Locale plocale, Object... params) {
  27. ResourceBundle pp = ResourceBundle.getBundle("application");
  28. String defaultLocal = pp.getString("spring.locale.default");
  29. // Locale locale = plocale != null ? plocale : Tools.getLocale("zh_CN");
  30. Locale locale = Locale.getDefault();
  31. ResourceBundle message = (ResourceBundle)MESSAGES.get(locale.getLanguage());
  32. if (message == null) {
  33. synchronized(MESSAGES) {
  34. message = (ResourceBundle)MESSAGES.get(locale.getLanguage());
  35. if (message == null) {
  36. message = ResourceBundle.getBundle("i18n/messages", locale);
  37. MESSAGES.put(locale.getLanguage(), message);
  38. }
  39. }
  40. }
  41. try {
  42. return params != null ? String.format(message.getString(key), params) : message.getString(key);
  43. } catch (Exception var10) {
  44. return locale.toString().equals(defaultLocal) && params.length == 1 ? String.valueOf(params[0]) : key;
  45. }
  46. }
  47. public static void flushMessage() {
  48. MESSAGES.clear();
  49. }
  50. }