forked from lets-blade/blade
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request lets-blade#193 from ccqy66/dev
✨ lets-blade#192 add i18n support
- Loading branch information
Showing
5 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.blade.kit; | ||
|
||
import lombok.NoArgsConstructor; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.ResourceBundle; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]" target="_blank">ccqy66</a> | ||
* @Date: 2018/1/8 | ||
* | ||
* file name format:i18n_{language}_{country}.properties | ||
* for example:i18n_zh_CN.properties | ||
* <p>How to use</p> | ||
* i18n_zh_CN.properties: | ||
* name=ccqy66 | ||
* ... | ||
* | ||
* public String getConfig(String key) { | ||
* return I18nUtils.getInstance("i18n_zh_CN.properties").get(key); | ||
* } | ||
* or | ||
* public String getConfig(String key) { | ||
* return I18nUtils.getInstance(new Locale("zh","CN")).get(key); | ||
* } | ||
* | ||
*/ | ||
@NoArgsConstructor | ||
public class I18nUtils { | ||
private static Map<String,ResourceHolder> CACHE = new ConcurrentHashMap<>(); | ||
private static Pattern pattern = Pattern.compile("_"); | ||
|
||
public static synchronized ResourceHolder getInstance(String baseName){ | ||
return createResourceHolder(baseName,null); | ||
} | ||
public static synchronized ResourceHolder getInstance(Locale locale) { | ||
return createResourceHolder(null,locale); | ||
} | ||
private static ResourceHolder createResourceHolder(String baseName,Locale locale) { | ||
Tuple2<String,Locale> localeModel = toLocaleModel(baseName, locale); | ||
ResourceHolder holder = CACHE.get(localeModel._1()); | ||
if (null != holder) { | ||
return holder; | ||
} | ||
holder = new ResourceHolder(ResourceBundle.getBundle(localeModel._1(),localeModel._2())); | ||
CACHE.putIfAbsent(localeModel._1(),holder); | ||
return holder; | ||
} | ||
public static Tuple2<String,Locale> toLocaleModel(String baseName,Locale locale) { | ||
if (StringKit.isBlank(baseName)) { | ||
return new Tuple2<>("i18n_"+locale.getLanguage()+"_"+locale.getCountry(),locale); | ||
}else { | ||
String[] baseNames = pattern.split(baseName); | ||
if (baseNames != null && baseNames.length == 3) { | ||
return new Tuple2<>(baseName,new Locale(baseNames[1],baseNames[2])); | ||
} | ||
throw new IllegalArgumentException("baseName illegal,name format is :i18n_{language}_{country}.properties," + | ||
"for example:i18n_zh_CN.properties"); | ||
} | ||
} | ||
|
||
public static class ResourceHolder { | ||
private ResourceBundle resourceBundle; | ||
public ResourceHolder(ResourceBundle resourceBundle) { | ||
this.resourceBundle = resourceBundle; | ||
} | ||
public String get(String key) { | ||
return resourceBundle.getString(key); | ||
} | ||
public Object getObject(String key) { | ||
return resourceBundle.getObject(key); | ||
} | ||
public boolean containsKey(String key) { | ||
return resourceBundle.containsKey(key); | ||
} | ||
public String format(String key,String ... params) { | ||
return MessageFormat.format(resourceBundle.getString(key),params); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.blade.kit; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]" target="_blank">ccqy66</a> | ||
*/ | ||
public class Tuple2<E,T> { | ||
private E e; | ||
private T t; | ||
|
||
public Tuple2(E e,T t) { | ||
this.e = e; | ||
this.t = t; | ||
} | ||
public E _1() { | ||
return e; | ||
} | ||
public T _2() { | ||
return t; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.blade.kit; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.Locale; | ||
|
||
/** | ||
* Created with IntelliJ IDEA. | ||
* User: chenchen42 | ||
* Date: 2018/1/8 | ||
* Time: 下午12:10 | ||
* To change this template use File | Settings | File Templates. | ||
*/ | ||
public class I18nUtilsTest { | ||
@Test | ||
public void testI18nByKey() { | ||
String name = I18nUtils.getInstance("i18n_en_US").get("name"); | ||
assert name.equals("ccqy66"); | ||
} | ||
@Test | ||
public void testI18nByLocale() { | ||
String name = I18nUtils.getInstance(new Locale("zh","CN")).get("name"); | ||
assert name.equals("ccqy66"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
name=ccqy66 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
name=ccqy66 |