1 /**
2 * 2007, Digitalis Informatica. All rights reserved. Distribuicao e Gestao de Informatica, Lda. Estrada de Paco de Arcos
3 * num.9 - Piso -1 2780-666 Paco de Arcos Telefone: (351) 21 4408990 Fax: (351) 21 4408999 http://www.digitalis.pt
4 */
5
6 package pt.digitalis.dif.dem.objects.messages;
7
8 import java.util.HashMap;
9 import java.util.Map;
10
11 import pt.digitalis.dif.startup.DIFGeneralConfigurationParameters;
12 import pt.digitalis.dif.utils.ObjectFormatter;
13
14 /**
15 * The translations of a message
16 *
17 * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a><br/>
18 * @author Rodrigo Gonçalves <a href="mailto:rgoncalves@digitalis.pt">rgoncalves@digitalis.pt</a>
19 * @created Nov 2, 2007
20 */
21 public class MessageTranslations {
22
23 /** The translations */
24 private Map<String, Message> translations = new HashMap<String, Message>();
25
26 /**
27 * Adds a new translation to the map.
28 *
29 * @param language
30 * the language of the translation
31 * @param message
32 * the message object
33 * @return the updated object
34 */
35 public MessageTranslations addTranslation(String language, Message message)
36 {
37
38 language = processLanguage(language);
39
40 translations.put(language.toLowerCase(), message);
41
42 return this;
43 }
44
45 /**
46 * Returns a message translation for a given language.
47 *
48 * @param language
49 * the language of the translation
50 * @return the message of the given language
51 */
52 public Message getMessage(String language)
53 {
54
55 language = processLanguage(language);
56
57 Message message = translations.get(language);
58
59 if (message == null)
60 {
61 if (language != DIFGeneralConfigurationParameters.getInstance().getDefaultLanguage())
62 return translations.get(DIFGeneralConfigurationParameters.getInstance().getDefaultLanguage());
63 else
64 {
65 if (translations.keySet().size() > 0)
66 return (Message) translations.values().toArray()[0];
67 }
68 }
69
70 return message;
71 }
72
73 /**
74 * Returns all the translations for the message.
75 *
76 * @return the message translations
77 */
78 public Map<String, Message> getTranslations()
79 {
80 return this.translations;
81 }
82
83 /**
84 * Takes a language as argument and processes it according to the following rule: - if null was passed use DiF's
85 * default language - if not null the language is converted to lower-case
86 *
87 * @param language
88 * the language
89 * @return the lower-case version of the language or DiF's default language.
90 */
91 private String processLanguage(String language)
92 {
93 // If language wasn't pass, revert back to default
94 if (language == null)
95 return DIFGeneralConfigurationParameters.getInstance().getDefaultLanguage();
96 // ...else use the passed language lower case version
97 else
98 return language.toLowerCase();
99 }
100
101 /**
102 * @see java.lang.Object#toString()
103 */
104 @Override
105 public String toString()
106 {
107 ObjectFormatter formatter = new ObjectFormatter();
108
109 formatter.addItem("Translations", translations);
110
111 return formatter.getFormatedObject();
112 }
113 }