Commit d6edff1a authored by 张永's avatar 张永

双语

parent 9dbe236d
package com.egolm.admin.config;
import java.util.Locale;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
@Configuration
public class I18Config extends WebMvcConfigurerAdapter {
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
// 默认语言
slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
return slr;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
// 参数名
lci.setParamName("LangID");
return lci;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}
......@@ -3,17 +3,22 @@ package com.egolm.admin.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.egolm.admin.pojo.TLinkList;
import com.egolm.admin.service.LinkListService;
import com.egolm.admin.util.AdminContstrant;
import com.egolm.admin.util.I18NUtils;
import com.egolm.common.StringUtil;
import com.egolm.common.bean.Rjx;
import com.egolm.common.jdbc.Page;
......@@ -31,6 +36,8 @@ public class LinkListController {
@Autowired
private LinkListService linkListService;
@ApiOperation("新增")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "linkNO", dataType = "String", required = true, value = "链接编号", defaultValue = ""),
......@@ -51,9 +58,10 @@ public class LinkListController {
String memo = request.getParameter("memo");
String userNO = request.getParameter("userNO");
String tag = request.getParameter("tag");
String LangID = request.getParameter("LangID");
if(!StringUtil.isNotEmpty(linkNO,linkName,linkType,linkPath,userNO,tag)) {
return Rjx.json().setCode(-1).setMessage("参数不能为空").toJson();
if(!StringUtil.isNotEmpty(linkNO,linkName,linkType,linkPath,userNO,tag,LangID)) {
return Rjx.json().setCode(-1).setMessage(I18NUtils.getMessage(LangID, "Msg_Parameter_empty")).toJson();
}
TLinkList tLinkList = new TLinkList();
......@@ -67,8 +75,11 @@ public class LinkListController {
tLinkList.setsUpdateUser(userNO.trim());
Rjx rjx = linkListService.save(tLinkList);
Rjx result = Rjx.json();
result.setCode(rjx.getCode());
result.setMessage(I18NUtils.getMessage(LangID, rjx.get(AdminContstrant.I18n_KEY)+""));
return rjx.toJson();
return result.toJson();
}
......@@ -92,9 +103,10 @@ public class LinkListController {
String memo = request.getParameter("memo");
String userNO = request.getParameter("userNO");
String tag = request.getParameter("tag");
String LangID = request.getParameter("LangID");
if(!StringUtil.isNotEmpty(linkNO,linkName,linkType,linkPath,userNO,tag)) {
return Rjx.json().setCode(-1).setMessage("参数不能为空").toJson();
if(!StringUtil.isNotEmpty(linkNO,linkName,linkType,linkPath,userNO,tag,LangID)) {
return Rjx.json().setCode(-1).setMessage(I18NUtils.getMessage(LangID, "Msg_Parameter_empty")).toJson();
}
TLinkList tLinkList = linkListService.getTLinkListByNO(linkNO);
......@@ -107,9 +119,13 @@ public class LinkListController {
tLinkList.setnTag(Integer.valueOf(tag));
tLinkList.setsUpdateUser(userNO.trim());
Rjx rjx = linkListService.modify(tLinkList);
return rjx.toJson();
Rjx result = Rjx.json();
result.setCode(rjx.getCode());
result.setMessage(I18NUtils.getMessage(LangID, rjx.get(AdminContstrant.I18n_KEY)+""));
return result.toJson();
}else {
return Rjx.jsonErr().setCode(-100).setMessage("数据不存在").toJson();
return Rjx.jsonErr().setCode(-100).setMessage(I18NUtils.getMessage(LangID, "Msg_Data_not_exists")).toJson();
}
}
......@@ -126,7 +142,10 @@ public class LinkListController {
String queryMsg = request.getParameter("queryMsg");
String index = request.getParameter("index");
String limit = request.getParameter("limit");
System.out.println("queryMsg---"+queryMsg);
String LangID = request.getParameter("LangID");
if(!StringUtil.isNotEmpty(index,limit,LangID)) {
return Rjx.json().setCode(-1).setMessage(I18NUtils.getMessage(LangID, "Msg_Parameter_empty")).toJson();
}
Map<String,Object> params = new HashMap<String,Object>();
params.put("queryMsg", queryMsg);
......@@ -137,9 +156,7 @@ public class LinkListController {
page.setLimitKey(" lastUpdateTime desc ");
List<Map<String,Object>> tLinkList = linkListService.query(params, page);
List<Map<String,Object>> tLinkList = linkListService.query(params, page);
return Rjx.jsonOk().set("list", tLinkList).setPage(page).toString();
}
......
......@@ -75,18 +75,22 @@ public class OSSFileController {
*/
@ApiOperation("上传(文件、图片)")
@ApiImplicitParams({
})
@ApiImplicitParam(paramType = "query", name = "LangID", dataType = "String", required = true, value = "语言", defaultValue = "936"),
})
@RequestMapping(value = "/upload",method=RequestMethod.POST) //
public String upload(@RequestParam("file") MultipartFile file,HttpServletRequest request) throws IOException {
String LangID = request.getParameter("LangID");
if(!StringUtil.isNotEmpty(LangID)) {
return Rjx.json().setCode(-1).setMessage(I18NUtils.getMessage(LangID, "Msg_Parameter_empty")).toJson();
}
List<String> codeList = new ArrayList<String>();
codeList.add(AdminContstrant.OSS_UPLOAD_CONFIG_KEY);
codeList.add(AdminContstrant.OSS_UPLOAD_SECRET_KEY);
System.out.println("-----"+StringUtil.join(",",codeList));
List<Map<String,Object>> listCtl = systemCtlService.getTSystemCtrlByCode(codeList);
if(listCtl.isEmpty() || listCtl.size() !=2 ) {
return Rjx.jsonErr().setCode(-100).setMessage("请检查"+StringUtil.join(",",codeList)+"参数是否配置").toJson();
return Rjx.jsonErr().setCode(-100).setMessage(I18NUtils.getMessage(LangID, "Msg_Check_Key_Set",StringUtil.join(",",codeList))).toJson();
}
String ossUrl = ""; //上传的URL
......
......@@ -4,13 +4,15 @@ import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;
import com.egolm.admin.pojo.TLinkList;
import com.egolm.admin.service.LinkListService;
import com.egolm.admin.util.AdminContstrant;
import com.egolm.common.StringUtil;
import com.egolm.common.bean.Rjx;
import com.egolm.common.jdbc.JdbcTemplate;
......@@ -26,6 +28,8 @@ import com.egolm.common.jdbc.Page;
*/
@Service
public class LinkListServiceImpl implements LinkListService {
private static final Log logger = LogFactory.getLog(LinkListServiceImpl.class);
@Autowired
@Qualifier("shopJdbcTemplate")
private JdbcTemplate jdbcTemplate;
......@@ -45,13 +49,13 @@ public class LinkListServiceImpl implements LinkListService {
linkList.setdLastUpdateTime(new Date());
TLinkList link = getTLinkListByNO(linkList.getsLinkNO());
if(link != null) {
return Rjx.jsonErr().setMessage("链接编号已存在");
return Rjx.jsonErr().set(AdminContstrant.I18n_KEY,"Msg_LinkNo_exists").setMessage("链接编号已存在");
}else {
int i = jdbcTemplate.save(linkList);
if(i >0) {
return Rjx.jsonOk().setMessage("保存成功");
return Rjx.jsonOk().set(AdminContstrant.I18n_KEY,"Msg_Save_Success").setMessage("保存成功");
}
return Rjx.jsonErr().setMessage("保存失败");
return Rjx.jsonErr().set(AdminContstrant.I18n_KEY,"Msg_Save_Error").setMessage("保存失败");
}
}
......@@ -70,9 +74,9 @@ public class LinkListServiceImpl implements LinkListService {
int i = jdbcTemplate.update(linkList);
if(i >0) {
return Rjx.jsonOk().setMessage("更新成功");
return Rjx.jsonOk().set(AdminContstrant.I18n_KEY,"Msg_Update_Success").setMessage("更新成功");
}
return Rjx.jsonErr().setMessage("更新失败");
return Rjx.jsonErr().set(AdminContstrant.I18n_KEY,"Msg_Update_Error").setMessage("更新失败");
}
/**
......@@ -88,7 +92,7 @@ public class LinkListServiceImpl implements LinkListService {
String sql = "SELECT * FROM tLinkList WHERE sLinkNO = ? AND nTag&1=0";
return jdbcTemplate.queryForBean(sql, TLinkList.class, linkNO);
} catch (Exception e) {
e.printStackTrace();
logger.info("链接资源不存在 linkNo:"+linkNO);
}
return null;
}
......
......@@ -13,4 +13,6 @@ public class AdminContstrant {
public static final String OSS_UPLOAD_CONFIG_KEY = "OSS_UPLOAD_CONFIG"; //自建OSS上传参数配置
public static final String OSS_UPLOAD_SECRET_KEY = "OSS_UPLOAD_SECRET"; //自建OSS上传密钥,算签名的
public static final String I18n_KEY = "i18nKey"; //service 返回到 controller设置的key
}
package com.egolm.admin.util;
import java.text.MessageFormat;
import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import com.egolm.common.StringUtil;
/**
*
* @ClassName: I18NUtil
* @Description: TODO( 图际化工具类)
* @author 张永
* @date 2018年4月11日 下午5:36:27
*
*/
@Component
public class I18NUtils {
private static MessageSource source;
public static void main(String[] args) {
System.out.println(getMessage("zh", "Msg_check_key_set","AAA"));
}
@Autowired
public void init(MessageSource source) {
I18NUtils.source = source;
}
/**
*
* <p>Title: </p>
* <p>Description: </p>
* @param LangID 语言标识 与IDC中设置 的对应
* @param key 获取的key
* @param values key中对应的值用于替换 里面的 {0} {1}...
* @return
* @author 张永
* @date 2018年4月11日 下午8:35:15
*/
public static String getMessage(String LangID,String key,Object...values){
if(StringUtil.isEmpty(LangID)){ //为空默认为中文
Locale.setDefault(Locale.CHINA);
}else{
if(LangID.equals("900")){ //英文
Locale.setDefault(Locale.ENGLISH);
}else{
Locale.setDefault(Locale.CHINA);
}
}
Locale locale = LocaleContextHolder.getLocale();
System.out.println("locale---------------"+locale);
String msg = source.getMessage(key, null, locale);
return MessageFormat.format(msg, values);
}
}
spring.profiles.active=dev
spring.messages.basename=i18n/messages
\ No newline at end of file
spring.messages.basename=messages
\ No newline at end of file
Msg_Parameter_empty=参数不能为空
Msg_Data_not_exists=数据不存在
Msg_Check_Key_Set=请检查{0}参数是否配置
Msg_LinkNo_exists=链接编号已存在
Msg_Save_Success=保存成功
Msg_Save_Error=保存失败
Msg_Update_Success=更新成功
Msg_Update_Error=更新失败
\ No newline at end of file
Msg_Parameter_empty= Parameters can not be empty
Msg_Data_not_exists= Data does not exist
Msg_Check_Key_Set= Please check whether the {0} parameter is configured
Msg_LinkNo_exists= Link number has already existed
Msg_Save_Success= Save success
Msg_Save_Error= Save failure
Msg_Update_Success= Update success
Msg_Update_Error= Update failure
\ No newline at end of file
Msg_Parameter_empty=\u53C2\u6570\u4E0D\u80FD\u4E3A\u7A7A
Msg_Data_not_exists=\u6570\u636E\u4E0D\u5B58\u5728
Msg_Check_Key_Set=\u8BF7\u68C0\u67E5{0}\u53C2\u6570\u662F\u5426\u914D\u7F6E
Msg_LinkNo_exists=\u94FE\u63A5\u7F16\u53F7\u5DF2\u5B58\u5728
Msg_Save_Success=\u4FDD\u5B58\u6210\u529F
Msg_Save_Error=\u4FDD\u5B58\u5931\u8D25
Msg_Update_Success=\u66F4\u65B0\u6210\u529F
Msg_Update_Error=\u66F4\u65B0\u5931\u8D25
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment