提交 b9f11aad 编写于 作者: O o2sword

多语言国际化3

上级 37004738
allModels=\u6D4B\u8BD5
exception_accessDenied=\u7528\u6237:{} \u6743\u9650\u4E0D\u8DB3.
exception_accessDenied_1=\u7528\u6237:{} \u8BBF\u95EE\u5BF9\u8C61 class:{}, id:{}, \u6743\u9650\u4E0D\u8DB3.
exception_accessDeniedOrEntityNotExist=\u7528\u6237:{} \u6743\u9650\u4E0D\u8DB3\u6216\u8005\u5BF9\u8C61\u4E0D\u5B58\u5728.
exception_accessDeniedOrEntityNotExist_1=\u7528\u6237:{} \u6743\u9650\u4E0D\u8DB3\u6216\u8005\u5BF9\u8C61\u4E0D\u5B58\u5728, {}.
exception_entityNotExist=\u6807\u8BC6\u4E3A:{} \u7684 {} \u5BF9\u8C61\u4E0D\u5B58\u5728.
exception_entityNotExist_1=\u6807\u8BC6\u4E3A:{} \u7684\u5BF9\u8C61\u4E0D\u5B58\u5728.
exception_entityNotExist_2=\u7C7B\u578B\u4E3A: {} \u7684\u5BF9\u8C61\u4E0D\u5B58\u5728.
allModels=test
exception_accessDenied=User :{} Insufficient permissions.
exception_accessDenied_1=User :{} access object class:{}, id:{}, insufficient permission.
exception_accessDeniedOrEntityNotExist=User :{} Insufficient permissions or the object does not exist.
exception_accessDeniedOrEntityNotExist_1=User :{} Insufficient permission or object does not exist, {}.
exception_entityNotExist=The {2} object identified as :{1} does not exist.
exception_entityNotExist_1=The object identified as :{} does not exist.
exception_entityNotExist_2=Object of type: {} does not exist.
allModels=\u6D4B\u8BD5
exception_accessDenied=\u7528\u6237:{} \u6743\u9650\u4E0D\u8DB3.
exception_accessDenied_1=\u7528\u6237:{} \u8BBF\u95EE\u5BF9\u8C61 class:{}, id:{}, \u6743\u9650\u4E0D\u8DB3.
exception_accessDeniedOrEntityNotExist=\u7528\u6237:{} \u6743\u9650\u4E0D\u8DB3\u6216\u8005\u5BF9\u8C61\u4E0D\u5B58\u5728.
exception_accessDeniedOrEntityNotExist_1=\u7528\u6237:{} \u6743\u9650\u4E0D\u8DB3\u6216\u8005\u5BF9\u8C61\u4E0D\u5B58\u5728, {}.
exception_entityNotExist=\u6807\u8BC6\u4E3A:{} \u7684 {} \u5BF9\u8C61\u4E0D\u5B58\u5728.
exception_entityNotExist_1=\u6807\u8BC6\u4E3A:{} \u7684\u5BF9\u8C61\u4E0D\u5B58\u5728.
exception_entityNotExist_2=\u7C7B\u578B\u4E3A: {} \u7684\u5BF9\u8C61\u4E0D\u5B58\u5728.
allModels=\u6E2C\u8A66
exception_accessDenied=\u7528\u6236:{} \u6B0A\u9650\u4E0D\u8DB3.
exception_accessDenied_1=\u7528\u6236:{} \u8A2A\u554F\u5C0D\u8C61 class:{}, id:{}, \u6B0A\u9650\u4E0D\u8DB3.
exception_accessDeniedOrEntityNotExist=\u7528\u6236:{} \u6B0A\u9650\u4E0D\u8DB3\u6216\u8005\u5C0D\u8C61\u4E0D\u5B58\u5728.
exception_accessDeniedOrEntityNotExist_1=\u7528\u6236:{} \u6B0A\u9650\u4E0D\u8DB3\u6216\u8005\u5C0D\u8C61\u4E0D\u5B58\u5728, {}.
exception_entityNotExist=\u6A19\u8B58\u70BA:{} \u7684 {} \u5C0D\u8C61\u4E0D\u5B58\u5728.
exception_entityNotExist_1=\u6A19\u8B58\u70BA:{} \u7684\u5C0D\u8C61\u4E0D\u5B58\u5728.
exception_entityNotExist_2=\u985E\u578B\u70BA: {} \u7684\u5C0D\u8C61\u4E0D\u5B58\u5728.
......@@ -45,6 +45,7 @@ public class Person extends ConfigObject {
public static final String DEFAULT_PASSWORDREGEX = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,}$";
public static final String DEFAULT_PASSWORDREGEXHINT = "6位以上,包含数字和字母.";
public static final String DEFAULT_LANGUAGE = "zh_CN";
public Person() {
this.captchaLogin = DEFAULT_CAPTCHALOGIN;
......@@ -59,6 +60,7 @@ public class Person extends ConfigObject {
this.passwordRegexHint = DEFAULT_PASSWORDREGEXHINT;
this.personUnitOrderByAsc = DEFAULT_PERSONUNITORDERBYASC;
this.tokenCookieHttpOnly = DEFAULT_TOKENCOOKIEHTTPONLY;
this.language = DEFAULT_LANGUAGE;
}
public static Person defaultInstance() {
......@@ -118,6 +120,9 @@ public class Person extends ConfigObject {
@FieldDescribe("人员组织排序是否为升序,true为升序(默认),false为降序")
private Boolean personUnitOrderByAsc;
@FieldDescribe("平台语言:zh_CN(中文,默认)、en(英语)")
private String language;
public Boolean getTokenCookieHttpOnly() {
return BooleanUtils.isTrue(this.tokenCookieHttpOnly);
}
......@@ -319,4 +324,12 @@ public class Person extends ConfigObject {
public void setPersonUnitOrderByAsc(Boolean personUnitOrderByAsc) {
this.personUnitOrderByAsc = personUnitOrderByAsc;
}
}
\ No newline at end of file
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
}
......@@ -142,6 +142,8 @@ public class WebServers extends ConcurrentSkipListMap<String, WebServer> {
/* 密码规则 */
map.put("passwordRegex", Config.person().getPasswordRegex());
map.put("passwordRegexHint", Config.person().getPasswordRegexHint());
/* 平台语言 */
map.put("language", Config.person().getLanguage());
/* RSA */
File publicKeyFile = new File(Config.base(), "config/public.key");
......
......@@ -4,26 +4,27 @@ import java.util.Objects;
import com.x.base.core.entity.JpaObject;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.tools.LanguageTools;
public class ExceptionAccessDenied extends PromptException {
private static final long serialVersionUID = -7354813827434276962L;
public ExceptionAccessDenied(String person) {
super("用户:{} 权限不足.", person);
super(LanguageTools.getValue("exception_accessDenied"), person);
}
public ExceptionAccessDenied(EffectivePerson effectivePerson) {
super("用户:{} 权限不足.", effectivePerson.getDistinguishedName());
super(LanguageTools.getValue("exception_accessDenied"), effectivePerson.getDistinguishedName());
}
public ExceptionAccessDenied(EffectivePerson effectivePerson, JpaObject jpa) {
super("用户:{} 访问对象 class:{}, id:{}, 权限不足.", effectivePerson.getDistinguishedName(),
super(LanguageTools.getValue("exception_accessDenied_1"), effectivePerson.getDistinguishedName(),
(null == jpa) ? null : jpa.getClass().getName(), (null == jpa) ? null : jpa.getId());
}
public ExceptionAccessDenied(EffectivePerson effectivePerson, String message) {
super("用户:{} 权限不足, {}.", effectivePerson.getDistinguishedName(), Objects.toString(message, ""));
super(LanguageTools.getValue("exception_accessDenied"), effectivePerson.getDistinguishedName(), Objects.toString(message, ""));
}
}
......@@ -3,21 +3,22 @@ package com.x.base.core.project.exception;
import java.util.Objects;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.tools.LanguageTools;
public class ExceptionAccessDeniedOrEntityNotExist extends PromptException {
private static final long serialVersionUID = -7354813827434276962L;
public ExceptionAccessDeniedOrEntityNotExist(String person) {
super("用户:{} 权限不足或者对象不存在.", person);
super(LanguageTools.getValue("exception_accessDeniedOrEntityNotExist"), person);
}
public ExceptionAccessDeniedOrEntityNotExist(EffectivePerson effectivePerson) {
super("用户:{} 权限不足或者对象不存在.", effectivePerson.getDistinguishedName());
super(LanguageTools.getValue("exception_accessDeniedOrEntityNotExist"), effectivePerson.getDistinguishedName());
}
public ExceptionAccessDeniedOrEntityNotExist(EffectivePerson effectivePerson, String message) {
super("用户:{} 权限不足或者对象不存在, {}.", effectivePerson.getDistinguishedName(), Objects.toString(message, ""));
super(LanguageTools.getValue("exception_accessDeniedOrEntityNotExist_1"), effectivePerson.getDistinguishedName(), Objects.toString(message, ""));
}
}
......@@ -2,6 +2,7 @@ package com.x.base.core.project.exception;
import java.util.List;
import com.x.base.core.project.tools.LanguageTools;
import org.apache.commons.lang3.StringUtils;
import com.x.base.core.entity.JpaObject;
......@@ -11,28 +12,28 @@ public class ExceptionEntityNotExist extends PromptException {
private static final long serialVersionUID = -7354813827434276962L;
public ExceptionEntityNotExist(String flag, JpaObject jpa) {
super("标识为:{} 的 {} 对象不存在.", flag, (null == jpa) ? null : jpa.nameOfEntity());
super(LanguageTools.getValue("exception_entityNotExist"), flag, (null == jpa) ? null : jpa.nameOfEntity());
}
public <T extends JpaObject> ExceptionEntityNotExist(String flag, Class<T> cls) {
super("标识为:{} 的 {} 对象不存在.", flag, (null == cls) ? null : cls.getSimpleName());
super(LanguageTools.getValue("exception_entityNotExist"), flag, (null == cls) ? null : cls.getSimpleName());
}
public <T extends JpaObject> ExceptionEntityNotExist(List<String> ids, Class<T> cls) {
super("标识为:{} 的 {} 对象不存在.", (null == ids) ? null : StringUtils.join(ids, ","),
super(LanguageTools.getValue("exception_entityNotExist"), (null == ids) ? null : StringUtils.join(ids, ","),
(null == cls) ? null : cls.getSimpleName());
}
public <T extends JpaObject> ExceptionEntityNotExist(String flag, String name) {
super("标识为:{} 的 {} 对象不存在.", flag, name);
super(LanguageTools.getValue("exception_entityNotExist"), flag, name);
}
public <T extends JpaObject> ExceptionEntityNotExist(String flag) {
super("标识为:{} 的对象不存在.", flag);
super(LanguageTools.getValue("exception_entityNotExist_1"), flag);
}
public <T extends JpaObject> ExceptionEntityNotExist(Class<T> cls) {
super("类型为: {} 的对象不存在.", cls.getSimpleName());
super(LanguageTools.getValue("exception_entityNotExist_2"), cls.getSimpleName());
}
}
......@@ -25,6 +25,8 @@ package com.x.base.core.project.logger;
*
*/
import org.apache.commons.lang3.StringUtils;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
......@@ -100,6 +102,7 @@ final public class MessageFormatter {
static final char DELIM_START = '{';
static final char DELIM_STOP = '}';
static final String DELIM_STR = "{}";
static final String DELIM_STR2 = "{%d}";
private static final char ESCAPE_CHAR = '\\';
/**
......@@ -200,7 +203,16 @@ final public class MessageFormatter {
StringBuilder sbuf = new StringBuilder(messagePattern.length() + 50);
int L;
String message = messagePattern;
for (L = 0; L < argArray.length; L++) {
j = messagePattern.indexOf(String.format(DELIM_STR2, L));
if(j > -1){
message = StringUtils.replace(message, String.format(DELIM_STR2, L), (String)argArray[L]);
if(L == argArray.length-1){
return new FormattingTuple(message, argArray, throwable);
}
continue;
}
j = messagePattern.indexOf(DELIM_STR, i);
......
package com.x.base.core.project.tools;
import com.x.base.core.project.config.Config;
import org.apache.commons.lang3.StringUtils;
import java.util.Locale;
......@@ -9,15 +10,21 @@ public class LanguageTools {
private final static String LANGUAGE_PLATFORM = "language.platform";
public static String getValueByKey(String key) {
return getValueByKey(key, null);
public static String getValue(String key) {
return getValue(key, null);
}
public static String getValueByKey(String key, String locale) {
public static String getValue(String key, String locale) {
if(StringUtils.isBlank(key)){
return null;
}
ResourceBundle resourceBundle = null;
if(StringUtils.isBlank(locale)){
try {
locale = Config.person().getLanguage();
} catch (Exception e) {
}
}
if(StringUtils.isBlank(locale)){
resourceBundle = ResourceBundle.getBundle(LANGUAGE_PLATFORM, Locale.getDefault());
}else if("zh".equalsIgnoreCase(locale) || "zh_CN".equalsIgnoreCase(locale)) {
......@@ -26,16 +33,24 @@ public class LanguageTools {
resourceBundle = ResourceBundle.getBundle(LANGUAGE_PLATFORM, Locale.ENGLISH);
}else if("zh_HK".equalsIgnoreCase(locale) || "zh_TW".equalsIgnoreCase(locale)) {
resourceBundle = ResourceBundle.getBundle(LANGUAGE_PLATFORM, Locale.TRADITIONAL_CHINESE);
}else{
resourceBundle = ResourceBundle.getBundle(LANGUAGE_PLATFORM, Locale.getDefault());
}
return resourceBundle.getString(key);
}
public static String getValueByKey(String baseName ,String key, String locale) {
public static String getValue(String baseName ,String key, String locale) {
if(StringUtils.isBlank(key)){
return null;
}
ResourceBundle resourceBundle = null;
if(StringUtils.isBlank(locale)){
try {
locale = Config.person().getLanguage();
} catch (Exception e) {
}
}
if(StringUtils.isBlank(locale)){
resourceBundle = ResourceBundle.getBundle(baseName, Locale.getDefault());
}else if("zh".equalsIgnoreCase(locale) || "zh_CN".equalsIgnoreCase(locale)) {
......@@ -44,6 +59,8 @@ public class LanguageTools {
resourceBundle = ResourceBundle.getBundle(baseName, Locale.ENGLISH);
}else if("zh_HK".equalsIgnoreCase(locale) || "zh_TW".equalsIgnoreCase(locale)) {
resourceBundle = ResourceBundle.getBundle(baseName, Locale.TRADITIONAL_CHINESE);
}else{
resourceBundle = ResourceBundle.getBundle(baseName, Locale.getDefault());
}
return resourceBundle.getString(key);
......
......@@ -47,15 +47,6 @@ class ActionGet extends BaseAction {
private static Logger logger = LoggerFactory.getLogger(ActionGet.class);
ActionResult<Wo> execute(EffectivePerson effectivePerson) throws Exception {
logger.print("===============test1============================");
logger.print(LanguageTools.getValueByKey("allModels", "zh"));
logger.print(LanguageTools.getValueByKey("allModels", "en"));
logger.print("===============test2============================");
try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
Business business = new Business(emc);
ActionResult<Wo> result = new ActionResult<>();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册