diff --git a/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/MessageUtils.java b/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/MessageUtils.java new file mode 100644 index 00000000..b8f6f0c1 --- /dev/null +++ b/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/MessageUtils.java @@ -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()); + } +} diff --git a/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/config/MyLocaleResolver.java b/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/config/MyLocaleResolver.java new file mode 100644 index 00000000..1f0b95df --- /dev/null +++ b/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/config/MyLocaleResolver.java @@ -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(); + } +} diff --git a/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/config/WebMvcConfiguration.java b/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/config/WebMvcConfiguration.java index d1088c4f..b773aa20 100644 --- a/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/config/WebMvcConfiguration.java +++ b/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/config/WebMvcConfiguration.java @@ -12,11 +12,16 @@ import org.springframework.http.converter.json.MappingJackson2HttpMessageConvert import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; 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.ViewControllerRegistry; 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.Locale; /** * Spring Boot 2.0 解决跨域问题 @@ -68,4 +73,13 @@ public class WebMvcConfiguration implements WebMvcConfigurer { jackson2HttpMessageConverter.setObjectMapper(objectMapper); converters.add(jackson2HttpMessageConverter); } + + /** + * 国际化配置 + */ + @Bean + public LocaleResolver localeResolver(){ + return new MyLocaleResolver(); + } + } diff --git a/jero-boot-modules/src/main/java/com/jero/modules/activiti/controller/AController.java b/jero-boot-modules/src/main/java/com/jero/modules/activiti/controller/AController.java index 4b801ce8..d1700234 100644 --- a/jero-boot-modules/src/main/java/com/jero/modules/activiti/controller/AController.java +++ b/jero-boot-modules/src/main/java/com/jero/modules/activiti/controller/AController.java @@ -2,6 +2,7 @@ package com.jero.modules.activiti.controller; import cn.hutool.core.bean.BeanUtil; import com.jero.common.api.vo.Result; +import com.jero.common.util.MessageUtils; import com.jero.modules.activiti.service.ProcessService; import io.swagger.annotations.ApiOperation; 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.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.util.AntPathMatcher; import org.springframework.util.CollectionUtils; +import org.springframework.util.PathMatcher; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; @@ -214,5 +217,10 @@ public class AController { log.error("导出model的bpmn文件失败:modelId={}", modelId, e); } } - + @GetMapping("test") + public Result getRedisTest(){ + String msg1 = MessageUtils.message("message"); + String msg2 = MessageUtils.message("message.operate.error","123456"); + return Result.OK(msg1+"\n"+msg2) ; + } } diff --git a/jero-boot-single-startup/src/main/resources/application-dev.yml b/jero-boot-single-startup/src/main/resources/application-dev.yml index b600ec3c..94949566 100644 --- a/jero-boot-single-startup/src/main/resources/application-dev.yml +++ b/jero-boot-single-startup/src/main/resources/application-dev.yml @@ -24,6 +24,8 @@ spring: multipart: max-file-size: 10MB max-request-size: 10MB + messages: + basename: i18n/messages mail: host: smtp.163.com username: test@163.com diff --git a/jero-boot-single-startup/src/main/resources/i18n/messages.properties b/jero-boot-single-startup/src/main/resources/i18n/messages.properties new file mode 100644 index 00000000..c0367056 --- /dev/null +++ b/jero-boot-single-startup/src/main/resources/i18n/messages.properties @@ -0,0 +1,2 @@ +message=1 +message.operate.error=123{0} \ No newline at end of file diff --git a/jero-boot-single-startup/src/main/resources/i18n/messages_en.properties b/jero-boot-single-startup/src/main/resources/i18n/messages_en.properties new file mode 100644 index 00000000..c0367056 --- /dev/null +++ b/jero-boot-single-startup/src/main/resources/i18n/messages_en.properties @@ -0,0 +1,2 @@ +message=1 +message.operate.error=123{0} \ No newline at end of file diff --git a/jero-boot-single-startup/src/main/resources/i18n/messages_zh.properties b/jero-boot-single-startup/src/main/resources/i18n/messages_zh.properties new file mode 100644 index 00000000..c0367056 --- /dev/null +++ b/jero-boot-single-startup/src/main/resources/i18n/messages_zh.properties @@ -0,0 +1,2 @@ +message=1 +message.operate.error=123{0} \ No newline at end of file diff --git a/jero-boot-single-startup/src/main/resources/messages_en.properties b/jero-boot-single-startup/src/main/resources/messages_en.properties deleted file mode 100644 index 8fe845c0..00000000 --- a/jero-boot-single-startup/src/main/resources/messages_en.properties +++ /dev/null @@ -1,2 +0,0 @@ -message.operate.success=operate successfully -message.operate.error=operation failure \ No newline at end of file diff --git a/jero-boot-single-startup/src/main/resources/messages_zh.properties b/jero-boot-single-startup/src/main/resources/messages_zh.properties deleted file mode 100644 index d3160ecf..00000000 --- a/jero-boot-single-startup/src/main/resources/messages_zh.properties +++ /dev/null @@ -1,2 +0,0 @@ -message.operate.success=操作成功 -message.operate.error=操作失败 \ No newline at end of file