浏览代码

增加可自定义ResourceBundle.Control的构造方法

lsh 1 年之前
父节点
当前提交
f81d1166ee
共有 2 个文件被更改,包括 27 次插入0 次删除
  1. 16 0
      src/main/java/com/jfinal/i18n/I18n.java
  2. 11 0
      src/main/java/com/jfinal/i18n/Res.java

+ 16 - 0
src/main/java/com/jfinal/i18n/I18n.java

@@ -17,7 +17,9 @@
 package com.jfinal.i18n;
 
 import java.util.Locale;
+import java.util.ResourceBundle;
 import java.util.concurrent.ConcurrentHashMap;
+
 import com.jfinal.kit.StrKit;
 
 /**
@@ -76,6 +78,16 @@ public class I18n {
 		return res;
 	}
 	
+	public static Res use(String baseName, String locale, ResourceBundle.Control control) {
+		String resKey = baseName + locale;
+		Res res = resMap.get(resKey);
+		if (res == null) {
+			res = new Res(baseName, locale, control);
+			resMap.put(resKey, res);
+		}
+		return res;
+	}
+	
 	public static Res use(String baseName, Locale locale) {
 		return use(baseName, toLocale(locale));
 	}
@@ -84,6 +96,10 @@ public class I18n {
 		return use(defaultBaseName, locale);
 	}
 	
+	public static Res use(String locale, ResourceBundle.Control control) {
+		return use(defaultBaseName, locale, control);
+	}
+	
 	public static Res use() {
 		return use(defaultBaseName, defaultLocale);
 	}

+ 11 - 0
src/main/java/com/jfinal/i18n/Res.java

@@ -38,6 +38,17 @@ public class Res {
 		this.resourceBundle = ResourceBundle.getBundle(baseName, I18n.toLocale(locale));
 	}
 	
+	public Res(String baseName, String locale, ResourceBundle.Control control) {
+		if (StrKit.isBlank(baseName)) {
+			throw new IllegalArgumentException("baseName can not be blank");
+		}
+		if (StrKit.isBlank(locale)) {
+			throw new IllegalArgumentException("locale can not be blank, the format like this: zh_CN or en_US");
+		}
+		
+		this.resourceBundle = ResourceBundle.getBundle(baseName, I18n.toLocale(locale), control);
+	}
+	
 	/**
 	 * Get the message value from ResourceBundle of the related Locale.
 	 * @param key message key