首页 技术 正文
技术 2022年11月15日
0 收藏 870 点赞 4,567 浏览 2386 个字

package yycg.util;

import java.io.Serializable;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Set;

/**
* 资源文件读取工具类
*
*/
public class ResourcesUtil implements Serializable {

private static final long serialVersionUID = -7657898714983901418L;

/**
* 系统语言环境,默认为中文zh
*/
public static final String LANGUAGE = “zh”;

/**
* 系统国家环境,默认为中国CN
*/
public static final String COUNTRY = “CN”;
private static Locale getLocale() {
Locale locale = new Locale(LANGUAGE, COUNTRY);
return locale;
}

/**
* 根据语言、国家、资源文件名和key名字获取资源文件值
*
* @param language
* 语言
*
* @param country
* 国家
*
* @param baseName
* 资源文件名
*
* @param section
* key名字
*
* @return 值
*/
private static String getProperties(String baseName, String section) {
String retValue = “”;
try {
Locale locale = getLocale();
ResourceBundle rb = ResourceBundle.getBundle(baseName, locale);
retValue = (String) rb.getObject(section);
} catch (Exception e) {
e.printStackTrace();
// TODO 添加处理
}
return retValue;
}

/**
* 通过key从资源文件读取内容
*
* @param fileName
* 资源文件名
*
* @param key
* 索引
*
* @return 索引对应的内容
*/
public static String getValue(String fileName, String key) {
String value = getProperties(fileName,key);
return value;
}

public static List<String> gekeyList(String baseName) {
Locale locale = getLocale();
ResourceBundle rb = ResourceBundle.getBundle(baseName, locale);

List<String> reslist = new ArrayList<String>();

Set<String> keyset = rb.keySet();
for (Iterator<String> it = keyset.iterator(); it.hasNext();) {
String lkey = (String)it.next();
reslist.add(lkey);
}

return reslist;

}

/**
* 通过key从资源文件读取内容,并格式化
*
* @param fileName
* 资源文件名
*
* @param key
* 索引
*
* @param objs
* 格式化参数
*
* @return 格式化后的内容
*/
public static String getValue(String fileName, String key, Object[] objs) {
String pattern = getValue(fileName, key);
String value = MessageFormat.format(pattern, objs);
return value;
}

public static void main(String[] args) {
System.out.println(getValue(“resources.messages”, “101”,new Object[]{100,200}));

//根据操作系统环境获取语言环境
/*Locale locale = Locale.getDefault();
System.out.println(locale.getCountry());//输出国家代码
System.out.println(locale.getLanguage());//输出语言代码s

//加载国际化资源(classpath下resources目录下的messages.properties,如果是中文环境会优先找messages_zh_CN.properties)
ResourceBundle rb = ResourceBundle.getBundle(“resources.messages”, locale);
String retValue = rb.getString(“101”);//101是messages.properties文件中的key
System.out.println(retValue);

//信息格式化,如果资源中有{}的参数则需要使用MessageFormat格式化,Object[]为传递的参数,数量根据资源文件中的{}个数决定
String value = MessageFormat.format(retValue, new Object[]{100,200});
System.out.println(value);
*/

}
}

上一篇: python2安装pymongo
下一篇: VUE.JS组件化
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,900