OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 90 | Rev 177 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 90 Rev 156
Line 19... Line 19...
19
import org.openconcerto.utils.Tuple2;
19
import org.openconcerto.utils.Tuple2;
20
 
20
 
21
import java.beans.Introspector;
21
import java.beans.Introspector;
22
import java.io.IOException;
22
import java.io.IOException;
23
import java.util.ArrayList;
23
import java.util.ArrayList;
-
 
24
import java.util.Arrays;
24
import java.util.Collections;
25
import java.util.Collections;
25
import java.util.HashMap;
26
import java.util.HashMap;
26
import java.util.LinkedHashMap;
27
import java.util.LinkedHashMap;
27
import java.util.List;
28
import java.util.List;
28
import java.util.Locale;
29
import java.util.Locale;
29
import java.util.Map;
30
import java.util.Map;
30
import java.util.MissingResourceException;
31
import java.util.MissingResourceException;
-
 
32
import java.util.Objects;
31
import java.util.Properties;
33
import java.util.Properties;
32
import java.util.regex.Matcher;
34
import java.util.regex.Matcher;
33
import java.util.regex.Pattern;
35
import java.util.regex.Pattern;
34
 
36
 
35
import com.ibm.icu.text.MessageFormat;
37
import com.ibm.icu.text.MessageFormat;
Line 47... Line 49...
47
 * @see LocalizedInstances
49
 * @see LocalizedInstances
48
 */
50
 */
49
public class TM {
51
public class TM {
50
 
52
 
51
    static public enum MissingMode {
53
    static public enum MissingMode {
52
        EXCEPTION, NULL, STRING
54
        EXCEPTION {
-
 
55
            @Override
-
 
56
            protected String returnMissing(TM tm, String key) throws MissingResourceException {
-
 
57
                throw new MissingResourceException("Missing translation", tm.getBaseName(), key);
-
 
58
            }
-
 
59
        },
-
 
60
        NULL {
-
 
61
            @Override
-
 
62
            protected String returnMissing(TM tm, String key) {
-
 
63
                return null;
-
 
64
            }
-
 
65
        },
-
 
66
        STRING {
-
 
67
            @Override
-
 
68
            protected String returnMissing(TM tm, String key) {
-
 
69
                return '!' + key + '!';
-
 
70
            }
-
 
71
        };
-
 
72
 
-
 
73
        protected abstract String returnMissing(final TM tm, final String key) throws MissingResourceException;
-
 
74
 
-
 
75
        // method to avoid array allocation and Arrays.toString()
-
 
76
        protected final String returnResult(final TM tm, final String res, final String key) throws MissingResourceException {
-
 
77
            return res == null ? this.returnMissing(tm, key) : res;
-
 
78
        }
-
 
79
 
-
 
80
        protected final String returnResult(final TM tm, final String res, final String... keys) throws MissingResourceException {
-
 
81
            return res == null ? this.returnMissing(tm, Arrays.toString(keys)) : res;
-
 
82
        }
53
    }
83
    }
54
 
84
 
55
    static public final String NOUN_CLASS_PROP = "nounClass";
85
    static public final String NOUN_CLASS_PROP = "nounClass";
56
    static {
86
    static {
57
        assert NOUN_CLASS_PROP.equals(Introspector.decapitalize(NounClass.class.getSimpleName()));
87
        assert NOUN_CLASS_PROP.equals(Introspector.decapitalize(NounClass.class.getSimpleName()));
Line 176... Line 206...
176
    public final String trM(final MissingMode mode, final String key, Map<String, ?> map) throws MissingResourceException {
206
    public final String trM(final MissingMode mode, final String key, Map<String, ?> map) throws MissingResourceException {
177
        return translate(mode, key, new MessageArgs(map));
207
        return translate(mode, key, new MessageArgs(map));
178
    }
208
    }
179
 
209
 
180
    public final String translate(final MissingMode mode, final String key, final Object... args) throws MissingResourceException {
210
    public final String translate(final MissingMode mode, final String key, final Object... args) throws MissingResourceException {
-
 
211
        return translate(mode, key, args.length == 0 ? MessageArgs.getEmpty() : new MessageArgs(args));
-
 
212
    }
-
 
213
 
-
 
214
    public final String translateFirst(final MissingMode mode, final String... keys) throws MissingResourceException {
181
        return translate(mode, key, new MessageArgs(args));
215
        return translateFirst(mode, MessageArgs.getEmpty(), keys);
-
 
216
    }
-
 
217
 
-
 
218
    /**
-
 
219
     * Return the first non-<code>null</code> result.
-
 
220
     * 
-
 
221
     * @param mode what to do if all keys are <code>null</code>.
-
 
222
     * @param args the arguments.
-
 
223
     * @param keys the keys to search for.
-
 
224
     * @return the first non-<code>null</code> result.
-
 
225
     * @throws MissingResourceException if {@link MissingMode#EXCEPTION} and all keys are
-
 
226
     *         <code>null</code>.
-
 
227
     */
-
 
228
    public final String translateFirst(final MissingMode mode, final MessageArgs args, final String... keys) throws MissingResourceException {
-
 
229
        String res = null;
-
 
230
        for (int i = 0; i < keys.length && res == null; i++) {
-
 
231
            final String key = keys[i];
-
 
232
            if (key != null)
-
 
233
                res = this.translate(MissingMode.NULL, key, args);
-
 
234
        }
-
 
235
        return mode.returnResult(this, res, keys);
182
    }
236
    }
183
 
237
 
184
    private final String translate(final MissingMode mode, final String key, MessageArgs args) throws MissingResourceException {
238
    private final String translate(final MissingMode mode, final String key, MessageArgs args) throws MissingResourceException {
-
 
239
        Objects.requireNonNull(mode, "Null mode");
-
 
240
        Objects.requireNonNull(key, "Null key");
-
 
241
        Objects.requireNonNull(args, "Null arguments");
185
        final String res = this.translations.translate(key, args);
242
        final String res = this.translations.translate(key, args);
186
        if (res == null) {
-
 
187
            if (mode == MissingMode.STRING)
-
 
188
                return '!' + key + '!';
243
        return mode.returnResult(this, res, key);
189
            else if (mode == MissingMode.NULL)
-
 
190
                return null;
-
 
191
            else
-
 
192
                throw new MissingResourceException("Missing translation", this.getBaseName(), key);
-
 
193
        }
-
 
194
        return res;
-
 
195
    }
244
    }
196
 
245
 
197
    protected MessageArgs replaceMap(final MessageArgs args, final String msg) {
246
    protected MessageArgs replaceMap(final MessageArgs args, final String msg) {
198
        final MessageArgs res;
247
        final MessageArgs res;
199
        if (args.isMapPrimary()) {
248
        if (args.isMapPrimary()) {