简易I18n转换Engine

分三部分:

  1. I18nFiled
  2. 在Entity里面指定i18n字段
  3. 调用convertI18n(xEntity).getI18nShortDesc()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target( { ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface I18nFiled {
String en();

String sc();

String tc();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
public class XEntity implements Serializable {
@Transient
@I18nFiled(en = "shortDescEn", sc = "shortDescSc", tc = "shortDescTc")
private String i18nShortDesc;

public String getI18nShortDesc() {
return i18nShortDesc;
}

public void setI18nShortDesc(String shortDesc) {
i18nShortDesc = shortDesc;
}
}

AUser的默认语言是英文,BUser的默认语言是繁体中文

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
 private static void defaultLanguage(Object i18n, Locale locale,
boolean AUser, I18nFiled i18nFiled, Field f)
throws SecurityException, NoSuchFieldException,
IllegalArgumentException, IllegalAccessException {
log.info("default locale");
if (AUser) {
Field enField = i18n.getClass().getDeclaredField(i18nFiled.en());
enField.setAccessible(true);
String enValue = enField.get(i18n).toString();
f.set(i18n, enValue);
} else {
// BUser
Field tcField = i18n.getClass().getDeclaredField(i18nFiled.tc());
tcField.setAccessible(true);
String tcValue = tcField.get(i18n).toString();
if (StringUtils.isNotEmpty(tcValue)) {
// show english
Field enField = i18n.getClass()
.getDeclaredField(i18nFiled.en());
enField.setAccessible(true);
String enValue = enField.get(i18n).toString();
f.set(i18n, enValue);
}
}
}

private static void convertI18n(Object i18n, Locale locale, boolean AUser) {
log.info("locale: " + locale);
Field[] fields = i18n.getClass().getDeclaredFields();
for (Field f : fields) {
if (f.isAnnotationPresent(I18nFiled.class)) {
f.setAccessible(true);
I18nFiled i18nFiled = f.getAnnotation(I18nFiled.class);
if (Locale.SIMPLIFIED_CHINESE.equals(locale)) {
Field scField = i18n.getClass().getDeclaredField(
i18nFiled.sc());
scField.setAccessible(true);
String scValue = scField.get(i18n).toString();
if (StringUtils.isNotEmpty(scValue)) {
f.set(i18n, scValue);
} else {
defaultLanguage(i18n, locale, AUser, i18nFiled, f);
}
} else if (Locale.TRADITIONAL_CHINESE.equals(locale)) {
Field tcField = i18n.getClass().getDeclaredField(
i18nFiled.tc());
tcField.setAccessible(true);
String tcValue = tcField.get(i18n).toString();
if (StringUtils.isNotEmpty(tcValue)) {
f.set(i18n, tcValue);
} else {
defaultLanguage(i18n, locale, AUser, i18nFiled, f);
}
} else {
defaultLanguage(i18n, locale, AUser, i18nFiled, f);
}
}
}
}

public static <T> T convertI18n(T i18n, PortletRequest request) {
Locale locale = request.getLocale();
User user = User.getCurrentUser(request);
UserInfo info = user.getUserInfo();
if (info instanceof AUserUserInfo) {
convertI18n(i18n, locale, true);
} else if (info instanceof BUserUserInfo) {
convertI18n(i18n, locale, false);
} else if (info instanceof UnknownUserInfo) {
convertI18n(i18n, locale, true);
}
return i18n;
}

public static <T> T convertI18n(T i18n) {
PortletRequest request = (PortletRequest) FacesContext
.getCurrentInstance().getExternalContext().getRequest();
return convertI18n(i18n, request);
}