add 消息国际化
This commit is contained in:
+22
@@ -0,0 +1,22 @@
|
|||||||
|
package com.jero.common.util;
|
||||||
|
|
||||||
|
import cn.hutool.extra.spring.SpringUtil;
|
||||||
|
import org.springframework.context.MessageSource;
|
||||||
|
import org.springframework.context.i18n.LocaleContextHolder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author liJiaRao
|
||||||
|
* @date 2023-08-17 14:07
|
||||||
|
*/
|
||||||
|
public class MessageUtils {
|
||||||
|
/**
|
||||||
|
* 根据消息键和参数 获取消息 委托给spring messageSource
|
||||||
|
* @param code 消息键
|
||||||
|
* @param args 参数
|
||||||
|
* @return 获取国际化翻译值
|
||||||
|
*/
|
||||||
|
public static String message(String code, Object... args) {
|
||||||
|
MessageSource messageSource = SpringUtil.getBean(MessageSource.class);
|
||||||
|
return messageSource.getMessage(code, args, LocaleContextHolder.getLocale());
|
||||||
|
}
|
||||||
|
}
|
||||||
+36
@@ -0,0 +1,36 @@
|
|||||||
|
package com.jero.config;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.web.servlet.LocaleResolver;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author liJiaRao
|
||||||
|
* @date 2023-08-17 14:30
|
||||||
|
*/
|
||||||
|
public class MyLocaleResolver implements LocaleResolver {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Locale resolveLocale(HttpServletRequest httpServletRequest) {
|
||||||
|
String l = httpServletRequest.getHeader("l");
|
||||||
|
Locale locale = Locale.CHINESE;
|
||||||
|
if (StringUtils.isNotBlank(l)){
|
||||||
|
locale = new Locale(l);
|
||||||
|
}
|
||||||
|
return locale;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public LocaleResolver localeResolver(){
|
||||||
|
return new MyLocaleResolver();
|
||||||
|
}
|
||||||
|
}
|
||||||
+14
@@ -12,11 +12,16 @@ import org.springframework.http.converter.json.MappingJackson2HttpMessageConvert
|
|||||||
import org.springframework.web.cors.CorsConfiguration;
|
import org.springframework.web.cors.CorsConfiguration;
|
||||||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||||
import org.springframework.web.filter.CorsFilter;
|
import org.springframework.web.filter.CorsFilter;
|
||||||
|
import org.springframework.web.servlet.LocaleResolver;
|
||||||
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
|
||||||
|
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Spring Boot 2.0 解决跨域问题
|
* Spring Boot 2.0 解决跨域问题
|
||||||
@@ -68,4 +73,13 @@ public class WebMvcConfiguration implements WebMvcConfigurer {
|
|||||||
jackson2HttpMessageConverter.setObjectMapper(objectMapper);
|
jackson2HttpMessageConverter.setObjectMapper(objectMapper);
|
||||||
converters.add(jackson2HttpMessageConverter);
|
converters.add(jackson2HttpMessageConverter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 国际化配置
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public LocaleResolver localeResolver(){
|
||||||
|
return new MyLocaleResolver();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-1
@@ -2,6 +2,7 @@ package com.jero.modules.activiti.controller;
|
|||||||
|
|
||||||
import cn.hutool.core.bean.BeanUtil;
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
import com.jero.common.api.vo.Result;
|
import com.jero.common.api.vo.Result;
|
||||||
|
import com.jero.common.util.MessageUtils;
|
||||||
import com.jero.modules.activiti.service.ProcessService;
|
import com.jero.modules.activiti.service.ProcessService;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import io.swagger.annotations.ApiParam;
|
import io.swagger.annotations.ApiParam;
|
||||||
@@ -18,7 +19,9 @@ import org.activiti.engine.task.TaskQuery;
|
|||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.util.AntPathMatcher;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
|
import org.springframework.util.PathMatcher;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
@@ -214,5 +217,10 @@ public class AController {
|
|||||||
log.error("导出model的bpmn文件失败:modelId={}", modelId, e);
|
log.error("导出model的bpmn文件失败:modelId={}", modelId, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@GetMapping("test")
|
||||||
|
public Result<String> getRedisTest(){
|
||||||
|
String msg1 = MessageUtils.message("message");
|
||||||
|
String msg2 = MessageUtils.message("message.operate.error","123456");
|
||||||
|
return Result.OK(msg1+"\n"+msg2) ;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ spring:
|
|||||||
multipart:
|
multipart:
|
||||||
max-file-size: 10MB
|
max-file-size: 10MB
|
||||||
max-request-size: 10MB
|
max-request-size: 10MB
|
||||||
|
messages:
|
||||||
|
basename: i18n/messages
|
||||||
mail:
|
mail:
|
||||||
host: smtp.163.com
|
host: smtp.163.com
|
||||||
username: test@163.com
|
username: test@163.com
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
message=1
|
||||||
|
message.operate.error=123{0}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
message=1
|
||||||
|
message.operate.error=123{0}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
message=1
|
||||||
|
message.operate.error=123{0}
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
message.operate.success=operate successfully
|
|
||||||
message.operate.error=operation failure
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
message.operate.success=操作成功
|
|
||||||
message.operate.error=操作失败
|
|
||||||
Reference in New Issue
Block a user