OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Compare Revisions

Regard whitespace Rev 155 → Rev 156

/trunk/OpenConcerto/src/org/openconcerto/utils/i18n/TM.java
21,6 → 21,7
import java.beans.Introspector;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
28,6 → 29,7
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.Objects;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
49,9 → 51,37
public class TM {
 
static public enum MissingMode {
EXCEPTION, NULL, STRING
EXCEPTION {
@Override
protected String returnMissing(TM tm, String key) throws MissingResourceException {
throw new MissingResourceException("Missing translation", tm.getBaseName(), key);
}
},
NULL {
@Override
protected String returnMissing(TM tm, String key) {
return null;
}
},
STRING {
@Override
protected String returnMissing(TM tm, String key) {
return '!' + key + '!';
}
};
 
protected abstract String returnMissing(final TM tm, final String key) throws MissingResourceException;
 
// method to avoid array allocation and Arrays.toString()
protected final String returnResult(final TM tm, final String res, final String key) throws MissingResourceException {
return res == null ? this.returnMissing(tm, key) : res;
}
 
protected final String returnResult(final TM tm, final String res, final String... keys) throws MissingResourceException {
return res == null ? this.returnMissing(tm, Arrays.toString(keys)) : res;
}
}
 
static public final String NOUN_CLASS_PROP = "nounClass";
static {
assert NOUN_CLASS_PROP.equals(Introspector.decapitalize(NounClass.class.getSimpleName()));
178,21 → 208,40
}
 
public final String translate(final MissingMode mode, final String key, final Object... args) throws MissingResourceException {
return translate(mode, key, new MessageArgs(args));
return translate(mode, key, args.length == 0 ? MessageArgs.getEmpty() : new MessageArgs(args));
}
 
public final String translateFirst(final MissingMode mode, final String... keys) throws MissingResourceException {
return translateFirst(mode, MessageArgs.getEmpty(), keys);
}
 
/**
* Return the first non-<code>null</code> result.
*
* @param mode what to do if all keys are <code>null</code>.
* @param args the arguments.
* @param keys the keys to search for.
* @return the first non-<code>null</code> result.
* @throws MissingResourceException if {@link MissingMode#EXCEPTION} and all keys are
* <code>null</code>.
*/
public final String translateFirst(final MissingMode mode, final MessageArgs args, final String... keys) throws MissingResourceException {
String res = null;
for (int i = 0; i < keys.length && res == null; i++) {
final String key = keys[i];
if (key != null)
res = this.translate(MissingMode.NULL, key, args);
}
return mode.returnResult(this, res, keys);
}
 
private final String translate(final MissingMode mode, final String key, MessageArgs args) throws MissingResourceException {
Objects.requireNonNull(mode, "Null mode");
Objects.requireNonNull(key, "Null key");
Objects.requireNonNull(args, "Null arguments");
final String res = this.translations.translate(key, args);
if (res == null) {
if (mode == MissingMode.STRING)
return '!' + key + '!';
else if (mode == MissingMode.NULL)
return null;
else
throw new MissingResourceException("Missing translation", this.getBaseName(), key);
return mode.returnResult(this, res, key);
}
return res;
}
 
protected MessageArgs replaceMap(final MessageArgs args, final String msg) {
final MessageArgs res;