| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| Messages |
|
| 1.6875;1.688 |
| 1 | package com.smartwerkz.jupload.classic.config; |
|
| 2 | ||
| 3 | import java.io.IOException; |
|
| 4 | import java.net.URL; |
|
| 5 | import java.text.MessageFormat; |
|
| 6 | import java.util.Locale; |
|
| 7 | import java.util.Properties; |
|
| 8 | ||
| 9 | import com.smartwerkz.jupload.classic.JUpload; |
|
| 10 | import com.smartwerkz.jupload.classic.util.debug.Debug; |
|
| 11 | ||
| 12 | /** |
|
| 13 | * Manages Messages resources for internationalisation. |
|
| 14 | * |
|
| 15 | * Specialty is that it does not search for properties files on the server side, |
|
| 16 | * outside of the jar file. |
|
| 17 | * |
|
| 18 | * Plugins can use the addMessage methods to read messages from custom URLs. |
|
| 19 | * Every custom URL will be added to the {@link #propURLs} - list. |
|
| 20 | * |
|
| 21 | * |
|
| 22 | * |
|
| 23 | * @author Mike Haller |
|
| 24 | * @author Dominik Seifert |
|
| 25 | * @since JUpload v0.70 (20.10.2003 09:41:31) |
|
| 26 | */ |
|
| 27 | 0 | public class Messages { |
| 28 | private final static String DEFAULT_PATH = "language"; |
|
| 29 | 0 | private final static String DEFAULT_PREFIX = "jupload"; |
| 30 | ||
| 31 | private final static URL DEFAULT_URL; |
|
| 32 | ||
| 33 | private static Properties props, defaults; |
|
| 34 | ||
| 35 | public static URL propURL; |
|
| 36 | ||
| 37 | ||
| 38 | ||
| 39 | static { |
|
| 40 | 0 | DEFAULT_URL = getResourceURL(JUpload.class, DEFAULT_PATH, DEFAULT_PREFIX, null); |
| 41 | ||
| 42 | 0 | props = new Properties(); |
| 43 | 0 | defaults = new Properties(); |
| 44 | 0 | |
| 45 | 0 | reload(); |
| 46 | 0 | } |
| 47 | 0 | |
| 48 | /** |
|
| 49 | 0 | * @param key |
| 50 | 0 | * The identifier key for the localized string |
| 51 | * @return Returns the localized string from the jupload.properties file |
|
| 52 | */ |
|
| 53 | public static String get(String key) { |
|
| 54 | 0 | String pattern = props.getProperty(key); |
| 55 | 0 | if (pattern == null) { |
| 56 | 0 | pattern = defaults.getProperty(key); |
| 57 | 0 | if (pattern == null) { |
| 58 | 0 | pattern = "Could not localize message [" + key + "]"; |
| 59 | 0 | } |
| 60 | 0 | } |
| 61 | 0 | return pattern; |
| 62 | 0 | } |
| 63 | ||
| 64 | /** |
|
| 65 | 0 | * Liest einen formatierten String aus den locale-Dateien. Formatierungen |
| 66 | * werden mit "{0}", "{1}" etc. eingesetzt. |
|
| 67 | * |
|
| 68 | * @param key |
|
| 69 | * der Bezeichner in der Properties-Datei |
|
| 70 | * @param replacements |
|
| 71 | * eine Liste mit den Werten f???r die Ersetzungen |
|
| 72 | * @return den neu formatierten Text mit den eingef???gten Werten |
|
| 73 | */ |
|
| 74 | public static String get(String key, Object[] replacements) { |
|
| 75 | 0 | String pattern = get(key); |
| 76 | 0 | return MessageFormat.format(pattern, replacements); |
| 77 | } |
|
| 78 | ||
| 79 | 0 | |
| 80 | 0 | /** |
| 81 | * Liest einen formatierten String aus den locale-Dateien. Formatierungen |
|
| 82 | * werden mit "{0}", "{1}" etc. eingesetzt. |
|
| 83 | * |
|
| 84 | * @param key The key for this message |
|
| 85 | * @param replacements a parameter-list that should replace default values |
|
| 86 | * |
|
| 87 | * @return The translated message with the given key and replacements inserted. |
|
| 88 | */ |
|
| 89 | public static String get(String key, Object replacement) { |
|
| 90 | 0 | return get(key, new Object[] { replacement }); |
| 91 | } |
|
| 92 | ||
| 93 | public static String get(String key, Object replacement1, Object replacement2) { |
|
| 94 | 0 | return get(key, new Object[] { replacement1, replacement2 }); |
| 95 | } |
|
| 96 | ||
| 97 | public static String get(String key, Object replacement1, Object replacement2, Object replacement3) { |
|
| 98 | 0 | return get(key, new Object[] { replacement1, replacement2, replacement3 }); |
| 99 | } |
|
| 100 | ||
| 101 | public static void setLanguage(String language, String country) { |
|
| 102 | 0 | setLocale(new Locale(language, country)); |
| 103 | 0 | } |
| 104 | ||
| 105 | /** |
|
| 106 | * Sets a new locale (language and country) for use with UI messages and |
|
| 107 | * internationalization. |
|
| 108 | * |
|
| 109 | * @param locale |
|
| 110 | * the new Locale to use |
|
| 111 | */ |
|
| 112 | public static void setLocale(Locale locale) { |
|
| 113 | 0 | Locale.setDefault(locale); |
| 114 | 0 | propURL = getResourceURL(Messages.class, DEFAULT_PATH, DEFAULT_PREFIX); |
| 115 | 0 | reload(); |
| 116 | 0 | } |
| 117 | ||
| 118 | /** |
|
| 119 | * Reload the default messages with the current locale settings. |
|
| 120 | */ |
|
| 121 | 0 | public static void reload() { |
| 122 | 0 | load(props, defaults, DEFAULT_URL); |
| 123 | 0 | } |
| 124 | ||
| 125 | ||
| 126 | /** |
|
| 127 | * Fill the defaults and props Properties with information |
|
| 128 | 0 | * from the given urls. |
| 129 | 0 | */ |
| 130 | 0 | public static void load(Properties defaults, Properties props, |
| 131 | 0 | URL defaultURL) { |
| 132 | 0 | props.clear(); |
| 133 | 0 | defaults.clear(); |
| 134 | 0 | |
| 135 | 0 | loadMessages(defaults, defaultURL); |
| 136 | ||
| 137 | 0 | URL from = getResourceURL(JUpload.class, DEFAULT_PATH, DEFAULT_PREFIX); |
| 138 | 0 | if (from != null) |
| 139 | 0 | fetchMessages(defaults, from); |
| 140 | ||
| 141 | 0 | if (propURL != null) |
| 142 | 0 | loadMessages(props, propURL); |
| 143 | 0 | } |
| 144 | 0 | |
| 145 | 0 | /** |
| 146 | * Adds further messages from a properties file from the given class' |
|
| 147 | 0 | * directory specified through: |
| 148 | * path/name_LANGUAGE_COUNTRY.<br> |
|
| 149 | 0 | * <br> |
| 150 | 0 | * <code> |
| 151 | 0 | * fetchMessages(MyPlugin.class, "myplugin.messages", "names", Locale.JAPAN); |
| 152 | * </code><br> |
|
| 153 | 0 | * <br> |
| 154 | 0 | * will add all messages from a file at:<br> |
| 155 | 0 | * /myplugin/messages/names_ja_JP.properties<br> |
| 156 | 0 | * <br> |
| 157 | * <code> |
|
| 158 | 0 | * fetchMessages(MyPlugin.class, "myplugin.messages", "names", null); |
| 159 | * </code><br> |
|
| 160 | * <br> |
|
| 161 | * will add all messages from a file at:<br> |
|
| 162 | * /myplugin/messages/names.properties |
|
| 163 | * |
|
| 164 | * @return Wether the resource could be loaded |
|
| 165 | */ |
|
| 166 | public static boolean addMessages(Class cl, String path, String name, Locale locale) { |
|
| 167 | 0 | return fetchMessages(props, cl, path, name, locale); |
| 168 | } |
|
| 169 | public static boolean fetchMessages(Properties props, Class cl, String path, String name, |
|
| 170 | Locale locale) { |
|
| 171 | 0 | return fetchMessages(props, getResourceURL(cl, path, name, locale)); |
| 172 | } |
|
| 173 | ||
| 174 | /** |
|
| 175 | * Add messages from a file with the pattern:<br> |
|
| 176 | * path/name_language_country.properties<br> |
|
| 177 | * or:<br> |
|
| 178 | * path/name.properties (in case that language is null)<br> |
|
| 179 | * or:<br> |
|
| 180 | * path/name_language.properties (in case that country is null)<br> |
|
| 181 | * <br> |
|
| 182 | 0 | * relative to the JUpload class' package or to the root |
| 183 | * if path is prefixed with a / |
|
| 184 | * |
|
| 185 | */ |
|
| 186 | 0 | public static boolean fetchMessages(Properties props, URL from) { |
| 187 | 0 | if (from == null) { |
| 188 | 0 | return false; |
| 189 | } |
|
| 190 | ||
| 191 | 0 | loadMessages(props, from); |
| 192 | 0 | return true; |
| 193 | } |
|
| 194 | ||
| 195 | private static void loadMessages(Properties props, URL from) { |
|
| 196 | 0 | if (from != null) |
| 197 | try { |
|
| 198 | 0 | props.load(from.openStream()); |
| 199 | } |
|
| 200 | 0 | catch (IOException e) { |
| 201 | 0 | Debug.debug(e); |
| 202 | 0 | } |
| 203 | 0 | } |
| 204 | ||
| 205 | 0 | public static URL getResourceURL(Class c, String path, String name) { |
| 206 | 0 | return getResourceURL(c, path, name, Locale.getDefault()); |
| 207 | 0 | } |
| 208 | ||
| 209 | public static URL getResourceURL(Class c, String path, String name, Locale locale) { |
|
| 210 | 0 | path = path != null ? path + "/" : ""; |
| 211 | 0 | path += name; |
| 212 | ||
| 213 | 0 | URL url = null; |
| 214 | 0 | if (locale != null) { |
| 215 | 0 | String language = locale.getLanguage(); |
| 216 | 0 | String country = locale.getCountry(); |
| 217 | 0 | |
| 218 | 0 | url = getURL(c, path, language, country); |
| 219 | 0 | if (url == null && country != null) { |
| 220 | 0 | url = getURL(c, path, language, null); |
| 221 | 0 | } |
| 222 | } |
|
| 223 | ||
| 224 | 0 | if (url == null) { |
| 225 | 0 | url = getURL(c, path, null, null); |
| 226 | } |
|
| 227 | ||
| 228 | 0 | return url; |
| 229 | } |
|
| 230 | 0 | |
| 231 | 0 | public static URL getURL(Class clazz, String where, String language, String country) { |
| 232 | 0 | language = (language != null ? ("_" + language) : ""); |
| 233 | 0 | country = (country != null ? ("_" + country) : ""); |
| 234 | 0 | |
| 235 | 0 | return clazz.getResource(where + language + country + ".properties"); |
| 236 | 0 | } |
| 237 | ||
| 238 | 0 | } |