| 1 | package be.nikiroo.jvcard.resources.bundles; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.io.IOException; |
| 5 | import java.io.Writer; |
| 6 | import java.util.Locale; |
| 7 | |
| 8 | import be.nikiroo.jvcard.resources.Bundles; |
| 9 | import be.nikiroo.jvcard.resources.Bundles.Bundle; |
| 10 | import be.nikiroo.jvcard.resources.Bundles.Target; |
| 11 | import be.nikiroo.jvcard.resources.enums.StringId; |
| 12 | |
| 13 | /** |
| 14 | * This class manages the translation of {@link TransBundle.StringId}s into |
| 15 | * user-understandable text. |
| 16 | * |
| 17 | * @author niki |
| 18 | * |
| 19 | */ |
| 20 | public class TransBundle extends Bundle<StringId> { |
| 21 | private boolean utf = true; |
| 22 | private Locale locale; |
| 23 | |
| 24 | /** |
| 25 | * Create a translation service with the default language. |
| 26 | */ |
| 27 | public TransBundle() { |
| 28 | new Bundles().super(StringId.class, Target.resources); |
| 29 | setLanguage(null); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Create a translation service for the given language. (Will fall back to |
| 34 | * the default one i not found.) |
| 35 | * |
| 36 | * @param language |
| 37 | * the language to use |
| 38 | */ |
| 39 | public TransBundle(String language) { |
| 40 | new Bundles().super(StringId.class, Target.resources); |
| 41 | |
| 42 | setLanguage(language); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Translate the given {@link StringId} into user text. |
| 47 | * |
| 48 | * @param stringId |
| 49 | * the ID to translate |
| 50 | * @param values |
| 51 | * the values to insert instead of the place holders in the |
| 52 | * translation |
| 53 | * |
| 54 | * @return the translated text with the given value where required |
| 55 | */ |
| 56 | public String getString(StringId stringId, Object... values) { |
| 57 | StringId id = stringId; |
| 58 | String result = ""; |
| 59 | |
| 60 | if (!isUnicode()) { |
| 61 | try { |
| 62 | id = StringId.valueOf(stringId.name() + "_NOUTF"); |
| 63 | } catch (IllegalArgumentException iae) { |
| 64 | // no special _NOUTF version found |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if (id == StringId.NULL) { |
| 69 | result = ""; |
| 70 | } else if (id == StringId.DUMMY) { |
| 71 | result = "[dummy]"; |
| 72 | } else if (map.containsKey(id.name())) { |
| 73 | result = map.getString(id.name()); |
| 74 | } else { |
| 75 | result = id.toString(); |
| 76 | } |
| 77 | |
| 78 | if (values != null && values.length > 0) |
| 79 | return String.format(locale, result, values); |
| 80 | else |
| 81 | return result; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Check if unicode characters should be used. |
| 86 | * |
| 87 | * @return TRUE to allow unicode |
| 88 | */ |
| 89 | public boolean isUnicode() { |
| 90 | return utf; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Allow or disallow unicode characters in the program. |
| 95 | * |
| 96 | * @param utf |
| 97 | * TRUE to allow unuciode, FALSE to only allow ASCII characters |
| 98 | */ |
| 99 | public void setUnicode(boolean utf) { |
| 100 | this.utf = utf; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Initialise the translation mappings for the given language. |
| 105 | * |
| 106 | * @param language |
| 107 | * the language to initialise, in the form "en-GB" or "fr" for |
| 108 | * instance |
| 109 | */ |
| 110 | private void setLanguage(String language) { |
| 111 | locale = getLocaleFor(language); |
| 112 | map = getBundle(Target.resources, locale); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public String getString(StringId id) { |
| 117 | return getString(id, (Object[]) null); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | protected File getUpdateFile(String path) { |
| 122 | String code = locale.toString(); |
| 123 | File file = null; |
| 124 | if (code.length() > 0) { |
| 125 | file = new File(path, name.name() + "_" + code + ".properties"); |
| 126 | } else { |
| 127 | // Default properties file: |
| 128 | file = new File(path, name.name() + ".properties"); |
| 129 | } |
| 130 | |
| 131 | return file; |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | protected void writeHeader(Writer writer) throws IOException { |
| 136 | String code = locale.toString(); |
| 137 | String name = locale.getDisplayCountry(locale); |
| 138 | |
| 139 | if (name.length() == 0) |
| 140 | name = locale.getDisplayLanguage(locale); |
| 141 | if (name.length() == 0) |
| 142 | name = "default"; |
| 143 | |
| 144 | if (code.length() > 0) { |
| 145 | name = name + " (" + code + ")"; |
| 146 | } |
| 147 | |
| 148 | StringId.writeHeader(writer, name); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | protected void writeValue(Writer writer, StringId id) throws IOException { |
| 153 | super.writeValue(writer, id); |
| 154 | |
| 155 | String name = id.name() + "_NOUTF"; |
| 156 | if (map.containsKey(name)) { |
| 157 | String value = map.getString(name).trim(); |
| 158 | writeValue(writer, name, value); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Return the {@link Locale} representing the given language. |
| 164 | * |
| 165 | * @param language |
| 166 | * the language to initialise, in the form "en-GB" or "fr" for |
| 167 | * instance |
| 168 | * |
| 169 | * @return the corresponding {@link Locale} or the default {@link Locale} if |
| 170 | * it is not known |
| 171 | */ |
| 172 | static private Locale getLocaleFor(String language) { |
| 173 | Locale locale; |
| 174 | |
| 175 | if (language == null) { |
| 176 | locale = Locale.getDefault(); |
| 177 | } else { |
| 178 | language = language.replaceAll("_", "-"); |
| 179 | String lang = language; |
| 180 | String country = null; |
| 181 | if (language.contains("-")) { |
| 182 | lang = language.split("-")[0]; |
| 183 | country = language.split("-")[1]; |
| 184 | } |
| 185 | |
| 186 | if (country != null) |
| 187 | locale = new Locale(lang, country); |
| 188 | else |
| 189 | locale = new Locale(lang); |
| 190 | } |
| 191 | |
| 192 | return locale; |
| 193 | } |
| 194 | } |