001/*
002 * Copyright 2002-2017 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;
025
026/**
027 * Helper class for easy access to messages from a MessageSource,
028 * providing various overloaded getMessage methods.
029 *
030 * <p>Available from ApplicationObjectSupport, but also reusable
031 * as a standalone helper to delegate to in application objects.
032 *
033 * @author Juergen Hoeller
034 * @since 23.10.2003
035 * @see ApplicationObjectSupport#getMessageSourceAccessor
036 */
037public class MessageSourceAccessor {
038
039        private final MessageSource messageSource;
040
041        private final Locale defaultLocale;
042
043
044        /**
045         * Create a new MessageSourceAccessor, using LocaleContextHolder's locale
046         * as default locale.
047         * @param messageSource the MessageSource to wrap
048         * @see org.springframework.context.i18n.LocaleContextHolder#getLocale()
049         */
050        public MessageSourceAccessor(MessageSource messageSource) {
051                this.messageSource = messageSource;
052                this.defaultLocale = null;
053        }
054
055        /**
056         * Create a new MessageSourceAccessor, using the given default locale.
057         * @param messageSource the MessageSource to wrap
058         * @param defaultLocale the default locale to use for message access
059         */
060        public MessageSourceAccessor(MessageSource messageSource, Locale defaultLocale) {
061                this.messageSource = messageSource;
062                this.defaultLocale = defaultLocale;
063        }
064
065
066        /**
067         * Return the default locale to use if no explicit locale has been given.
068         * <p>The default implementation returns the default locale passed into the
069         * corresponding constructor, or LocaleContextHolder's locale as fallback.
070         * Can be overridden in subclasses.
071         * @see #MessageSourceAccessor(org.springframework.context.MessageSource, java.util.Locale)
072         * @see org.springframework.context.i18n.LocaleContextHolder#getLocale()
073         */
074        protected Locale getDefaultLocale() {
075                return (this.defaultLocale != null ? this.defaultLocale : LocaleContextHolder.getLocale());
076        }
077
078        /**
079         * Retrieve the message for the given code and the default Locale.
080         * @param code code of the message
081         * @param defaultMessage String to return if the lookup fails
082         * @return the message
083         */
084        public String getMessage(String code, String defaultMessage) {
085                return this.messageSource.getMessage(code, null, defaultMessage, getDefaultLocale());
086        }
087
088        /**
089         * Retrieve the message for the given code and the given Locale.
090         * @param code code of the message
091         * @param defaultMessage String to return if the lookup fails
092         * @param locale Locale in which to do lookup
093         * @return the message
094         */
095        public String getMessage(String code, String defaultMessage, Locale locale) {
096                return this.messageSource.getMessage(code, null, defaultMessage, locale);
097        }
098
099        /**
100         * Retrieve the message for the given code and the default Locale.
101         * @param code code of the message
102         * @param args arguments for the message, or {@code null} if none
103         * @param defaultMessage String to return if the lookup fails
104         * @return the message
105         */
106        public String getMessage(String code, Object[] args, String defaultMessage) {
107                return this.messageSource.getMessage(code, args, defaultMessage, getDefaultLocale());
108        }
109
110        /**
111         * Retrieve the message for the given code and the given Locale.
112         * @param code code of the message
113         * @param args arguments for the message, or {@code null} if none
114         * @param defaultMessage String to return if the lookup fails
115         * @param locale Locale in which to do lookup
116         * @return the message
117         */
118        public String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
119                return this.messageSource.getMessage(code, args, defaultMessage, locale);
120        }
121
122        /**
123         * Retrieve the message for the given code and the default Locale.
124         * @param code code of the message
125         * @return the message
126         * @throws org.springframework.context.NoSuchMessageException if not found
127         */
128        public String getMessage(String code) throws NoSuchMessageException {
129                return this.messageSource.getMessage(code, null, getDefaultLocale());
130        }
131
132        /**
133         * Retrieve the message for the given code and the given Locale.
134         * @param code code of the message
135         * @param locale Locale in which to do lookup
136         * @return the message
137         * @throws org.springframework.context.NoSuchMessageException if not found
138         */
139        public String getMessage(String code, Locale locale) throws NoSuchMessageException {
140                return this.messageSource.getMessage(code, null, locale);
141        }
142
143        /**
144         * Retrieve the message for the given code and the default Locale.
145         * @param code code of the message
146         * @param args arguments for the message, or {@code null} if none
147         * @return the message
148         * @throws org.springframework.context.NoSuchMessageException if not found
149         */
150        public String getMessage(String code, Object[] args) throws NoSuchMessageException {
151                return this.messageSource.getMessage(code, args, getDefaultLocale());
152        }
153
154        /**
155         * Retrieve the message for the given code and the given Locale.
156         * @param code code of the message
157         * @param args arguments for the message, or {@code null} if none
158         * @param locale Locale in which to do lookup
159         * @return the message
160         * @throws org.springframework.context.NoSuchMessageException if not found
161         */
162        public String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException {
163                return this.messageSource.getMessage(code, args, locale);
164        }
165
166        /**
167         * Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance)
168         * in the default Locale.
169         * @param resolvable the MessageSourceResolvable
170         * @return the message
171         * @throws org.springframework.context.NoSuchMessageException if not found
172         */
173        public String getMessage(MessageSourceResolvable resolvable) throws NoSuchMessageException {
174                return this.messageSource.getMessage(resolvable, getDefaultLocale());
175        }
176
177        /**
178         * Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance)
179         * in the given Locale.
180         * @param resolvable the MessageSourceResolvable
181         * @param locale Locale in which to do lookup
182         * @return the message
183         * @throws org.springframework.context.NoSuchMessageException if not found
184         */
185        public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
186                return this.messageSource.getMessage(resolvable, locale);
187        }
188
189}