| 123456789101112131415161718192021222324252627282930313233343536 |
- package com.poyee.i18n;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.core.io.Resource;
- import org.springframework.core.io.ResourceLoader;
- import org.springframework.stereotype.Component;
- import org.yaml.snakeyaml.Yaml;
- import java.io.InputStream;
- import java.util.Map;
- /**
- * 多语言 配置文件
- */
- @Component
- public class MessageProperties {
- @Autowired
- private ResourceLoader resourceLoader;
- public Map<String, Map<String, String>> init() {
- return loadMessagesFromYaml();
- }
- public Map<String, Map<String, String>> loadMessagesFromYaml() {
- Resource resource = resourceLoader.getResource("classpath:i18n/message.yml");
- try (InputStream input = resource.getInputStream()) {
- Yaml yaml = new Yaml();
- Map map = yaml.loadAs(input, Map.class);
- return map;
- } catch (Exception e) {
- throw new RuntimeException("Failed to load message.yml", e);
- }
- }
- }
|