From d9aba78edd3f6343f00e1794422d26e28b1176f0 Mon Sep 17 00:00:00 2001
From: ‘liusuyi’ <1951119284@qq.com>
Date: Thu, 02 Nov 2023 16:10:25 +0800
Subject: [PATCH] 增加I18N业务适配后台管理国际化

---
 ruoyi-admin/src/main/resources/i18n/messages_en_US.properties             |   37 ++++++++++++
 ruoyi-admin/src/main/java/com/ruoyi/web/core/config/I18nConfig.java       |   46 +++++++++++++++
 ruoyi-admin/src/main/resources/i18n/messages_zh_CN.properties             |   37 ++++++++++++
 ruoyi-common/src/main/java/com/ruoyi/common/filter/MyI18nInterceptor.java |   41 +++++++++++++
 4 files changed, 161 insertions(+), 0 deletions(-)

diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/I18nConfig.java b/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/I18nConfig.java
new file mode 100644
index 0000000..d0cd0e1
--- /dev/null
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/I18nConfig.java
@@ -0,0 +1,46 @@
+package com.ruoyi.web.core.config;
+
+import com.ruoyi.common.filter.MyI18nInterceptor;
+import lombok.extern.slf4j.Slf4j;
+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.InterceptorRegistration;
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
+import org.springframework.web.servlet.i18n.SessionLocaleResolver;
+
+import java.util.Locale;
+
+
+@Configuration
+@Slf4j
+public class I18nConfig implements WebMvcConfigurer {
+    @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("lang");
+    //    return lci;
+    //}
+
+    @Override
+    public void addInterceptors(InterceptorRegistry registry) {
+        // 注册拦截器
+        MyI18nInterceptor myHandlerInterceptor = new MyI18nInterceptor();
+        InterceptorRegistration loginRegistry = registry.addInterceptor(myHandlerInterceptor);
+        // 拦截路径
+        loginRegistry.addPathPatterns("/**");
+    }
+
+}
diff --git a/ruoyi-admin/src/main/resources/i18n/messages_en_US.properties b/ruoyi-admin/src/main/resources/i18n/messages_en_US.properties
new file mode 100644
index 0000000..3b74bd8
--- /dev/null
+++ b/ruoyi-admin/src/main/resources/i18n/messages_en_US.properties
@@ -0,0 +1,37 @@
+#\u9519\u8BEF\u6D88\u606F
+not.null=* Mandatory
+user.jcaptcha.error=Verification code error
+user.jcaptcha.expire=The verification code has expired
+user.not.exists=User does not exist/password error
+user.password.not.match=User does not exist/password error
+user.password.retry.limit.count=Password input error {0} times
+user.password.retry.limit.exceed=Password input error {0} times, account locked for {1} minutes
+user.password.delete=Sorry, your account has been deleted
+user.blocked=The user has been banned. Please contact the administrator
+role.blocked=The role has been banned. Please contact the administrator
+user.logout.success=Exit successfully
+
+length.not.valid=The length must be between {min} and {max} characters
+
+user.username.not.valid=*Composed of 2 to 20 Chinese characters, letters, numbers, or underscores, and must start with a non numeric character
+user.password.not.valid=*5-50 characters
+ 
+user.email.not.valid=Email format error
+user.mobile.phone.number.not.valid=Mobile number format error
+user.login.success=Login successful
+user.register.success=login was successful
+user.notfound=Please log in again
+user.forcelogout=Administrator forced exit, please log in again
+user.unknown.error=Unknown error, please log in again
+
+##\u6587\u4EF6\u4E0A\u4F20\u6D88\u606F
+upload.exceed.maxSize=The uploaded file size exceeds the limit file size< The maximum allowed file size is: {0}MB !
+upload.filename.exceed.length=The maximum length of the uploaded file name is {0} characters
+
+##\u6743\u9650
+no.permission=You do not have permission for data. Please contact the administrator to add permission [{0}]
+no.create.permission=You do not have permission to create data. Please contact the administrator to add permissions [{0}]
+no.update.permission=You do not have permission to modify data. Please contact the administrator to add permissions [{0}]
+no.delete.permission=You do not have permission to delete data. Please contact the administrator to add permissions [{0}]
+no.export.permission=You do not have permission to export data. Please contact the administrator to add permissions [{0}]
+no.view.permission=You do not have permission to view data. Please contact the administrator to add permissions [{0}]
diff --git a/ruoyi-admin/src/main/resources/i18n/messages_zh_CN.properties b/ruoyi-admin/src/main/resources/i18n/messages_zh_CN.properties
new file mode 100644
index 0000000..4098fc9
--- /dev/null
+++ b/ruoyi-admin/src/main/resources/i18n/messages_zh_CN.properties
@@ -0,0 +1,37 @@
+#错误消息
+not.null=* 必须填写
+user.jcaptcha.error=验证码错误
+user.jcaptcha.expire=验证码已失效
+user.not.exists=用户不存在/密码错误
+user.password.not.match=用户不存在/密码错误
+user.password.retry.limit.count=密码输入错误{0}次
+user.password.retry.limit.exceed=密码输入错误{0}次,帐户锁定{1}分钟
+user.password.delete=对不起,您的账号已被删除
+user.blocked=用户已封禁,请联系管理员
+role.blocked=角色已封禁,请联系管理员
+user.logout.success=退出成功
+
+length.not.valid=长度必须在{min}到{max}个字符之间
+
+user.username.not.valid=* 2到20个汉字、字母、数字或下划线组成,且必须以非数字开头
+user.password.not.valid=* 5-50个字符
+ 
+user.email.not.valid=邮箱格式错误
+user.mobile.phone.number.not.valid=手机号格式错误
+user.login.success=登录成功
+user.register.success=注册成功
+user.notfound=请重新登录
+user.forcelogout=管理员强制退出,请重新登录
+user.unknown.error=未知错误,请重新登录
+
+##文件上传消息
+upload.exceed.maxSize=上传的文件大小超出限制的文件大小!<br/>允许的文件最大大小是:{0}MB!
+upload.filename.exceed.length=上传的文件名最长{0}个字符
+
+##权限
+no.permission=您没有数据的权限,请联系管理员添加权限 [{0}]
+no.create.permission=您没有创建数据的权限,请联系管理员添加权限 [{0}]
+no.update.permission=您没有修改数据的权限,请联系管理员添加权限 [{0}]
+no.delete.permission=您没有删除数据的权限,请联系管理员添加权限 [{0}]
+no.export.permission=您没有导出数据的权限,请联系管理员添加权限 [{0}]
+no.view.permission=您没有查看数据的权限,请联系管理员添加权限 [{0}]
diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/filter/MyI18nInterceptor.java b/ruoyi-common/src/main/java/com/ruoyi/common/filter/MyI18nInterceptor.java
new file mode 100644
index 0000000..836fba0
--- /dev/null
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/filter/MyI18nInterceptor.java
@@ -0,0 +1,41 @@
+package com.ruoyi.common.filter;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.context.i18n.LocaleContextHolder;
+import org.springframework.web.servlet.HandlerInterceptor;
+import org.springframework.web.servlet.ModelAndView;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.Locale;
+
+@Slf4j
+public class MyI18nInterceptor implements HandlerInterceptor {
+
+    @Override
+    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
+        final String key = "language";
+        String language = "en_US";//request.getHeader(key);
+        // 前端传递的language必须是zh-CN格式的,中间的-必须要完整,不能只传递zh或en
+        log.info("当前语言={}",language);
+        Locale locale = new Locale(language.split("_")[0],language.split("_")[1]);
+        // 这样赋值以后,MessageUtils.message方法就不用修改了
+        LocaleContextHolder.setLocale(locale);
+        return true;
+    }
+
+    /**
+     * 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)
+     */
+    @Override
+    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
+    }
+
+    /**
+     * 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)
+     */
+    @Override
+    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
+    }
+
+}

--
Gitblit v1.9.3