springboot 国际化处理 Published on Aug 30, 2023 in 随笔 with 1 comment ```java package com.ruoyi.common.i18n; import java.lang.reflect.Field; import java.util.List; import java.util.Locale; import java.util.Objects; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.util.PropertyPlaceholderHelper; import com.ruoyi.common.utils.ReflectASMUtils; import com.ruoyi.common.utils.SpringUtils; import com.ruoyi.common.utils.StringUtils; public class I18nUtils { private final static MessageSource messageSource = SpringUtils .getBean(AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME); private static final PropertyPlaceholderHelper FieldPlaceholderHelper = new PropertyPlaceholderHelper("#{", "}"); private static final PropertyPlaceholderHelper PlaceholderHelper = new PropertyPlaceholderHelper("{", "}", ":", true); public static void replaceI18nFields(List> objs) { replaceI18nFields(objs, LocaleContextHolder.getLocale()); } /** * 将列表所有对象中@I18nField标注的字段替换为指定国际化语言值 * * @param objs * @param locale */ public static void replaceI18nFields(List> objs, Locale locale) { if (Objects.isNull(objs)) { return; } objs.forEach(obj -> replaceI18nFields(obj, locale)); } public static void replaceI18nFields(Object obj) { replaceI18nFields(obj, LocaleContextHolder.getLocale()); } /** * 将对象有@I18nField标注的字段替换为指定国际化语言值 * * @param obj * @param locale */ public static void replaceI18nFields(Object obj, Locale locale) { if (Objects.isNull(obj)) { return; } Field[] declaredFields = obj.getClass().getDeclaredFields(); for (Field field : declaredFields) { if (field.isAnnotationPresent(I18nField.class)) { I18nField i18nField = field.getAnnotation(I18nField.class); try { if (StringUtils.isNotEmpty(i18nField.value())) { String langStr = i18nField.value(); // 先将#{fieldName}占位替换为obj字段值 langStr = FieldPlaceholderHelper.replacePlaceholders(langStr, placehoderName -> { Object fieldV = ReflectASMUtils.invokeGetter(obj, placehoderName); return Objects.nonNull(fieldV) ? fieldV.toString() : StringUtils.EMPTY; }); String langValue = get(langStr, locale); ReflectASMUtils.invokeSetter(obj, field.getName(), langValue); } else { Object fieldV = ReflectASMUtils.invokeGetter(obj, field.getName()); if (Objects.nonNull(fieldV)) { String langKey = fieldV.toString(); String langValue = get(langKey, locale); ReflectASMUtils.invokeSetter(obj, field.getName(), langValue); } } } catch (Exception e) { e.printStackTrace(); } } } } /** * 获取国际化键名对应的当前默认语言值 * * @param str * @return */ public static String get(String str) { return get(str, LocaleContextHolder.getLocale()); } /** * 获取国际化键名指定的语言值 * * @param str * @param locale * @param args * @return */ public static String get(String str, Locale locale, Object... args) { if (StringUtils.isEmpty(str)) { return str; } return PlaceholderHelper.replacePlaceholders(str, langKey -> messageSource.getMessage(langKey, args, locale)); } } ``` ```java package com.jinw.cms.i18n; import com.jinw.utils.cms.StringUtils; import java.lang.annotation.*; /** * 类字段添加此注释,根据国际化字典表获取当前语言环境翻译.@see I18nUtils.replaceI18nFields * * @author liux */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface I18nField { /** * 国际化表键名,为空时使用字段值获取当前语言环境值 * * 可使用占位符来获取当前对象其他字段属性值,例如实体类的ID字段值,可使用#{fieldName},大括号内是字段名,会被替换为指定字段的值。 * */ public String value() default StringUtils.EMPTY; } ```  ```yaml spring: # 资源信息 messages: encoding: utf-8 # 国际化资源文件路径(配置文件路径) basename: i18n/messages ``` 本文由 admin 创作,采用 知识共享署名4.0 国际许可协议进行许可。本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。
要怎么全局使用的呢?