MessageProperties.java 1008 B

123456789101112131415161718192021222324252627282930313233343536
  1. package com.poyee.i18n;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.core.io.Resource;
  4. import org.springframework.core.io.ResourceLoader;
  5. import org.springframework.stereotype.Component;
  6. import org.yaml.snakeyaml.Yaml;
  7. import java.io.InputStream;
  8. import java.util.Map;
  9. /**
  10. * 多语言 配置文件
  11. */
  12. @Component
  13. public class MessageProperties {
  14. @Autowired
  15. private ResourceLoader resourceLoader;
  16. public Map<String, Map<String, String>> init() {
  17. return loadMessagesFromYaml();
  18. }
  19. public Map<String, Map<String, String>> loadMessagesFromYaml() {
  20. Resource resource = resourceLoader.getResource("classpath:i18n/message.yml");
  21. try (InputStream input = resource.getInputStream()) {
  22. Yaml yaml = new Yaml();
  23. Map map = yaml.loadAs(input, Map.class);
  24. return map;
  25. } catch (Exception e) {
  26. throw new RuntimeException("Failed to load message.yml", e);
  27. }
  28. }
  29. }