a8b7d2cb69c8cc0e503430ea86871f32a2176b5b.svn-base 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package com.xxl.job.admin.core.util;
  2. import com.xxl.job.admin.core.conf.XxlJobAdminConfig;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.core.io.ClassPathResource;
  6. import org.springframework.core.io.Resource;
  7. import org.springframework.core.io.support.EncodedResource;
  8. import org.springframework.core.io.support.PropertiesLoaderUtils;
  9. import java.io.IOException;
  10. import java.text.MessageFormat;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. import java.util.Properties;
  14. /**
  15. * i18n util
  16. *
  17. * @author xuxueli 2018-01-17 20:39:06
  18. */
  19. public class I18nUtil {
  20. private static Logger logger = LoggerFactory.getLogger(I18nUtil.class);
  21. private static Properties prop = null;
  22. public static Properties loadI18nProp(){
  23. if (prop != null) {
  24. return prop;
  25. }
  26. try {
  27. // build i18n prop
  28. String i18n = XxlJobAdminConfig.getAdminConfig().getI18n();
  29. String i18nFile = MessageFormat.format("i18n/message_{0}.properties", i18n);
  30. // load prop
  31. Resource resource = new ClassPathResource(i18nFile);
  32. EncodedResource encodedResource = new EncodedResource(resource,"UTF-8");
  33. prop = PropertiesLoaderUtils.loadProperties(encodedResource);
  34. } catch (IOException e) {
  35. logger.error(e.getMessage(), e);
  36. }
  37. return prop;
  38. }
  39. /**
  40. * get val of i18n key
  41. *
  42. * @param key
  43. * @return
  44. */
  45. public static String getString(String key) {
  46. return loadI18nProp().getProperty(key);
  47. }
  48. /**
  49. * get mult val of i18n mult key, as json
  50. *
  51. * @param keys
  52. * @return
  53. */
  54. public static String getMultString(String... keys) {
  55. Map<String, String> map = new HashMap<String, String>();
  56. Properties prop = loadI18nProp();
  57. if (keys!=null && keys.length>0) {
  58. for (String key: keys) {
  59. map.put(key, prop.getProperty(key));
  60. }
  61. } else {
  62. for (String key: prop.stringPropertyNames()) {
  63. map.put(key, prop.getProperty(key));
  64. }
  65. }
  66. String json = JacksonUtil.writeValueAsString(map);
  67. return json;
  68. }
  69. }