Pārlūkot izejas kodu

Merge remote-tracking branch 'origin/main'

classic_blue 1 nedēļu atpakaļ
vecāks
revīzija
9b2df193d9

+ 64 - 0
fuintBackend/fuint-application/src/main/java/com/fuint/common/config/LocaleResolverConfig.java

@@ -0,0 +1,64 @@
+package com.fuint.common.config;
+
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.lang.NonNull;
+import org.springframework.lang.Nullable;
+import org.springframework.web.servlet.LocaleResolver;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.Locale;
+
+/**
+ * 自定义LocaleResolver
+ *
+ * @author Felix.Du
+ * @date: 2022/3/30 21:25
+ */
+@Configuration
+public class LocaleResolverConfig implements LocaleResolver {
+
+	@Autowired
+	private HttpServletRequest request;
+
+	public Locale getLocal() {
+		return resolveLocale(request);
+	}
+
+	/**
+	 * 从HttpServletRequest中获取Locale
+	 *
+	 * @param httpServletRequest    httpServletRequest
+	 * @return                      语言Local
+	 */
+	@Override
+	public Locale resolveLocale(HttpServletRequest httpServletRequest) {
+		//获取请求中的语言参数
+		String language = httpServletRequest.getHeader("lang");
+		//如果没有就使用默认的(根据主机的语言环境生成一个 Locale
+		Locale locale = Locale.getDefault();
+		//如果请求的链接中携带了 国际化的参数
+		if (!StringUtils.isEmpty(language)){
+			//zh_CN
+			String[] s = language.split("-");
+			//国家,地区
+			locale = new Locale(s[0], s[1]);
+		}
+		return locale;
+	}
+
+	/**
+	 * 用于实现Locale的切换。比如SessionLocaleResolver获取Locale的方式是从session中读取,但如果
+	 * 用户想要切换其展示的样式(由英文切换为中文),那么这里的setLocale()方法就提供了这样一种可能
+	 *
+	 * @param request               HttpServletRequest
+	 * @param httpServletResponse   HttpServletResponse
+	 * @param locale                locale
+	 */
+	@Override
+	public void setLocale(@NonNull HttpServletRequest request, @Nullable HttpServletResponse httpServletResponse, @Nullable Locale locale) {
+
+	}
+}

+ 1 - 1
fuintBackend/fuint-application/src/main/java/com/fuint/common/param/PageParam.java

@@ -16,7 +16,7 @@ public class PageParam implements Serializable {
     private Integer page = 1;
 
     @ApiModelProperty("分页大小")
-    private int pageSize = 20;
+    private Integer pageSize = 20;
 
     /**
      * 排序字段

+ 112 - 0
fuintBackend/fuint-application/src/main/java/com/fuint/common/util/I18nUtil.java

@@ -0,0 +1,112 @@
+package com.fuint.common.util;
+
+import com.fuint.common.config.LocaleResolverConfig;
+import lombok.extern.slf4j.Slf4j;
+import lombok.var;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.support.ResourceBundleMessageSource;
+import org.springframework.stereotype.Component;
+import org.springframework.util.StringUtils;
+
+
+import javax.annotation.PostConstruct;
+import java.nio.charset.StandardCharsets;
+import java.util.Locale;
+import java.util.Objects;
+
+@Slf4j
+@Component
+public class I18nUtil {
+
+	@Value("${spring.messages.basename}")
+	private String basename;
+
+	private final LocaleResolverConfig resolver;
+
+	private static LocaleResolverConfig customLocaleResolver;
+
+	private static String path;
+
+
+	public I18nUtil(LocaleResolverConfig resolver) {
+		this.resolver = resolver;
+	}
+
+
+	@PostConstruct
+	public void init() {
+		setBasename(basename);
+		setCustomLocaleResolver(resolver);
+	}
+
+	/**
+	 * 获取 国际化后内容信息
+	 *
+	 * @param code 国际化key
+	 * @return 国际化后内容信息
+	 */
+	public static String getMessage(String code) {
+		Locale locale = customLocaleResolver.getLocal();
+		return getMessage(code, null, code, locale);
+	}
+
+	/**
+	 * 获取指定语言中的国际化信息,如果没有则走英文
+	 *
+	 * @param code 国际化 key
+	 * @param lang 语言参数
+	 * @return 国际化后内容信息
+	 */
+	public static String getMessage(String code, String lang) {
+		Locale locale;
+		if (StringUtils.isEmpty(lang)) {
+			locale = Locale.US;
+		} else {
+			try {
+				var split = lang.split("-");
+				locale = new Locale(split[0], split[1]);
+			} catch (Exception e) {
+				locale = Locale.US;
+			}
+		}
+		return getMessage(code, null, code, locale);
+	}
+
+	/**
+	 * 获取站内信指定语言 目前只支持 中文与英文两类 默认英文
+	 *
+	 * @param code 国际化 key
+	 * @param lang 语言参数
+	 * @return 国际化后内容信息
+	 */
+	public static String getStationLetterMessage(String code, String lang) {
+		Locale locale = Objects.equals(lang, "zh-CN") ? Locale.SIMPLIFIED_CHINESE : Locale.US;
+		return getMessage(code, null, code, locale);
+	}
+
+
+	public static String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
+		ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
+		messageSource.setDefaultEncoding(StandardCharsets.UTF_8.toString());
+		messageSource.setBasename(path);
+		String content;
+		try {
+			content = messageSource.getMessage(code, args, locale);
+		} catch (Exception e) {
+			log.error("国际化参数获取失败===>{},{}", e.getMessage(), e);
+			content = defaultMessage;
+		}
+		return content;
+
+	}
+
+	public static void setBasename(String basename) {
+		I18nUtil.path = basename;
+	}
+
+	public static void setCustomLocaleResolver(LocaleResolverConfig resolver) {
+		I18nUtil.customLocaleResolver = resolver;
+	}
+
+}
+

