⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i18nresourcecache.java.svn-base

📁 EasyJWeb是基于java技术
💻 SVN-BASE
字号:
package com.easyjf.web.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

import com.easyjf.web.Globals;

/**
 * 管理国际化资源缓存
 * 
 * @author stefanie wu
 * 
 */
public class I18NResourceCache {
	/**
	 * 缓存,保存形式为Map<资源模块名,Map<语言,属性对象>>
	 */
	private Map<String, Map<String, Properties>> cache;

	/**
	 * 线程级别的资源模块名
	 */
	private ThreadLocal<String> resourceName;

	/**
	 * 属性文件保存的路径,相对于根路径,默认路径为/WEB-INF/applicationSource/。
	 */
	private String basePath;

	/**
	 * 线程级别的用户本地化信息对象
	 */
	private ThreadLocal<Locale> priLocale;

	public void setLocale(Locale locale) {
		this.priLocale = new ThreadLocal<Locale>();
		priLocale.set(locale);
	}

	public I18NResourceCache(String basePath) {
		this.cache = new HashMap<String, Map<String, Properties>>();
		this.basePath = basePath;
	}

	/**
	 * 根据一个国际化模块的名字和一个语言名字来返回一个确定的属性集对象。
	 * 如果国际化模块的名字为空,则使用保存在当前线程中的默认国际化模块的名字,如果模块的名字不为空,则设置当前线程默认的模块名字为该值。
	 * 如果语言名字为空,则使用默认的语言集(从Action中赋值),如果不为空,则根据该语言返回属性集对象,不改变默认语言集。
	 * 
	 * @param pageName
	 *            国际化模块的名字,如cms模块的所有页面可以使用一个属性文件来保存需要国际化字段的值,而blog模块可以使用一个单独的属性文件来保存需要国际化字段的值。
	 * 
	 * @param localLanguage
	 *            语言名字,该值为Locale.getLanguage()方法返回的值。
	 * @return 返回一个属性集对象。
	 */
	public Properties getPropertyValue(String pageName, String localLanguage) {
		if (pageName != null) {
			this.resourceName = new ThreadLocal<String>();
			resourceName.set(pageName);
		}
		String locale = null;
		if (localLanguage != null) {
			locale = localLanguage;
		}
		if (locale == null) {
			locale = this.priLocale.get().getLanguage();
		}
		Map<String, Properties> resource = null;
		if (resourceName != null) {
			resource = cache.get(resourceName.get());
		}
		if (resource != null) {
			Properties proper = resource.get(locale);
			if (proper != null) {
				return proper;
			} else {
				Properties newBundle = getPropertiesObj(locale);
				resource.put(locale, newBundle);
				return newBundle;
			}
		} else {
			Properties newBundle = getPropertiesObj(locale);
			Map<String, Properties> subMap = new HashMap<String, Properties>();
			subMap.put(locale, newBundle);
			cache.put(pageName, subMap);
			return newBundle;
		}
	}

	private Properties getPropertiesObj(String locale) {
		if("zh".equals(locale)){
			locale+="_CN";
		}
		String fileName = Globals.APP_BASE_DIR + basePath.substring(1)
				+ resourceName.get() + "_" + locale + ".properties";
		File file = new File(fileName);
		if (file != null && file.isFile()) {
			try {
				Properties newBundle = new Properties();
				newBundle.load(new FileInputStream(file));
				return newBundle;
			} catch (IOException e) {
				e.printStackTrace();
				throw new java.lang.IllegalArgumentException();
			}
		}
		return null;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -