i18nAspect.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package com.poyee.aspectj;
  2. import com.poyee.annotation.I18n;
  3. import com.poyee.common.enums.I18nFormat;
  4. import com.poyee.util.ServletUtils;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.aspectj.lang.JoinPoint;
  8. import org.aspectj.lang.Signature;
  9. import org.aspectj.lang.annotation.Aspect;
  10. import org.aspectj.lang.annotation.Before;
  11. import org.aspectj.lang.annotation.Pointcut;
  12. import org.aspectj.lang.reflect.MethodSignature;
  13. import org.springframework.context.i18n.LocaleContextHolder;
  14. import org.springframework.stereotype.Component;
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.servlet.http.HttpSession;
  17. import java.lang.reflect.Method;
  18. /**
  19. * 国际化切面类,用于处理国际化相关的逻辑
  20. */
  21. @Slf4j
  22. @Aspect
  23. @Component
  24. public class i18nAspect {
  25. // 配置织入点,针对使用了i18n注解的方法
  26. @Pointcut("@annotation(com.poyee.annotation.I18n)")
  27. public void logPointCut() {
  28. }
  29. /**
  30. * 在控制器的方法执行前,处理国际化逻辑
  31. * @param joinPoint 切入点对象,包含被拦截的方法信息
  32. */
  33. @Before("execution(public * com.poyee.controller.*.*(..))")
  34. // @Before("logPointCut()")
  35. public void doBefore(JoinPoint joinPoint) {
  36. Signature signature = joinPoint.getSignature();
  37. MethodSignature methodSignature = (MethodSignature) signature;
  38. Method method = methodSignature.getMethod();
  39. checkI18n(method);
  40. }
  41. /**
  42. * 判断是否支持国际化,并根据情况设置会话属性
  43. * @param method 被拦截的方法对象,用于获取注解信息
  44. */
  45. private void checkI18n(Method method) {
  46. if (method.isAnnotationPresent(I18n.class)) {
  47. I18n i18n = method.getAnnotation(I18n.class);
  48. HttpServletRequest request = ServletUtils.getRequest();
  49. String language = request.getHeader("Accept-Language");
  50. boolean isI18n = true;
  51. if(StringUtils.isBlank(language)) {
  52. language = LocaleContextHolder.getLocale().getLanguage();
  53. isI18n = false;
  54. }
  55. try {
  56. HttpSession session = ServletUtils.getSession(false);
  57. if (session == null) {
  58. log.warn("Session is null, cannot set I18n attributes.");
  59. return;
  60. }
  61. // 根据方法上的i18n注解和请求头中的语言信息,决定是否启用国际化
  62. if(i18n.value() && isI18n) {
  63. session.setAttribute("language", language);
  64. I18nFormat[] format = i18n.format();
  65. if(format.length > 0) {
  66. session.setAttribute("i18nFormat", i18n.format());
  67. if (log.isInfoEnabled()) {
  68. log.info("i18nFormat:{}", format[0].getMsg());
  69. }
  70. }
  71. session.setAttribute("i18n", true);
  72. } else {
  73. // 如果 I18n.value() 为 false,但 isI18n 为 true,仍不启用国际化
  74. session.setAttribute("i18n", false);
  75. }
  76. } catch (Exception e) {
  77. log.error("Failed to set I18n attributes", e);
  78. }
  79. }
  80. }
  81. }