View Javadoc

1   /*
2    * $Id: Translator.java 2332 2010-07-31 13:01:17Z andrewinkler $
3    * ============================================================================
4    * Project awtools-lang
5    * Copyright (c) 2000-2010 by Andre Winkler. All rights reserved.
6    * ============================================================================
7    *          GNU LESSER GENERAL PUBLIC LICENSE
8    *  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
9    *
10   *  This library is free software; you can redistribute it and/or
11   *  modify it under the terms of the GNU Lesser General Public
12   *  License as published by the Free Software Foundation; either
13   *  version 2.1 of the License, or (at your option) any later version.
14   *
15   *  This library is distributed in the hope that it will be useful,
16   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   *  Lesser General Public License for more details.
19   *
20   *  You should have received a copy of the GNU Lesser General Public
21   *  License along with this library; if not, write to the Free Software
22   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23   *
24   */
25  
26  package de.awtools.lang;
27  
28  import java.text.MessageFormat;
29  import java.util.MissingResourceException;
30  import java.util.ResourceBundle;
31  
32  import org.apache.commons.lang.Validate;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  /**
37   * Liefert für einen Schlüssel einen Text. Dekoriert die Klasse
38   * <code>PropertyResourceBundle</code> um ein anderes Fehlerverhalten.
39   * Zusätzlich können Platzhalter in der Form <code>{0}</code> verarbeitet
40   * werden.
41   *
42   * @author  $Author: andrewinkler $
43   * @version $Revision: 2332 $ $Date: 2010-07-31 15:01:17 +0200 (Sa, 31 Jul 2010) $
44   */
45  public final class Translator {
46  
47      /** Der private Logger der Klasse. */
48      private final Logger log = LoggerFactory.getLogger(Translator.class);
49  
50      /** Das dekorierte ResourceBundle. */
51      private final ResourceBundle bundle;
52  
53      /**
54       * Erstellt ein ResourceBundle.
55       *
56       * @param value Das zu dekorierende <code>ResourceBundle</code>.
57       */
58      public Translator(final ResourceBundle value) {
59          Validate.notNull(value);
60          bundle = value;
61      }
62  
63      /**
64       * Liefert die Übersetzung für einen String.
65       *
66       * @param key Der gesuchte Schlüssel.
67       * @return Die Übersetzung.
68       */
69      public String getString(final String key) {
70          return (getString(key, new Object[0]));
71      }
72  
73      /**
74       * Liefert die Übersetzung für einen String. Das Array der
75       * <code>replaces</code> ersetzt alle Vorkommen von {0}...{n}.
76       *
77       * @param key Der Schlüssel.
78       * @param replaces Die Ersetzungen.
79       * @return Die Übersetzung.
80       */
81      public String getString(final String key, final Object... replaces) {
82          String text;
83  
84          try {
85              text = (bundle.getString(key));
86          } catch (MissingResourceException ex) {
87              log.debug(ex.getMessage());
88              text = key;
89          }
90  
91          if (replaces != null && replaces.length > 0) {
92              text = (MessageFormat.format(text, replaces));
93          }
94  
95          return text;
96      }
97  
98  }