All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.antelopeframework.spring.context.MessageSourceHelper Maven / Gradle / Ivy

The newest version!
package com.github.antelopeframework.spring.context;

import java.util.Locale;

import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.context.i18n.LocaleContextHolder;

/**
 * A helper that delegates to a {@link MessageSource} instance. Usage:
 * 
 * 
 *   {{message "code" args* [default="default message"] [local] }}
 * 
* * Where: *
    *
  • code: String literal. Required.
  • *
  • args: Object. Optional
  • *
  • default: A default message. Optional.
  • *
  • locale: Locale. Optional.
  • *
* * 参考自: * * * @author yangzhi * */ public class MessageSourceHelper implements MessageSourceAware { @Autowired private MessageSource messageSource; public void setMessageSource(final MessageSource messageSource) { this.messageSource = Validate.notNull(messageSource, "A message source is required."); } /** * * @param code * @param args * @return */ public String getMessage(String code, Object[] args) { return this.getMessage(code, args, currentLocale()); } /** * * @param code * @param args * @param locale * @return */ public String getMessage(String code, Object[] args, Locale locale) { return this.getMessage(code, args, code, locale); } /** * * @param code * @param args * @param defaultMessage * @return */ public String getMessage(String code, Object[] args, String defaultMessage) { return this.getMessage(code, args, defaultMessage, currentLocale()); } /** * * @param code * @param args * @param defaultMessage * @param locale * @return */ public String getMessage(String code, Object[] args, String defaultMessage, Locale locale) { return messageSource.getMessage(code, args, defaultMessage, locale); } /** * Resolve the current user locale. * * @return The current user locale. */ protected Locale currentLocale() { return LocaleContextHolder.getLocale(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy