• 首页
  • 邻居
  • 关于
  • 归档
  • 搜索
  • 夜间模式
    ©2020-2026  我的学习笔记 Theme by OneBlog

    我的学习笔记博客

    搜索
    标签
    # 随笔 # Java # 教程 # openwrt # Mysql # SQL # 爬虫 # post # Js调优 # MAVEN
  • 首页>
  • 随笔>
  • 正文
  • springboot 国际化处理

    2023年08月30日 1.6 k 阅读 1 评论 4065 字
    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));
        }
    }
    
    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 {
    
        /**
         * 国际化表键名,为空时使用字段值获取当前语言环境值
         * <p>
         * 可使用占位符来获取当前对象其他字段属性值,例如实体类的ID字段值,可使用#{fieldName},大括号内是字段名,会被替换为指定字段的值。
         * </p>
         */
        public String value() default StringUtils.EMPTY;
    }
    

    spring:
      # 资源信息
      messages:
        encoding: utf-8
        # 国际化资源文件路径(配置文件路径)
        basename: i18n/messages
    
    本文著作权归作者 [ admin ] 享有,未经作者书面授权,禁止转载,封面图片来源于 [ 互联网 ] ,本文仅供个人学习、研究和欣赏使用。如有异议,请联系博主及时处理。
    取消回复

    发表留言
    回复

    读者留言1

    1. 提问 Lv.1
      2024-07-19 14:49 回复

      要怎么全局使用的呢?

    加载更多评论
    加载中...
    — 已加载全部评论 —
    首页邻居关于归档
    Copyright©2020-2026  All Rights Reserved.  Load:0.017 s
    京ICP备18019712号
    Theme by OneBlog V3.6.5
    夜间模式

    开源不易,请尊重作者版权,保留基本的版权信息。