View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.service.impl;
24  
25  import java.util.Locale;
26  
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  import org.springframework.context.NoSuchMessageException;
30  import org.springframework.context.i18n.LocaleContextHolder;
31  import org.springframework.context.support.ReloadableResourceBundleMessageSource;
32  
33  import com.gisgraphy.service.IInternationalisationService;
34  
35  /**
36   * Allow i18n and L10n When we are not in a web context
37   * 
38   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
39   */
40  public class InternationalisationService implements IInternationalisationService {
41      
42      private Locale locale = LocaleContextHolder.getLocale();
43      
44      private ReloadableResourceBundleMessageSource resourceBundle;
45  
46      public static final Logger logger = LoggerFactory
47      .getLogger(InternationalisationService.class);
48     
49      
50      /* (non-Javadoc)
51       * @see com.gisgraphy.service.IInternationalisationService#getString(java.lang.String)
52       */
53      public String getString(String key) {
54         try {
55       return this.resourceBundle.getMessage(key, null,locale);
56      } catch (NoSuchMessageException e) {
57  	logger.warn("can not finfd message "+key+" in bundle");
58  	return key;
59      }
60         
61      }
62  
63      
64      /* (non-Javadoc)
65       * @see com.gisgraphy.service.IInternationalisationService#getString(java.lang.String, java.lang.Object[])
66       */
67      public String getString(String key, Object[] params) {
68  	 try {
69  	     return this.resourceBundle.getMessage(key, params, locale);
70  	    } catch (NoSuchMessageException e) {
71  		logger.warn("can not finfd message "+key+" in bundle");
72  		return key;
73  	    }
74          
75      }
76      
77     
78      /* (non-Javadoc)
79       * @see com.gisgraphy.service.IInternationalisationService#getLocale()
80       */
81      public Locale getLocale() {
82          return locale;
83      }
84  
85  
86      /* (non-Javadoc)
87       * @see com.gisgraphy.service.IInternationalisationService#setLocale(java.util.Locale)
88       */
89      public void setLocale(Locale locale) {
90          this.locale = locale;
91      }
92  
93      
94      public void setResourceBundle(ReloadableResourceBundleMessageSource resourceBundle) {
95          this.resourceBundle = resourceBundle;
96      }
97      
98  }