From d9aba78edd3f6343f00e1794422d26e28b1176f0 Mon Sep 17 00:00:00 2001 From: ‘liusuyi’ <1951119284@qq.com> Date: 星期四, 02 十一月 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蹇呴』鏄痾h-CN鏍煎紡鐨勶紝涓棿鐨�-蹇呴』瑕佸畬鏁达紝涓嶈兘鍙紶閫抸h鎴杄n + 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) { + } + + /** + * 鍦ㄦ暣涓姹傜粨鏉熶箣鍚庤璋冪敤锛屼篃灏辨槸鍦―ispatcherServlet 娓叉煋浜嗗搴旂殑瑙嗗浘涔嬪悗鎵ц锛堜富瑕佹槸鐢ㄤ簬杩涜璧勬簮娓呯悊宸ヤ綔锛� + */ + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { + } + +} -- Gitblit v1.9.3