Skip to content

Commit

Permalink
Merge pull request lets-blade#193 from ccqy66/dev
Browse files Browse the repository at this point in the history
lets-blade#192 add i18n support
  • Loading branch information
hellokaton authored Jan 10, 2018
2 parents 62c7449 + ca986b3 commit 30e9c0f
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/main/java/com/blade/kit/I18nUtils.java
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);
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/blade/kit/Tuple2.java
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;
}
}
25 changes: 25 additions & 0 deletions src/test/java/com/blade/kit/I18nUtilsTest.java
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");
}
}
1 change: 1 addition & 0 deletions src/test/resources/i18n_en_US.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name=ccqy66
1 change: 1 addition & 0 deletions src/test/resources/i18n_zh_CN.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name=ccqy66

0 comments on commit 30e9c0f

Please sign in to comment.