zoukankan      html  css  js  c++  java
  • I18nUtils

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.util.*;
    
    public class I18nUtils {
    
    
        private static final Map<String, Map<String, String>> i18nData = new HashMap<>();
    
    
        public static Map<String, String> getI18nByLangCode(String langCode) {
            langCode = langCode.toLowerCase();
            String fileName = "string_" + langCode + ".properties";
            return i18nData.get(fileName);
        }
    
    
        public static Map<String, String> getI18nMap(String langCode) {
            Map<String, String> m = getI18nByLangCode(langCode);
            if (m == null) {
                m = getI18nByLangCode("en");
            }
            return m;
        }
    
    
        public static void loadI18nResouce() throws Exception {
    
            String path = I18nUtils.class.getResource("/").getPath();
    
            File f = new File(path + "/i18n");
    
            if (f.exists() && f.isDirectory()) {
    
                File[] files = f.listFiles();
    
                if (files != null && files.length > 0) {
    
                    for (File file : files) {
    
                        String fileName = file.getName();
    
                        if (fileName.startsWith("string_") && fileName.endsWith(".properties")) {
    
    
                            InputStream in = new FileInputStream(file);
    
                            Properties properties = new Properties();
    
                            properties.load(in);
    
    
                            Map<String, String> map = toMap(properties);
    
                            i18nData.put(fileName, map);
    
    
                            in.close();
                            properties.clear();
    
                        }
    
                    }
    
                }
    
            }
    
        }
    
    
    
    
    
        private static Map<String, String> toMap(Properties properties) {
    
            Map<String, String> map = new HashMap<>();
    
            Enumeration<?> names = properties.propertyNames();
    
            while (names.hasMoreElements()) {
                Object name = names.nextElement();
    
                if (name != null) {
    
                    String name1 = name.toString();
    
                    String value = properties.getProperty(name1);
    
                    map.put(name1, value);
                }
            }
    
            return map;
    
        }
    
    
    }
    

      

  • 相关阅读:
    致命错误 RC1004: 文件查找结束时有无法预知的错误(vc++)
    demo713总结
    图标,鼠标,字符串,音频..
    不同的色深条件(8、16、24、32),像素绘制方式
    SQL 保留两位小数的实现方式
    MVC4的REmote缺陷
    MVC4安装过程
    mongodb 的几种驱动
    iis7 web配置问题及解决办法
    Fast Binary File Reading with C#
  • 原文地址:https://www.cnblogs.com/lhp2012/p/8393076.html
Copyright © 2011-2022 走看看