001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.bean.validation; 018 019import java.util.Locale; 020 021import javax.validation.MessageInterpolator; 022 023import org.apache.wicket.Session; 024import org.apache.wicket.util.lang.Args; 025 026/** 027 * {@link MessageInterpolator} that adapts another to a locale from Wicket's {@link Session} 028 * 029 * @author igor 030 */ 031public class SessionLocaleInterpolator implements MessageInterpolator 032{ 033 private final MessageInterpolator delegate; 034 035 /** 036 * Constructor 037 * 038 * @param delegate 039 * the MessageInterpolator to delegate to 040 */ 041 public SessionLocaleInterpolator(MessageInterpolator delegate) 042 { 043 Args.notNull(delegate, "delegate"); 044 this.delegate = delegate; 045 } 046 047 @Override 048 public String interpolate(final String messageTemplate, 049 final MessageInterpolator.Context context) 050 { 051 final Locale locale = getLocale(); 052 if (locale != null) 053 { 054 return interpolate(messageTemplate, context, locale); 055 } 056 else 057 { 058 return delegate.interpolate(messageTemplate, context); 059 } 060 } 061 062 @Override 063 public String interpolate(final String message, final MessageInterpolator.Context context, 064 final Locale locale) 065 { 066 return delegate.interpolate(message, context, locale); 067 } 068 069 private Locale getLocale() 070 { 071 return Session.exists() ? Session.get().getLocale() : null; 072 } 073}