OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
177 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.utils.i18n;
15
 
16
import org.openconcerto.utils.SystemUtils.PropertyList;
17
 
18
import java.util.ArrayList;
19
import java.util.Collections;
20
import java.util.List;
21
 
22
import com.ibm.icu.text.MessageFormat;
23
import com.ibm.icu.text.RuleBasedNumberFormat;
24
import com.ibm.icu.util.ULocale;
25
 
26
/**
27
 * Prints names that can be passed to {@link RuleBasedNumberFormat#setDefaultRuleSet(String)},
28
 * {@link RuleBasedNumberFormat#format(long, String)} or {@link MessageFormat}. Can also be used to
29
 * test {@link MessageFormat}, e.g. "{0, ordinal, %digits-ordinal-feminine} femme" => "1re femme".
30
 *
31
 * @author sylvain
32
 */
33
public class RuleBasedNumberFormatUtils {
34
 
35
    public static final String LOCALE_PROP_NAME = "locales";
36
 
37
    public static void main(String[] args) {
38
        if (args.length == 0) {
39
            System.out.println("--list\tList rule names");
40
            System.out.println("--eval number message\tEvaluate the passed string using MessageFormat");
41
            System.out.println("Use " + LOCALE_PROP_NAME + " system property to define locales to use");
42
            System.exit(0);
43
        }
44
        final String action = args[0];
45
        if (action.equals("--list")) {
46
            for (final ULocale l : getLocales()) {
47
                System.out.println(I18nUtils.dumpAllRules(l));
48
                System.out.println();
49
            }
50
        } else if (action.equals("--eval")) {
51
            final Object[] params = new Object[] { Long.valueOf(args[1]) };
52
            final String message = args[2];
53
            for (final ULocale l : getLocales()) {
54
                System.out.println(l.getName() + ":");
55
                System.out.println(new MessageFormat(message, l).format(params));
56
                System.out.println();
57
            }
58
        } else {
59
            throw new IllegalArgumentException("Unknown action : " + action);
60
        }
61
    }
62
 
63
    private static List<ULocale> getLocales() {
64
        final List<ULocale> res;
65
        final List<String> pl = new PropertyList(LOCALE_PROP_NAME, ",").getValues();
66
        if (pl == null || pl.isEmpty()) {
67
            res = Collections.singletonList(ULocale.getDefault());
68
        } else {
69
            res = new ArrayList<>(pl.size());
70
            for (final String l : pl) {
71
                res.add(ULocale.forLanguageTag(l));
72
            }
73
        }
74
        return res;
75
    }
76
}