Browse Source

!122 I18n国际化toLocale(Locale locale)方法增加Variant判断
Merge pull request !122 from 大海/master

JFinal 1 year ago
parent
commit
ca710985e9
2 changed files with 30 additions and 0 deletions
  1. 19 0
      src/main/java/com/jfinal/i18n/I18n.java
  2. 11 0
      src/main/java/com/jfinal/i18n/Res.java

+ 19 - 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);
 	}
@@ -103,6 +119,9 @@ public class I18n {
 	}
 	
 	public static String toLocale(Locale locale) {
+		if(locale.getVariant() != null && !locale.getVariant().isEmpty()) {
+			return locale.getLanguage() + "_" + locale.getCountry()+"_"+locale.getVariant();
+		}
 		return locale.getLanguage() + "_" + locale.getCountry();
 	}
 }

+ 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