View Javadoc

1   /***
2    * Jetrix TetriNET Server
3    * Copyright (C) 2001-2003  Emmanuel Bourg
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  
20  package net.jetrix.messages.channel;
21  
22  import java.util.HashMap;
23  import java.util.Locale;
24  import java.util.Map;
25  
26  import net.jetrix.Language;
27  import net.jetrix.Protocol;
28  import net.jetrix.Server;
29  
30  /***
31   * A generic internationalized text message.
32   *
33   * @author Emmanuel Bourg
34   * @version $Revision: 860 $, $Date: 2010-05-06 13:21:05 +0200 (jeu., 06 mai 2010) $
35   */
36  public abstract class TextMessage extends ChannelMessage
37  {
38      private String text;
39      private String key;
40      private Object params[];
41      private Map<Locale, String> texts;
42      private Map<Protocol, Map<Locale, String>> rawMessages;
43  
44      /***
45       * Return the text of this message using the default server locale.
46       */
47      public String getText()
48      {
49          if (key == null)
50          {
51              return text;
52          }
53          else
54          {
55              Locale defaultLocale;
56              if (Server.getInstance() != null)
57              {
58                  // get the server locale configured in config.xml
59                  defaultLocale = Server.getInstance().getConfig().getLocale();
60              }
61              else
62              {
63                  // get the default system locale
64                  defaultLocale = Locale.getDefault();
65              }
66  
67              return getText(defaultLocale);
68          }
69      }
70  
71      /***
72       * Return the text of this message using the specified locale.
73       */
74      public String getText(Locale locale)
75      {
76          if (key == null)
77          {
78              return text;
79          }
80          else
81          {
82              if (texts == null)
83              {
84                  texts = new HashMap<Locale, String>();
85              }
86  
87              String s = texts.get(locale);
88  
89              if (s == null)
90              {
91                  s = Language.getText(key, locale, params);
92                  texts.put(locale, s);
93              }
94  
95              return s;
96          }
97      }
98  
99      /***
100      * Set the text of the message (locale independant).
101      */
102     public void setText(String text)
103     {
104         this.text = text;
105         this.key = null;
106     }
107 
108     /***
109      * Return the key of the message.
110      */
111     public String getKey()
112     {
113         return key;
114     }
115 
116     /***
117      * Set the key and the parameters of the message for internationalized
118      * text messages.
119      */
120     public void setKey(String key, Object... params)
121     {
122         this.key = key;
123         this.params = params;
124     }
125 
126     /***
127      * Return the parameters of the message.
128      */
129     public Object[] getParams()
130     {
131         return params;
132     }
133 
134     /***
135      * Set the parameters of the message.
136      */
137     public void setParams(Object... params)
138     {
139         this.params = params;
140     }
141 
142     public String getRawMessage(Protocol protocol, Locale locale)
143     {
144         if (key == null)
145         {
146             // use the default caching method for non localized messages
147             return super.getRawMessage(protocol, locale);
148         }
149         else
150         {
151             // use the caching on the (protocol, locale) combo
152             if (rawMessages == null)
153             {
154                 rawMessages = new HashMap<Protocol, Map<Locale, String>>();
155             }
156 
157             Map<Locale, String> i18nMessages = rawMessages.get(protocol);
158             if (i18nMessages == null)
159             {
160                 i18nMessages = new HashMap<Locale, String>();
161                 rawMessages.put(protocol, i18nMessages);
162             }
163 
164             String cachedMessage = i18nMessages.get(locale);
165             if (cachedMessage == null)
166             {
167                 cachedMessage = protocol.translate(this, locale);
168                 i18nMessages.put(locale, cachedMessage);
169             }
170 
171             return cachedMessage;
172         }
173     }
174 
175 }