代码初始化
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package com.vetti.common.international;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 字段国际化
|
||||
*
|
||||
* @author wangxiangshun
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD, ElementType.METHOD})
|
||||
@JacksonAnnotationsInside
|
||||
@JsonSerialize(using = InternationalSerializer.class)
|
||||
public @interface International {
|
||||
|
||||
/**
|
||||
* 所属类型
|
||||
*/
|
||||
String type() default "";
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.vetti.common.international;
|
||||
|
||||
/**
|
||||
* 国际资源获取器
|
||||
*
|
||||
* @author wangxiangshun
|
||||
*/
|
||||
public interface InternationalResourceGetter {
|
||||
|
||||
/**
|
||||
* 获取文本
|
||||
*
|
||||
* @param resourceId
|
||||
* 资源ID
|
||||
* @param lang
|
||||
* 语言
|
||||
* @return 国际化文本
|
||||
*/
|
||||
String getText(String resourceId, String lang);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.vetti.common.international;
|
||||
|
||||
/**
|
||||
* 国际资源设置器
|
||||
*
|
||||
* @author wangxiangshun
|
||||
*/
|
||||
public interface InternationalResourceSetter {
|
||||
|
||||
/**
|
||||
* 创建资源
|
||||
*
|
||||
* @param text
|
||||
* 文本
|
||||
* @param type
|
||||
* 类型
|
||||
* @param lang
|
||||
* 语言
|
||||
* @return 资源ID
|
||||
*/
|
||||
String create(String text, String type, String lang);
|
||||
|
||||
/**
|
||||
* 删除资源
|
||||
*
|
||||
* @param resourceId
|
||||
* 资源ID
|
||||
*/
|
||||
void remove(String resourceId);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.vetti.common.international;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.vetti.common.config.InternationalProperties;
|
||||
import com.vetti.common.utils.spring.SpringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 国际化序列化器
|
||||
*
|
||||
* @author wangxiangshun
|
||||
*/
|
||||
public class InternationalSerializer extends JsonSerializer<String> {
|
||||
|
||||
@Override
|
||||
public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
||||
if (null == value) {
|
||||
gen.writeNull();
|
||||
return;
|
||||
}
|
||||
|
||||
InternationalResourceGetter iTextGetter = SpringUtils.getBean(InternationalResourceGetter.class);
|
||||
InternationalProperties iProperties = SpringUtils.getBean(InternationalProperties.class);
|
||||
if (null == iTextGetter || !iProperties.isEnable()) {
|
||||
gen.writeString(value);
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取客户语言环境
|
||||
String lang = iProperties.getLang();
|
||||
if (!StrUtil.isBlank(lang)) {
|
||||
String itext = iTextGetter.getText(value, lang);
|
||||
if (null == itext) {
|
||||
gen.writeNull();
|
||||
} else {
|
||||
gen.writeString(itext);
|
||||
}
|
||||
} else {
|
||||
gen.writeString(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user