- Timestamp:
- 2011-09-01T13:42:54+02:00 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/I18n.java
r4376 r4394 9 9 import java.net.URL; 10 10 import java.text.MessageFormat; 11 import java.util.ArrayList; 11 12 import java.util.Arrays; 13 import java.util.Collection; 12 14 import java.util.Comparator; 13 15 import java.util.HashMap; … … 15 17 import java.util.zip.ZipEntry; 16 18 import java.util.Locale; 17 import java.util.Vector;18 19 19 20 import javax.swing.JColorChooser; … … 127 128 * These strings are collected by a script that runs on the source code files. 128 129 * After translation, the localizations are distributed with the main program. 130 * <br/> 131 * For example, {@code tr("JOSM''s default value is ''{0}''.", val)}. 132 * <br/> 133 * Use {@see #trn} for distinguishing singular from plural text, i.e., 134 * do not use {@code tr(size == 1 ? "singular" : "plural")} nor 135 * {@code size == 1 ? tr("singular") : tr("plural")} 129 136 * 130 137 * @param text the text to translate. … … 132 139 * Can be broken over multiple lines. 133 140 * An apostrophe ' must be quoted by another apostrophe. 134 * @param objects Parameters for the string. 135 * Mark occurrences in <code>text</code> with {0}, {1}, ... 136 * @return the translated string 137 * 138 * Example: tr("JOSM''s default value is ''{0}''.", val); 141 * @param objects the parameters for the string. 142 * Mark occurrences in {@code text} with {@code {0}}, {@code {1}}, ... 143 * @return the translated string. 144 * @see #trn 145 * @see #trc 146 * @see #trnc 139 147 */ 140 148 public static final String tr(String text, Object... objects) { … … 142 150 } 143 151 144 public static final String tr(String text) { 145 if (text == null) 146 return null; 147 return MessageFormat.format(gettext(text, null), (Object)null); 148 } 149 150 /** 151 * Provide translation in a context. 152 * There can be different translations for the same text (but 153 * different context). 152 /** 153 * Translates some text in a context for the current locale. 154 * There can be different translations for the same text within different contexts. 154 155 * 155 156 * @param context string that helps translators to find an appropriate 156 * translation for <code>text</code> 157 * @param text the text to translate 158 * @return the translated string 157 * translation for {@code text}. 158 * @param text the text to translate. 159 * @return the translated string. 160 * @see #tr 161 * @see #trn 162 * @see #trnc 159 163 */ 160 164 public static final String trc(String context, String text) { … … 178 182 * the translatable strings from the source files). 179 183 * 180 * Example:184 * For example, {@code 181 185 * String[] options = new String[] {marktr("up"), marktr("down")}; 182 * lbl.setText(tr(options[0])); 186 * lbl.setText(tr(options[0]));} 187 * @param text the string to be marked for translation. 188 * @return {@code text} unmodified. 183 189 */ 184 190 public static final String marktr(String text) { … … 191 197 192 198 /** 193 * Example: trn("Found {0} error in {1}!", "Found {0} errors in {1}!", i, Integer.toString(i), url); 194 */ 195 public static final String trn(String text, String pluralText, long n, Object... objects) { 196 return MessageFormat.format(gettextn(text, pluralText, null, n), objects); 197 } 198 199 /** 200 * Example: trn("There was an error!", "There were errors!", i); 201 */ 202 public static final String trn(String text, String pluralText, long n) { 203 return MessageFormat.format(gettextn(text, pluralText, null, n), (Object)null); 204 } 205 206 public static final String trnc(String ctx, String text, String pluralText, long n, Object... objects) { 207 return MessageFormat.format(gettextn(text, pluralText, ctx, n), objects); 208 } 209 210 public static final String trnc(String ctx, String text, String pluralText, long n) { 211 return MessageFormat.format(gettextn(text, pluralText, ctx, n), (Object)null); 212 } 213 214 private static final String gettext(String text, String ctx) 199 * Translates some text for the current locale and distinguishes between 200 * {@code singularText} and {@code pluralText} depending on {@code n}. 201 * <br/> 202 * For instance, {@code trn("There was an error!", "There were errors!", i)} or 203 * {@code trn("Found {0} error in {1}!", "Found {0} errors in {1}!", i, Integer.toString(i), url)}. 204 * 205 * @param singularText the singular text to translate. 206 * Must be a string literal. (No constants or local vars.) 207 * Can be broken over multiple lines. 208 * An apostrophe ' must be quoted by another apostrophe. 209 * @param pluralText the plural text to translate. 210 * Must be a string literal. (No constants or local vars.) 211 * Can be broken over multiple lines. 212 * An apostrophe ' must be quoted by another apostrophe. 213 * @param n a number to determine whether {@code singularText} or {@code pluralText} is used. 214 * @param objects the parameters for the string. 215 * Mark occurrences in {@code singularText} and {@code pluralText} with {@code {0}}, {@code {1}}, ... 216 * @return the translated string. 217 * @see #tr 218 * @see #trc 219 * @see #trnc 220 */ 221 public static final String trn(String singularText, String pluralText, long n, Object... objects) { 222 return MessageFormat.format(gettextn(singularText, pluralText, null, n), objects); 223 } 224 225 /** 226 * Translates some text in a context for the current locale and distinguishes between 227 * {@code singularText} and {@code pluralText} depending on {@code n}. 228 * There can be different translations for the same text within different contexts. 229 * 230 * @param context string that helps translators to find an appropriate 231 * translation for {@code text}. 232 * @param singularText the singular text to translate. 233 * Must be a string literal. (No constants or local vars.) 234 * Can be broken over multiple lines. 235 * An apostrophe ' must be quoted by another apostrophe. 236 * @param pluralText the plural text to translate. 237 * Must be a string literal. (No constants or local vars.) 238 * Can be broken over multiple lines. 239 * An apostrophe ' must be quoted by another apostrophe. 240 * @param n a number to determine whether {@code singularText} or {@code pluralText} is used. 241 * @param objects the parameters for the string. 242 * Mark occurrences in {@code singularText} and {@code pluralText} with {@code {0}}, {@code {1}}, ... 243 * @return the translated string. 244 * @see #tr 245 * @see #trc 246 * @see #trn 247 */ 248 public static final String trnc(String context, String singularText, String pluralText, long n, Object... objects) { 249 return MessageFormat.format(gettextn(singularText, pluralText, context, n), objects); 250 } 251 252 private static final String gettext(String text, String ctx, boolean lazy) 215 253 { 216 254 int i; … … 231 269 return trans[0]; 232 270 } 233 return text; 234 } 271 return lazy ? gettext(text, null) : text; 272 } 273 274 private static final String gettext(String text, String ctx) { 275 return gettext(text, ctx, false); 276 } 277 235 278 236 279 /* try without context, when context try fails */ 237 private static final String gettext_lazy(String text, String ctx) 238 { 239 int i; 240 if(ctx == null && text.startsWith("_:") && (i = text.indexOf("\n")) >= 0) 241 { 242 ctx = text.substring(2,i-1); 243 text = text.substring(i+1); 244 } 245 if(strings != null) 246 { 247 String trans = strings.get(ctx == null ? text : "_:"+ctx+"\n"+text); 248 if(trans != null) 249 return trans; 250 } 251 if(pstrings != null) { 252 String[] trans = pstrings.get(ctx == null ? text : "_:"+ctx+"\n"+text); 253 if(trans != null) 254 return trans[0]; 255 } 256 return gettext(text, null); 280 private static final String gettext_lazy(String text, String ctx) { 281 return gettext(text, ctx, true); 257 282 } 258 283 … … 281 306 */ 282 307 public static final Locale[] getAvailableTranslations() { 283 Vector<Locale> v = newVector<Locale>();308 Collection<Locale> v = new ArrayList<Locale>(languages.size()); 284 309 if(Main.class.getResource("/data/en.lang") != null) 285 310 {
Note:
See TracChangeset
for help on using the changeset viewer.