国际化的工具类ognl utils
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);
*/
}
}
国际化的工具类ognl utils的更多相关文章
- easyui DataGrid 工具类之 Utils class
import java.lang.reflect.InvocationTargetException;import java.text.ParseException;import java.text. ...
- JAVA中SpringMVC获取bean方法,在工具类(utils)注入service
有时候我们会出现无法用注解 @Autowired 注入bean的情况,这个时候可以 通过contextLoader获取 WebApplicationContext ctx = ContextLoade ...
- mongoDB工具类以及测试类【java】
java操作mongo工具类 package Utils; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; im ...
- 工具类 ,无需再存localstorage
/** * 工具类 */var Utils = { /** * 获得查询参数 */ getQueryString: function(name) { var search = location.sea ...
- 自定义的操作Cookie的工具类
可以在SpringMVC等环境中使用的操作Cookie的工具类 package utils; import java.io.UnsupportedEncodingException; import j ...
- Java连载71-二分查找和Arrays工具类
一.二分法查找 1.二分法查找是建立在已经排序的基础之上的 2.程序分析是从下到大排序. 3.这个数组中没有重复的元素. package com.bjpowernode.java_learning ...
- Spring @Autowired注解在utils静态工具类
[转] 使用场景:在一个静态方法中,如何使用以下注入: @Autowired private ItemMapper itemMapper; @Component public class TestUt ...
- Spark中常用工具类Utils的简明介绍
<深入理解Spark:核心思想与源码分析>一书前言的内容请看链接<深入理解SPARK:核心思想与源码分析>一书正式出版上市 <深入理解Spark:核心思想与源码分析> ...
- utils部分--一些通用的工具类封装
1.简介 utils部分是对一些常用的工具类进行简单的封装,使用起来比较方便.这里列举常用的一些. 2.ContextUtils使用 主要封装了网络判断.一些方法解释如下: ? 1 2 3 4 5 6 ...
随机推荐
- Oracle 11g 更改字符集
查看字符集: select * from v$nls_parameters where parameter = 'NLS_CHARACTERSET'; 修改字符集: sqlplus "/as ...
- javascript实现掉落弹出层------Day29
顾名思义.所谓"掉落弹出层".就是出现一个弹出层,而出现的位置是从上方向下掉落.掉落到指定的位置停止,这样分析起来.和"右下角弹出提醒对话框"比起来,确有异曲同 ...
- KnockoutJS + My97DatePicker b
如何将Knockoutjs和其他脚本库结合使用?这里给出一个Knockoutjs与my97datepicker配合使用的例子,例子中使用了ko的自定义绑定功能: ko.bindingHandlers. ...
- 配置的好的Apache和PHP语言的环境下,如何在Apache目录下htdocs/html目录下 同时部署两个项目呢
建虚拟目录打开Apache->conf->httpd.conf在最下面粘贴NameVirtualHost 127.0.0.1 <VirtualHost 127.0.0.1> S ...
- VS2010没有Intellisense(智能感知)的解决办法
VS2010没有Intellisense(智能感知)的解决办法 Visual Studio 2010 的Intellisense是依赖于Microsoft SQL Server Compact 3.5 ...
- Android 线性布局(LinearLayout)相关官方文档 - 指南部分
Android 线性布局(LinearLayout)相关官方文档 - 指南部分 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用 ...
- springmvc注解和参数传递
一.SpringMVC注解入门 1. 创建web项目2. 在springmvc的配置文件中指定注解驱动,配置扫描器 <!-- mvc的注解驱动 --> <mvc:annotation ...
- .NET CORE WebApi Post跨域提交
参考博客:https://www.cnblogs.com/chongyao/p/8652743.html 搭建一个 .NET CORE 的 WebApi 想前后端分离, 于是为了简单做了个demo站点 ...
- RAC安装重新运行root.sh
rac1执行root.sh成功,rac2执行失败. 在重新执行root.sh前,在rac2上把crs等配置信息删除: # /u01/app//grid/crs/install/rootcrs.pl - ...
- Python ceil() 函数
描述 ceil(x) 函数返回一个大于或等于 x 的的最小整数(向上取整). 语法 以下是 ceil() 方法的语法: import math math.ceil( x ) 注意:ceil()是不能直 ...