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;
    
        }
    
    
    }
    

      

  • 相关阅读:
    JavaScript中getBoundingClientRect()方法详解
    webpack解惑:require的五种用法 (转)
    大白话讲解Promise(一)
    HTML5触摸事件(touchstart、touchmove和touchend) (转)
    JavaScript 数组中的 indexOf 方法
    arguments
    webpack入坑之旅
    webpack-dev-server
    webpackJsonp is not defined?
    CSS3——背景 文本 字体 链接 列表样式 表格
  • 原文地址:https://www.cnblogs.com/lhp2012/p/8393076.html
Copyright © 2011-2022 走看看