svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.i18n / utils / java / src / org / gvsig / i18n / utils / TranslationDatabase.java @ 40559
History | View | Annotate | Download (6.32 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
/**
|
25 |
*
|
26 |
*/
|
27 |
package org.gvsig.i18n.utils; |
28 |
|
29 |
import java.io.File; |
30 |
import java.io.FileInputStream; |
31 |
import java.io.FileNotFoundException; |
32 |
import java.io.FileOutputStream; |
33 |
import java.io.IOException; |
34 |
import java.util.ArrayList; |
35 |
import java.util.HashMap; |
36 |
import java.util.Iterator; |
37 |
import java.util.Set; |
38 |
|
39 |
/**
|
40 |
* @author cesar
|
41 |
*
|
42 |
*/
|
43 |
public class TranslationDatabase { |
44 |
|
45 |
public TranslationDatabase(ConfigOptions config){
|
46 |
this.config = config;
|
47 |
} |
48 |
|
49 |
private ConfigOptions config;
|
50 |
private HashMap dictionaries; |
51 |
|
52 |
public void load() { |
53 |
// always start with an empty HashMap when loading
|
54 |
dictionaries = new HashMap(); |
55 |
String lang;
|
56 |
DoubleProperties dictionary; |
57 |
|
58 |
FileInputStream stream=null; |
59 |
|
60 |
for (int currentLang=0; currentLang<config.languages.length; currentLang++) { |
61 |
lang = config.languages[currentLang]; |
62 |
dictionary = new DoubleProperties();
|
63 |
try {
|
64 |
stream = new FileInputStream(config.databaseDir+File.separator+config.defaultBaseName+"_"+lang+".properties"); |
65 |
try {
|
66 |
dictionary.load(stream); |
67 |
} catch (IOException e) { |
68 |
System.err.println("Error cargando la base de datos para el idioma: ["+lang+"]. "+e.getLocalizedMessage()); |
69 |
} |
70 |
} catch (FileNotFoundException e) { |
71 |
System.err.println("Error cargando la base de datos para el idioma: ["+lang+"]. "+e.getLocalizedMessage()); |
72 |
} |
73 |
dictionaries.put(lang, dictionary); |
74 |
} |
75 |
} |
76 |
|
77 |
public void save() { |
78 |
String lang;
|
79 |
DoubleProperties dictionary; |
80 |
|
81 |
FileOutputStream stream=null; |
82 |
|
83 |
for (int currentLang=0; currentLang<config.languages.length; currentLang++) { |
84 |
lang = config.languages[currentLang]; |
85 |
|
86 |
dictionary = ((DoubleProperties)dictionaries.get(lang)); |
87 |
|
88 |
try {
|
89 |
stream = new FileOutputStream(config.databaseDir+File.separator+config.defaultBaseName+"_"+lang+".properties"); |
90 |
try {
|
91 |
dictionary.store(stream, "Translations for language: " + lang);
|
92 |
} catch (IOException e) { |
93 |
System.err.println("Error guardando la base de datos para el idioma: ["+lang+"]. "+e.getLocalizedMessage()); |
94 |
} |
95 |
} catch (FileNotFoundException e) { |
96 |
System.err.println("Error guardando la base de datos para el idioma: ["+lang+"]. "+e.getLocalizedMessage()); |
97 |
} |
98 |
} |
99 |
} |
100 |
|
101 |
public String getTranslation(String lang, String key) { |
102 |
if (lang==null || key==null) return null; |
103 |
|
104 |
DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang); |
105 |
if (dictionary==null) return null; |
106 |
return (String) dictionary.get(key); |
107 |
} |
108 |
|
109 |
public String setTranslation(String lang, String key, String translation) { |
110 |
if (lang==null || key==null) return null; |
111 |
|
112 |
DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang); |
113 |
if (dictionary==null) return null; |
114 |
String oldvalue = (String) dictionary.get(key); |
115 |
dictionary.put(key, translation); |
116 |
return oldvalue;
|
117 |
} |
118 |
|
119 |
/**
|
120 |
* Removes the key from the specified dictionary, and its associated translation.
|
121 |
* It has no effect if the key was not present in the dictionary.
|
122 |
*
|
123 |
* @param lang The language from which the key should be removed.
|
124 |
* @param key The key to be removed.
|
125 |
* @return The translation associated with the key, or null if the
|
126 |
* key was not present in the dictionary. It also returns null if any of the parameters is
|
127 |
* null, or if there was no dictionary for the specified language.
|
128 |
*/
|
129 |
public String removeTranslation(String lang, String key) { |
130 |
if (lang==null || key==null) return null; |
131 |
|
132 |
DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang); |
133 |
if (dictionary==null) return null; |
134 |
String oldvalue = (String) dictionary.get(key); |
135 |
dictionary.remove(key); |
136 |
return oldvalue;
|
137 |
} |
138 |
|
139 |
/**
|
140 |
* Removes the key and its associated translation from all the dictionaries.
|
141 |
* The key will be deleted from the dictionaries in which it is present (if any).
|
142 |
*
|
143 |
* @param key The key to be removed.
|
144 |
* @return True if the key was removed from any dictionary, or false if the key
|
145 |
* was not present in any dictionary.
|
146 |
* @throws NullPointerException if the key is null.
|
147 |
*/
|
148 |
public boolean removeTranslation(String key) throws NullPointerException { |
149 |
DoubleProperties dictionary; |
150 |
String lang;
|
151 |
boolean present=false; |
152 |
|
153 |
Set keys = dictionaries.keySet();
|
154 |
Iterator langIterator = keys.iterator();
|
155 |
while (langIterator.hasNext()) {
|
156 |
lang = (String) langIterator.next();
|
157 |
dictionary = (DoubleProperties) dictionaries.get(lang); |
158 |
if (dictionary.containsKey(key)) {
|
159 |
present=true;
|
160 |
dictionary.remove(key); |
161 |
} |
162 |
} |
163 |
return present;
|
164 |
} |
165 |
|
166 |
public boolean containsLanguage(String lang) { |
167 |
return dictionaries.containsKey(lang);
|
168 |
} |
169 |
|
170 |
public boolean containsKey(String lang, String key) { |
171 |
if (lang==null || key==null) return false; |
172 |
|
173 |
DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang); |
174 |
return dictionary.containsKey(key);
|
175 |
} |
176 |
|
177 |
public String getAssociatedKey(String lang, String value) { |
178 |
if (lang==null || value==null) return null; |
179 |
|
180 |
DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang); |
181 |
return dictionary.getAssociatedKey(value);
|
182 |
} |
183 |
|
184 |
public ArrayList getAssociatedKeys(String lang, String value) { |
185 |
if (lang==null || value==null) return null; |
186 |
|
187 |
DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang); |
188 |
return dictionary.getAssociatedKeys(value);
|
189 |
} |
190 |
|
191 |
public boolean containsTranslation(String lang, String translation) { |
192 |
if (lang==null || translation==null) return false; |
193 |
|
194 |
DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang); |
195 |
return dictionary.containsValue(translation);
|
196 |
} |
197 |
} |