Commit 1fac5124 authored by 黄毅's avatar 黄毅

双语

parent 05373446
package com.egolm.oms.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());
}
}
......@@ -16,6 +16,7 @@ import com.egolm.common.jdbc.JdbcTemplate;
import com.egolm.common.jdbc.Page;
import com.egolm.common.jdbc.dialect.SqlServerDialect;
import com.egolm.oms.service.OrderService;
import com.egolm.oms.util.I18NUtils;
@Service
public class OrderServiceImpl implements OrderService {
private static final Log logger = LogFactory.getLog(OrderServiceImpl.class);
......@@ -32,6 +33,7 @@ public class OrderServiceImpl implements OrderService {
*/
public String orderCheck(Map<String,Object> params) {
jdbcTemplate.setDialect(new SqlServerDialect());
String LangID = params.get("langID")+"";
try {
Rjx rjx = Rjx.json();
String shopNO = params.get("shopNO")+"";
......@@ -54,11 +56,12 @@ public class OrderServiceImpl implements OrderService {
String errorSubMsg = errorFullMsg.substring(errorFullMsg.lastIndexOf(key)+key.length(), errorFullMsg.length());
System.out.println(errorFullMsg);
System.out.println(errorSubMsg);
return Rjx.jsonErr().setMessage("提交商品异常").set("result", errorSubMsg).toJson();
return Rjx.jsonErr().setMessage(I18NUtils.getMessage(LangID, "Msg_Submit_Goods_error")).set("result", errorSubMsg).toJson();
}catch (Exception e) {
System.out.println("e...."+e.getMessage());
e.printStackTrace();
return Rjx.jsonErr().setMessage("提交商品失败").toJson();
return Rjx.jsonErr().setMessage(I18NUtils.getMessage(LangID, "Msg_Submit_Goods_failure")).toJson();
}
}
......@@ -72,7 +75,8 @@ public class OrderServiceImpl implements OrderService {
* @throws
*/
public String submit(JSONObject submitObj) {
jdbcTemplate.setDialect(new SqlServerDialect());
jdbcTemplate.setDialect(new SqlServerDialect());
String LangID=""+submitObj.get("langID");
try {
String shopNO=""+submitObj.get("shopNO");
String payID=""+submitObj.get("payTypeID");
......@@ -93,12 +97,12 @@ public class OrderServiceImpl implements OrderService {
}else{
errorMsg = (String)commitMap.get(0).get("StatusName");
}
return Rjx.jsonErr().setMessage("订单提交失败").set("errorMsg", errorMsg).toJson();
return Rjx.jsonErr().setMessage(I18NUtils.getMessage(LangID, "Msg_Submit_Order_failure")).set("errorMsg", errorMsg).toJson();
}
} catch (Exception e) {
e.printStackTrace();
return Rjx.jsonErr().setMessage("订单提交异常").toJson();
return Rjx.jsonErr().setMessage(I18NUtils.getMessage(LangID, "Msg_Submit_Order_error")).toJson();
}
}
......
package com.egolm.oms.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
\ No newline at end of file
spring.profiles.active=dev
spring.messages.basename=messages
\ No newline at end of file
Msg_Submit_Goods_error=提交商品异常
Msg_Submit_Goods_failure=提交商品失败
Msg_Submit_Order_failure=订单提交失败
Msg_Submit_Order_error=订单提交异常
\ No newline at end of file
Msg_Submit_Goods_error=Submit goods exception
Msg_Submit_Goods_failure=Submit goods failure
Msg_Submit_Order_failure=Submit order failure
Msg_Submit_Order_error=Submit order exception
Msg_Submit_Goods_error=\u63D0\u4EA4\u5546\u54C1\u5F02\u5E38
Msg_Submit_Goods_failure=\u63D0\u4EA4\u5546\u54C1\u5931\u8D25
Msg_Submit_Order_failure=\u8BA2\u5355\u63D0\u4EA4\u5931\u8D25
Msg_Submit_Order_error=\u8BA2\u5355\u63D0\u4EA4\u5F02\u5E38
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