转载自王宾ID:laiahu 获得资源文件的key和value

package com.util;

import java.util.Enumeration;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
import java.text.MessageFormat;

/**
 * 获得配置资源(默认的为src/properties/message.properties文件)中的key的value信息
 * 也可用Utility.setResourcePath(resourcePathStr)来改变资源的位置
 * @author Administrator
 *
 */

public class Utility {
 
 public static final String module = Utility.class.getName();

 // store resource bundle(默认放在properties文件夹下的message.properties文件中)
 private static String csResourcePath = "properties.message";

 public static String getResourcePath() {
  
  return csResourcePath;
 }

 public static void setResourcePath(String asResourcePath) {
  csResourcePath = asResourcePath;
 }

 /**
  * Gary: get user locale from langcd
  * @param asLangCd:国家语言参数
  * @return
  */
 public static Locale getUserLocale(String asLangCd) {
  Locale lsUserLocale = Locale.ENGLISH;
  if (asLangCd.equals("zht"))
   lsUserLocale = Locale.TRADITIONAL_CHINESE;
  else if (asLangCd.equals("zhs"))
   lsUserLocale = Locale.SIMPLIFIED_CHINESE;
  else if (asLangCd.equals("en"))
   lsUserLocale = Locale.ENGLISH;

  return lsUserLocale;
 }

 public static Locale getUserLocale() {
  //the default locale is SIMPLIFIED_CHINESE
  return Locale.SIMPLIFIED_CHINESE;
 }

 /**
  * Gary: Functions to get message from resource
  * @param asKey
  * @param asLangCd
  * @return
  */
 public static String getMessage(String asKey) {
  return getMessage(asKey, getUserLocale());
 }

 public static String getMessage(String asKey, String asLangCd) {
  return getMessage(asKey, getUserLocale(asLangCd));
 }

 public static String getMessage(String asKey, Locale asLocale) {
  ResourceBundle bundle = ResourceBundle.getBundle(Utility
    .getResourcePath(), asLocale);
  return bundle.getString(asKey);
 }

 public static String getMessage(String asKey, String asLangCd, String arg0,
   String arg1, String arg2) {
  return getMessage(asKey, getUserLocale(asLangCd), arg0, arg1, arg2);
 }

 public static String getMessage(String asKey, Locale asLocale, String arg0,
   String arg1, String arg2) {
  String msg = getMessage(asKey, asLocale);
  String[] messageArgs = new String[3];
  messageArgs[0] = arg0;
  messageArgs[1] = arg1;
  messageArgs[2] = arg2;
  return MessageFormat.format(msg, messageArgs);
 }

 public static boolean isEmpty(String string) {
  boolean isEmpty = true;
  if (string != null && !"".equals(string) && string.trim().length() > 0)
   isEmpty = false;
  return isEmpty;
 }
 
 
 /**
  * 自己的
  * 加载配置文件中的所有键值对,并保存在Properties中
  * @param propertyFileName:属性文件名
  * @return
  * 如getBundlePros("properties/email"); //在src/properties包下的email.properties文件
 
 public Properties getBundleProps(String propertyFileName){
  Properties props = new Properties();
  ResourceBundle bundle =  ResourceBundle.getBundle(propertyFileName.trim());
  Enumeration enume = bundle.getKeys();
  String key = "";
  String value = "";
  while(enume.hasMoreElements()){
   key = (String)enume.nextElement();
   value = bundle.getString(key.toString());
         props.put(key, value);
         }
  return props;
 }
 */
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值