| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
package org.crosswire.common.util; |
| 21 | |
|
| 22 | |
import java.io.IOException; |
| 23 | |
import java.net.URI; |
| 24 | |
import java.net.URL; |
| 25 | |
import java.util.Locale; |
| 26 | |
import java.util.Map; |
| 27 | |
|
| 28 | |
import org.crosswire.common.config.ChoiceFactory; |
| 29 | |
import org.slf4j.LoggerFactory; |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
public final class Translations { |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | 0 | private Translations() { |
| 43 | |
try { |
| 44 | 0 | loadSupportedTranslations(); |
| 45 | 0 | PropertyMap props = ResourceUtil.getProperties(getClass()); |
| 46 | 0 | translation = props.get(TRANSLATION_KEY); |
| 47 | 0 | if (translation == null || translation.length() == 0) { |
| 48 | |
|
| 49 | |
|
| 50 | 0 | for (int i = 0; i < translations.length; i++) { |
| 51 | 0 | Locale supportedLocale = new Locale(translations[i]); |
| 52 | 0 | if (supportedLocale.getLanguage().equals(originalLocale.getLanguage()) && supportedLocale.getCountry().equals(originalLocale.getCountry())) { |
| 53 | 0 | translation = translations[i]; |
| 54 | 0 | return; |
| 55 | |
} |
| 56 | |
} |
| 57 | |
|
| 58 | |
|
| 59 | 0 | for (int i = 0; i < translations.length; i++) { |
| 60 | 0 | Locale supportedLocale = new Locale(translations[i]); |
| 61 | 0 | if (supportedLocale.getLanguage().equals(originalLocale.getLanguage())) { |
| 62 | 0 | translation = translations[i]; |
| 63 | 0 | return; |
| 64 | |
} |
| 65 | |
} |
| 66 | |
|
| 67 | |
|
| 68 | 0 | translation = DEFAULT_TRANSLATION; |
| 69 | |
} |
| 70 | 0 | } catch (IOException e) { |
| 71 | 0 | translation = DEFAULT_TRANSLATION; |
| 72 | 0 | } |
| 73 | 0 | } |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
public static Translations instance() { |
| 81 | 0 | return instance; |
| 82 | |
} |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
public PropertyMap getSupported() { |
| 90 | 0 | loadSupportedTranslations(); |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | 0 | PropertyMap names = new PropertyMap(); |
| 95 | |
|
| 96 | 0 | for (int i = 0; i < translations.length; i++) { |
| 97 | 0 | names.put(translations[i], toString(translations[i])); |
| 98 | |
} |
| 99 | |
|
| 100 | 0 | return names; |
| 101 | |
} |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
public Locale getCurrentLocale() { |
| 109 | |
|
| 110 | |
|
| 111 | 0 | if (translation == null || DEFAULT_TRANSLATION.equals(translation)) { |
| 112 | 0 | return DEFAULT_LOCALE; |
| 113 | |
} |
| 114 | |
|
| 115 | |
|
| 116 | 0 | if (translation.indexOf('_') != -1) { |
| 117 | 0 | String[] locale = StringUtil.split(translation, '_'); |
| 118 | 0 | return new Locale(locale[0], locale[1]); |
| 119 | |
} |
| 120 | |
|
| 121 | |
|
| 122 | 0 | return new Locale(translation); |
| 123 | |
} |
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
public String getCurrent() { |
| 131 | 0 | return toString(translation); |
| 132 | |
} |
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
public void setCurrent(String newTranslation) { |
| 141 | 0 | String found = DEFAULT_TRANSLATION; |
| 142 | 0 | String currentTranslation = ""; |
| 143 | 0 | for (int i = 0; i < translations.length; i++) { |
| 144 | 0 | String trans = translations[i]; |
| 145 | 0 | currentTranslation = toString(translation); |
| 146 | |
|
| 147 | 0 | if (trans.equals(newTranslation) || currentTranslation.equals(newTranslation)) { |
| 148 | 0 | found = trans; |
| 149 | 0 | break; |
| 150 | |
} |
| 151 | |
} |
| 152 | |
|
| 153 | |
try { |
| 154 | 0 | translation = found; |
| 155 | 0 | PropertyMap props = new PropertyMap(); |
| 156 | 0 | if (!DEFAULT_TRANSLATION.equals(translation)) { |
| 157 | 0 | props.put(TRANSLATION_KEY, translation); |
| 158 | |
} |
| 159 | |
|
| 160 | 0 | URI outputURI = CWProject.instance().getWritableURI(getClass().getName(), FileUtil.EXTENSION_PROPERTIES); |
| 161 | 0 | NetUtil.storeProperties(props, outputURI, "BibleDesktop UI Translation"); |
| 162 | 0 | } catch (IOException ex) { |
| 163 | 0 | log.error("Failed to save BibleDesktop UI Translation", ex); |
| 164 | 0 | } |
| 165 | 0 | } |
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
public void setLocale() { |
| 175 | 0 | Locale.setDefault(getCurrentLocale()); |
| 176 | 0 | } |
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
public void register() { |
| 182 | 0 | ChoiceFactory.getDataMap().put(TRANSLATION_KEY, getSupportedTranslations()); |
| 183 | 0 | } |
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
public static String getCurrentTranslation() { |
| 191 | 0 | return Translations.instance().getCurrent(); |
| 192 | |
} |
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
public static void setCurrentTranslation(String newTranslation) { |
| 201 | 0 | Translations.instance().setCurrent(newTranslation); |
| 202 | 0 | } |
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
public static Map<String, String> getSupportedTranslations() { |
| 210 | 0 | return Translations.instance().getSupported(); |
| 211 | |
} |
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
private void loadSupportedTranslations() { |
| 217 | 0 | if (translations == null) { |
| 218 | |
try { |
| 219 | 0 | URL index = ResourceUtil.getResource(Translations.class, "translations.txt"); |
| 220 | 0 | translations = NetUtil.listByIndexFile(NetUtil.toURI(index)); |
| 221 | 0 | } catch (IOException ex) { |
| 222 | 0 | translations = new String[0]; |
| 223 | 0 | } |
| 224 | |
} |
| 225 | 0 | } |
| 226 | |
|
| 227 | |
public String toString(String translationCode) { |
| 228 | 0 | StringBuilder currentTranslation = new StringBuilder(Languages.getName(translationCode)); |
| 229 | |
|
| 230 | 0 | if (translationCode.indexOf('_') != -1) { |
| 231 | 0 | String[] locale = StringUtil.split(translationCode, '_'); |
| 232 | 0 | currentTranslation.append(", "); |
| 233 | 0 | currentTranslation.append(Countries.getCountry(locale[1])); |
| 234 | |
} |
| 235 | |
|
| 236 | 0 | return currentTranslation.toString(); |
| 237 | |
} |
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
private static final String TRANSLATION_KEY = "translation-codes"; |
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
public static final String DEFAULT_TRANSLATION = "en"; |
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | 0 | public static final Locale DEFAULT_LOCALE = Locale.ENGLISH; |
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
private String translation; |
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
private String[] translations; |
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | 0 | private static Locale originalLocale = Locale.getDefault(); |
| 269 | |
|
| 270 | 0 | private static Translations instance = new Translations(); |
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | 0 | private static final org.slf4j.Logger log = LoggerFactory.getLogger(Translations.class); |
| 276 | |
} |