MessageUtils.java 704 B

1234567891011121314151617181920212223242526272829
  1. package com.sysu.admin.utils.i18n;
  2. import org.springframework.context.MessageSource;
  3. import org.springframework.context.i18n.LocaleContextHolder;
  4. import org.springframework.stereotype.Component;
  5. /**
  6. * 国际化工具类
  7. */
  8. @Component
  9. public class MessageUtils {
  10. private static MessageSource messageSource;
  11. public MessageUtils(MessageSource messageSource) {
  12. this.messageSource = messageSource;
  13. }
  14. /**
  15. * 获取单个国际化翻译值
  16. */
  17. public static String get(String msgKey) {
  18. try {
  19. return messageSource.getMessage(msgKey, null, LocaleContextHolder.getLocale());
  20. } catch (Exception e) {
  21. return msgKey;
  22. }
  23. }
  24. }