package com.qianwen.core.i18n.advice; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.StrUtil; import java.lang.reflect.Field; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.qianwen.core.i18n.annotation.I18nClass; import com.qianwen.core.i18n.annotation.I18nField; import com.qianwen.core.i18n.props.I18nOptions; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.core.MethodParameter; import org.springframework.expression.Expression; import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.server.ServerHttpRequest; import org.springframework.http.server.ServerHttpResponse; import org.springframework.util.Assert; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; @RestControllerAdvice /* loaded from: blade-starter-i18n-9.3.0.0-SNAPSHOT.jar:org/springblade/core/i18n/advice/I18nResponseAdvice.class */ public class I18nResponseAdvice implements ResponseBodyAdvice { private final MessageSource messageSource; private final boolean useCodeAsDefaultMessage; private Locale fallbackLocale; private final List excludeClassList = Arrays.asList("com.qianwen.core.tool.api.R"); private static final Logger log = LoggerFactory.getLogger(I18nResponseAdvice.class); private static final ExpressionParser PARSER = new SpelExpressionParser(); private static final Map EXPRESSION_CACHE = new HashMap(); public I18nResponseAdvice(MessageSource messageSource, I18nOptions i18nOptions) { this.fallbackLocale = null; this.messageSource = messageSource; String fallbackLanguageTag = i18nOptions.getFallbackLanguageTag(); if (fallbackLanguageTag != null) { String[] arr = fallbackLanguageTag.split("-"); Assert.isTrue(arr.length == 2, "error fallbackLanguageTag!"); this.fallbackLocale = new Locale(arr[0], arr[1]); } this.useCodeAsDefaultMessage = i18nOptions.isUseCodeAsDefaultMessage(); } public boolean supports(MethodParameter methodParameter, Class> aClass) { return true; } public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) { switchLanguage(body); return body; } public void switchLanguage(Object source) { Field[] fields; if (source == null) { return; } Class sourceClass = source.getClass(); I18nClass i18nClass = (I18nClass) sourceClass.getAnnotation(I18nClass.class); if (i18nClass == null && !this.excludeClassList.contains(sourceClass.getName())) { return; } for (Field field : ReflectUtil.getFields(sourceClass)) { Class fieldType = field.getType(); Object fieldValue = ReflectUtil.getFieldValue(source, field); if (fieldValue instanceof String) { I18nField i18nField = (I18nField) field.getAnnotation(I18nField.class); if (i18nField != null) { String conditionExpression = i18nField.condition(); if (StrUtil.isNotEmpty(conditionExpression)) { Expression expression = EXPRESSION_CACHE.get(conditionExpression); if (expression == null) { expression = PARSER.parseExpression(conditionExpression); EXPRESSION_CACHE.put(conditionExpression, expression); } Boolean needI18n = (Boolean) expression.getValue(source, Boolean.class); if (needI18n != null && !needI18n.booleanValue()) { } } String annotationCode = i18nField.code(); String code = StrUtil.isNotEmpty(annotationCode) ? annotationCode : (String) fieldValue; if (!StrUtil.isEmpty(code)) { Locale locale = LocaleContextHolder.getLocale(); String message = codeToMessage(code, locale, this.fallbackLocale); ReflectUtil.setFieldValue(source, field, message); } } } else if (fieldValue instanceof Collection) { Collection elements = (Collection) fieldValue; if (!CollectionUtil.isEmpty(elements)) { for (Object element : elements) { switchLanguage(element); } } } else if (fieldType.isArray()) { Object[] elements2 = (Object[]) fieldValue; if (elements2 != null && elements2.length != 0) { for (Object element2 : elements2) { switchLanguage(element2); } } } else { switchLanguage(fieldValue); } } } /* JADX WARN: Removed duplicated region for block: B:15:0x0060 */ /* JADX WARN: Removed duplicated region for block: B:17:0x0062 */ /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct code enable 'Show inconsistent code' option in preferences */ private java.lang.String codeToMessage(java.lang.String r8, java.util.Locale r9, java.util.Locale r10) { /* r7 = this; r0 = r7 org.springframework.context.MessageSource r0 = r0.messageSource // Catch: org.springframework.context.NoSuchMessageException -> L11 r1 = r8 r2 = 0 r3 = r9 java.lang.String r0 = r0.getMessage(r1, r2, r3) // Catch: org.springframework.context.NoSuchMessageException -> L11 r11 = r0 r0 = r11 return r0 L11: r13 = move-exception r0 = r13 r12 = r0 org.slf4j.Logger r0 = com.qianwen.core.i18n.advice.I18nResponseAdvice.log java.lang.String r1 = "[codeToMessage]未找到对应的国际化配置,code: {}, local: {}" r2 = r8 r3 = r9 r0.warn(r1, r2, r3) r0 = r10 if (r0 == 0) goto L59 r0 = r9 r1 = r10 if (r0 == r1) goto L59 r0 = r7 org.springframework.context.MessageSource r0 = r0.messageSource // Catch: org.springframework.context.NoSuchMessageException -> L3d r1 = r8 r2 = 0 r3 = r10 java.lang.String r0 = r0.getMessage(r1, r2, r3) // Catch: org.springframework.context.NoSuchMessageException -> L3d r11 = r0 r0 = r11 return r0 L3d: r13 = move-exception org.slf4j.Logger r0 = com.qianwen.core.i18n.advice.I18nResponseAdvice.log java.lang.String r1 = "[codeToMessage]期望语言和回退语言中皆未找到对应的国际化配置,code: {}, local: {}, fallbackLocale:{}" r2 = 3 java.lang.Object[] r2 = new java.lang.Object[r2] r3 = r2 r4 = 0 r5 = r8 r3[r4] = r5 r3 = r2 r4 = 1 r5 = r9 r3[r4] = r5 r3 = r2 r4 = 2 r5 = r10 r3[r4] = r5 r0.warn(r1, r2) L59: r0 = r7 boolean r0 = r0.useCodeAsDefaultMessage if (r0 == 0) goto L62 r0 = r8 return r0 L62: r0 = r12 throw r0 */ throw new UnsupportedOperationException("Method not decompiled: com.qianwen.core.i18n.advice.I18nResponseAdvice.codeToMessage(java.lang.String, java.util.Locale, java.util.Locale):java.lang.String"); } }