001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.context.support; 018 019import java.util.Locale; 020 021import org.springframework.context.MessageSource; 022import org.springframework.context.MessageSourceResolvable; 023import org.springframework.context.NoSuchMessageException; 024import org.springframework.context.i18n.LocaleContextHolder; 025import org.springframework.lang.Nullable; 026 027/** 028 * Helper class for easy access to messages from a MessageSource, 029 * providing various overloaded getMessage methods. 030 * 031 * <p>Available from ApplicationObjectSupport, but also reusable 032 * as a standalone helper to delegate to in application objects. 033 * 034 * @author Juergen Hoeller 035 * @since 23.10.2003 036 * @see ApplicationObjectSupport#getMessageSourceAccessor 037 */ 038public class MessageSourceAccessor { 039 040 private final MessageSource messageSource; 041 042 @Nullable 043 private final Locale defaultLocale; 044 045 046 /** 047 * Create a new MessageSourceAccessor, using LocaleContextHolder's locale 048 * as default locale. 049 * @param messageSource the MessageSource to wrap 050 * @see org.springframework.context.i18n.LocaleContextHolder#getLocale() 051 */ 052 public MessageSourceAccessor(MessageSource messageSource) { 053 this.messageSource = messageSource; 054 this.defaultLocale = null; 055 } 056 057 /** 058 * Create a new MessageSourceAccessor, using the given default locale. 059 * @param messageSource the MessageSource to wrap 060 * @param defaultLocale the default locale to use for message access 061 */ 062 public MessageSourceAccessor(MessageSource messageSource, Locale defaultLocale) { 063 this.messageSource = messageSource; 064 this.defaultLocale = defaultLocale; 065 } 066 067 068 /** 069 * Return the default locale to use if no explicit locale has been given. 070 * <p>The default implementation returns the default locale passed into the 071 * corresponding constructor, or LocaleContextHolder's locale as fallback. 072 * Can be overridden in subclasses. 073 * @see #MessageSourceAccessor(org.springframework.context.MessageSource, java.util.Locale) 074 * @see org.springframework.context.i18n.LocaleContextHolder#getLocale() 075 */ 076 protected Locale getDefaultLocale() { 077 return (this.defaultLocale != null ? this.defaultLocale : LocaleContextHolder.getLocale()); 078 } 079 080 /** 081 * Retrieve the message for the given code and the default Locale. 082 * @param code the code of the message 083 * @param defaultMessage the String to return if the lookup fails 084 * @return the message 085 */ 086 public String getMessage(String code, String defaultMessage) { 087 String msg = this.messageSource.getMessage(code, null, defaultMessage, getDefaultLocale()); 088 return (msg != null ? msg : ""); 089 } 090 091 /** 092 * Retrieve the message for the given code and the given Locale. 093 * @param code the code of the message 094 * @param defaultMessage the String to return if the lookup fails 095 * @param locale the Locale in which to do lookup 096 * @return the message 097 */ 098 public String getMessage(String code, String defaultMessage, Locale locale) { 099 String msg = this.messageSource.getMessage(code, null, defaultMessage, locale); 100 return (msg != null ? msg : ""); 101 } 102 103 /** 104 * Retrieve the message for the given code and the default Locale. 105 * @param code the code of the message 106 * @param args arguments for the message, or {@code null} if none 107 * @param defaultMessage the String to return if the lookup fails 108 * @return the message 109 */ 110 public String getMessage(String code, @Nullable Object[] args, String defaultMessage) { 111 String msg = this.messageSource.getMessage(code, args, defaultMessage, getDefaultLocale()); 112 return (msg != null ? msg : ""); 113 } 114 115 /** 116 * Retrieve the message for the given code and the given Locale. 117 * @param code the code of the message 118 * @param args arguments for the message, or {@code null} if none 119 * @param defaultMessage the String to return if the lookup fails 120 * @param locale the Locale in which to do lookup 121 * @return the message 122 */ 123 public String getMessage(String code, @Nullable Object[] args, String defaultMessage, Locale locale) { 124 String msg = this.messageSource.getMessage(code, args, defaultMessage, locale); 125 return (msg != null ? msg : ""); 126 } 127 128 /** 129 * Retrieve the message for the given code and the default Locale. 130 * @param code the code of the message 131 * @return the message 132 * @throws org.springframework.context.NoSuchMessageException if not found 133 */ 134 public String getMessage(String code) throws NoSuchMessageException { 135 return this.messageSource.getMessage(code, null, getDefaultLocale()); 136 } 137 138 /** 139 * Retrieve the message for the given code and the given Locale. 140 * @param code the code of the message 141 * @param locale the Locale in which to do lookup 142 * @return the message 143 * @throws org.springframework.context.NoSuchMessageException if not found 144 */ 145 public String getMessage(String code, Locale locale) throws NoSuchMessageException { 146 return this.messageSource.getMessage(code, null, locale); 147 } 148 149 /** 150 * Retrieve the message for the given code and the default Locale. 151 * @param code the code of the message 152 * @param args arguments for the message, or {@code null} if none 153 * @return the message 154 * @throws org.springframework.context.NoSuchMessageException if not found 155 */ 156 public String getMessage(String code, @Nullable Object[] args) throws NoSuchMessageException { 157 return this.messageSource.getMessage(code, args, getDefaultLocale()); 158 } 159 160 /** 161 * Retrieve the message for the given code and the given Locale. 162 * @param code the code of the message 163 * @param args arguments for the message, or {@code null} if none 164 * @param locale the Locale in which to do lookup 165 * @return the message 166 * @throws org.springframework.context.NoSuchMessageException if not found 167 */ 168 public String getMessage(String code, @Nullable Object[] args, Locale locale) throws NoSuchMessageException { 169 return this.messageSource.getMessage(code, args, locale); 170 } 171 172 /** 173 * Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance) 174 * in the default Locale. 175 * @param resolvable the MessageSourceResolvable 176 * @return the message 177 * @throws org.springframework.context.NoSuchMessageException if not found 178 */ 179 public String getMessage(MessageSourceResolvable resolvable) throws NoSuchMessageException { 180 return this.messageSource.getMessage(resolvable, getDefaultLocale()); 181 } 182 183 /** 184 * Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance) 185 * in the given Locale. 186 * @param resolvable the MessageSourceResolvable 187 * @param locale the Locale in which to do lookup 188 * @return the message 189 * @throws org.springframework.context.NoSuchMessageException if not found 190 */ 191 public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException { 192 return this.messageSource.getMessage(resolvable, locale); 193 } 194 195}