    001// Copyright 2009 The Apache Software Foundation
    002//
    003// Licensed under the Apache License, Version 2.0 (the "License");
    004// you may not use this file except in compliance with the License.
    005// You may obtain a copy of the License at
    006//
    007//     http://www.apache.org/licenses/LICENSE-2.0
    008//
    009// Unless required by applicable law or agreed to in writing, software
    010// distributed under the License is distributed on an "AS IS" BASIS,
    011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    012// See the License for the specific language governing permissions and
    013// limitations under the License.
    014package org.apache.tapestry5.internal.beanvalidator;
    015
    016import java.util.Locale;
    017
    018import javax.validation.MessageInterpolator;
    019
    020import org.apache.tapestry5.ioc.services.ThreadLocale;
    021/**
    022 * The default message interpolation algorithm uses {@link Locale#getDefault()}. This behavior is not appropriate for Tapestry applications,
    023 * thus we need a {@link Locale} aware message interpolator.
    024 */
    025public class MessageInterpolatorImpl implements MessageInterpolator
    026{
    027        private final MessageInterpolator delegate;
    028        private final ThreadLocale threadLocale;
    029
    030        public MessageInterpolatorImpl(MessageInterpolator delegate, ThreadLocale threadLocale)
    031        {
    032                this.delegate = delegate;
    033                this.threadLocale = threadLocale;
    034        }
    035
    036        /**
    037         * @see javax.validation.MessageInterpolator#interpolate(java.lang.String, javax.validation.MessageInterpolator.Context)
    038         */
    039        @Override
    040        public String interpolate(String messageTemplate, Context context)
    041        {
    042                return interpolate(messageTemplate, context, threadLocale.getLocale());
    043        }
    044
    045        /**
    046         * @see javax.validation.MessageInterpolator#interpolate(java.lang.String, javax.validation.MessageInterpolator.Context, java.util.Locale)
    047         */
    048        @Override
    049        public String interpolate(String messageTemplate, Context context, Locale locale)
    050        {
    051                return this.delegate.interpolate(messageTemplate, context, locale);
    052        }
    053
    054}