通过访问本地资源文件来获取国际化信息

其他java类可通过调用getMessage()方法获取相应的message
package com.holike.lotter.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Component;

/**
 * (初始想法)通过实现MessageSourceAware接口来实现一个msgSrc访问器
 * (目前):通过访问本地资源文件来获取国际化信息;
 * @author liweikun
 *
 */
@Component
public class MsgSrcUtil implements MessageSourceAware {
	protected static MessageSourceAccessor messages;
	private static String MESSAGE_BASENAME = "language/message";
    
	@Override
	public void setMessageSource(MessageSource messageSource) {
		messages = new MessageSourceAccessor(messageSource);
	}

	public static String getMessage(String key, Object[] params, Locale locale) {
		String message = getStringOrNull(doGetBundle(MESSAGE_BASENAME, locale), key);
		return message;
	}

	// 获取资源包的信息
	private static String getStringOrNull(ResourceBundle bundle, String key) {
		if (bundle.containsKey(key)) {
			try {
				return bundle.getString(key);
			} catch (MissingResourceException ex) {
				// Assume key not found for some other reason
				// -> do NOT throw the exception to allow for checking parent
				// message source.
			}
		}
		return null;
	}

	// 获取资源包
	private static ResourceBundle doGetBundle(String basename, Locale locale) {
		return ResourceBundle.getBundle(basename, locale, new MessageSourceControl());
	}

	// 信息资源文件访问
	private static class MessageSourceControl extends ResourceBundle.Control {

		@Override
		public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader,
				boolean reload) throws IllegalAccessException, InstantiationException, IOException {

			// Special handling of default encoding
			if (format.equals("java.properties")) {
				String bundleName = toBundleName(baseName, locale);
				final String resourceName = toResourceName(bundleName, "properties");
				final ClassLoader classLoader = loader;
				final boolean reloadFlag = reload;
				InputStream stream;
				try {
					stream = AccessController.doPrivileged(new PrivilegedExceptionAction
  
  
   
   () {
						@Override
						public InputStream run() throws IOException {
							InputStream is = null;
							if (reloadFlag) {
								URL url = classLoader.getResource(resourceName);
								if (url != null) {
									URLConnection connection = url.openConnection();
									if (connection != null) {
										connection.setUseCaches(false);
										is = connection.getInputStream();
									}
								}
							} else {
								is = classLoader.getResourceAsStream(resourceName);
							}
							return is;
						}
					});
				} catch (PrivilegedActionException ex) {
					throw (IOException) ex.getException();
				}
				if (stream != null) {
					String encoding = Common.DEFAULT_ENCODING;
					if (encoding == null) {
						encoding = "ISO-8859-1";
					}
					try {
						return loadBundle(new InputStreamReader(stream, encoding));
					} finally {
						stream.close();
					}
				} else {
					return null;
				}
			} else {
				// Delegate handling of "java.class" format to standard Control
				return super.newBundle(baseName, locale, format, loader, reload);
			}
		}

		protected ResourceBundle loadBundle(Reader reader) throws IOException {
			return new PropertyResourceBundle(reader);
		}
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值