+ 8 - 2
fuintBackend/fuint-application/src/main/java/com/fuint/module/backendApi/controller/BackendCurrencyController.java

@@ -3,6 +3,7 @@ package com.fuint.module.backendApi.controller;
 import com.fuint.common.dto.AccountInfo;
 import com.fuint.common.dto.ext.CurrencyDto;
 import com.fuint.common.param.PageParam;
+import com.fuint.common.util.I18nUtil;
 import com.fuint.common.util.TokenUtil;
 import com.fuint.common.vo.CurrencyVo;
 import com.fuint.framework.web.BaseController;
@@ -19,6 +20,7 @@ import com.fuint.utils.StringUtil;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.AllArgsConstructor;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.web.bind.annotation.*;
 import javax.servlet.http.HttpServletRequest;
@@ -43,6 +45,9 @@ public class BackendCurrencyController extends BaseController {
      */
     private CurrencyService currencyService;
 
+    @Autowired
+    private HttpServletRequest request;
+
     /**
      * 货币表列表查询
      *
@@ -52,8 +57,9 @@ public class BackendCurrencyController extends BaseController {
     @RequestMapping(value = "/list", method = RequestMethod.GET)
     @CrossOrigin
     @PreAuthorize("@pms.hasPermission('currency:list')")
-    public PageResult<CurrencyVo> list(@RequestParam(defaultValue = "") String name, PageParam pageParam) throws BusinessCheckException {
-        return currencyService.getCurrencyById(name,pageParam);
+    public ResponseObject list(@RequestParam(defaultValue = "") String name, PageParam pageParam) throws BusinessCheckException {
+        String message1 = I18nUtil.getMessage("A00001", request.getHeader("lang"));
+        return getSuccessResult(message1);
     }
 
     /**

+ 2 - 2
fuintBackend/fuint-application/src/main/resources/application.properties

@@ -1,7 +1,7 @@
 # \u57FA\u672C\u914D\u7F6E
 server.port=8080
 env.profile=dev
-env.properties.path=D:\\Users\\Administrator\\IdeaProject\\store-windows\\fuintBackend\\configure
+env.properties.path=D:/fuint/store-windows/fuintBackend/configure
 
 # \u6570\u636E\u5E93\u914D\u7F6E
 spring.datasource.type=com.zaxxer.hikari.HikariDataSource
@@ -24,7 +24,7 @@ mybatis-plus.mapper-locations = classpath*:/mapper/*.xml
 # \u9ED8\u8BA4\u65F6\u95F4\u683C\u5F0F
 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
 
-
+spring.messages.basename=i18n/messages
 mybatis-plus.configuration.log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
 mybatis-plus.global-config.db-config.logic-delete-value=1
 mybatis-plus.global-config.db-config.logic-not-delete-value=0

+ 0 - 0
fuintBackend/fuint-application/src/main/resources/i18n/messages.properties


+ 1 - 0
fuintBackend/fuint-application/src/main/resources/i18n/messages_en_US.properties

@@ -0,0 +1 @@
+

+ 1 - 0
fuintBackend/fuint-application/src/main/resources/i18n/messages_zh_CN.properties

@@ -0,0 +1 @@
+