OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Blame | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.utils.i18n;

import org.openconcerto.utils.SystemUtils.PropertyList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.ibm.icu.text.MessageFormat;
import com.ibm.icu.text.RuleBasedNumberFormat;
import com.ibm.icu.util.ULocale;

/**
 * Prints names that can be passed to {@link RuleBasedNumberFormat#setDefaultRuleSet(String)},
 * {@link RuleBasedNumberFormat#format(long, String)} or {@link MessageFormat}. Can also be used to
 * test {@link MessageFormat}, e.g. "{0, ordinal, %digits-ordinal-feminine} femme" => "1re femme".
 * 
 * @author sylvain
 */
public class RuleBasedNumberFormatUtils {

    public static final String LOCALE_PROP_NAME = "locales";

    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("--list\tList rule names");
            System.out.println("--eval number message\tEvaluate the passed string using MessageFormat");
            System.out.println("Use " + LOCALE_PROP_NAME + " system property to define locales to use");
            System.exit(0);
        }
        final String action = args[0];
        if (action.equals("--list")) {
            for (final ULocale l : getLocales()) {
                System.out.println(I18nUtils.dumpAllRules(l));
                System.out.println();
            }
        } else if (action.equals("--eval")) {
            final Object[] params = new Object[] { Long.valueOf(args[1]) };
            final String message = args[2];
            for (final ULocale l : getLocales()) {
                System.out.println(l.getName() + ":");
                System.out.println(new MessageFormat(message, l).format(params));
                System.out.println();
            }
        } else {
            throw new IllegalArgumentException("Unknown action : " + action);
        }
    }

    private static List<ULocale> getLocales() {
        final List<ULocale> res;
        final List<String> pl = new PropertyList(LOCALE_PROP_NAME, ",").getValues();
        if (pl == null || pl.isEmpty()) {
            res = Collections.singletonList(ULocale.getDefault());
        } else {
            res = new ArrayList<>(pl.size());
            for (final String l : pl) {
                res.add(ULocale.forLanguageTag(l));
            }
        }
        return res;
    }
}