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 java.util.Arrays;
import java.util.Collection;
import java.util.Locale;

import net.jcip.annotations.Immutable;

@Immutable
public class Grammar_es extends Grammar {

    static private final Grammar_es INSTANCE = new Grammar_es();

    public static Grammar_es getInstance() {
        return INSTANCE;
    }

    private Grammar_es() {
        this(Locale.forLanguageTag("es"));
    }

    protected Grammar_es(final Locale l) {
        super(l);
    }

    @Override
    protected Collection<? extends VariantKey> createVariantKeys() {
        return Arrays.asList(SINGULAR, PLURAL, INDEFINITE_ARTICLE_SINGULAR, INDEFINITE_ARTICLE_PLURAL, DEFINITE_ARTICLE_SINGULAR, DEFINITE_ARTICLE_PLURAL, DEMONSTRATIVE_SINGULAR, DEMONSTRATIVE_PLURAL,
                INDEFINITE_NUMERAL, DEFINITE_NUMERAL, DEMONSTRATIVE_NUMERAL, INDEFINITE_ORDINAL, DEFINITE_ORDINAL);
    }

    @Override
    protected Collection<? extends NounClass> createNounClasses() {
        return Arrays.asList(NounClass.FEMININE, NounClass.MASCULINE);
    }

    public final Phrase createPhrase(final String singular) {
        return this.createPhrase(singular, null);
    }

    public final Phrase createPhrase(final String singular, final String plural) {
        final Phrase res = new Phrase(this, singular, null);
        if (plural != null)
            res.putVariant(PLURAL, plural);
        return res;
    }

    @Override
    public String getVariant(Phrase noun, VariantKey key) {
        final String res;
        if (key.equals(SINGULAR)) {
            res = noun.getBase();
        } else if (key.equals(INDEFINITE_ARTICLE_SINGULAR)) {
            res = (noun.getNounClass() == NounClass.FEMININE ? "una " : "un ") + getSingular(noun);
        } else if (key.equals(DEFINITE_ARTICLE_SINGULAR)) {
            res = getDefiniteArticle(noun) + getSingular(noun);
        } else if (key.equals(DEMONSTRATIVE_SINGULAR)) {
            res = (noun.getNounClass() == NounClass.FEMININE ? "esta " : "este ") + getSingular(noun);
        } else if (key.equals(PLURAL)) {
            res = getPlural(noun.getBase());
        } else if (key.equals(INDEFINITE_ARTICLE_PLURAL)) {
            res = (noun.getNounClass() == NounClass.FEMININE ? "unas " : "unos ") + getPlural(noun);
        } else if (key.equals(DEFINITE_ARTICLE_PLURAL)) {
            res = (noun.getNounClass() == NounClass.FEMININE ? "las " : "los ") + getPlural(noun);
        } else if (key.equals(DEMONSTRATIVE_PLURAL)) {
            res = (noun.getNounClass() == NounClass.FEMININE ? "estas " : "estos ") + getPlural(noun);
        } else if (key.equals(INDEFINITE_NUMERAL)) {
            res = "{0, plural, =0 {" + getZero(noun) + "} one {# " + getSingular(noun) + "} other {# " + getPlural(noun) + "}}";
        } else if (key.equals(DEFINITE_NUMERAL)) {
            res = "{0, plural, =0 {" + getZero(noun) + "} one {" + getVariant(noun, DEFINITE_ARTICLE_SINGULAR) + "} other {" + (noun.getNounClass() == NounClass.FEMININE ? "las " : "los ") + " # "
                    + getPlural(noun) + "}}";
        } else if (key.equals(DEMONSTRATIVE_NUMERAL)) {
            res = "{0, plural, =0 {" + getZero(noun) + "} one {" + getVariant(noun, DEMONSTRATIVE_SINGULAR) + "} other {" + (noun.getNounClass() == NounClass.FEMININE ? "estas " : "estos ") + " # "
                    + getPlural(noun) + "}}";
        } else if (key.equals(DEFINITE_ORDINAL) || key.equals(INDEFINITE_ORDINAL)) {
            final boolean estFéminin = noun.getNounClass() == NounClass.FEMININE;
            final String article = key.equals(DEFINITE_ORDINAL) ? (estFéminin ? "la " : "el ") : "";
            res = article + "{0, ordinal, %digits-ordinal" + (estFéminin ? "-feminine" : "") + "} " + getSingular(noun);
        } else {
            res = null;
        }

        return res;
    }

    protected String getZero(Phrase noun) {
        return (noun.getNounClass() == NounClass.MASCULINE ? "ningún " : "ninguna ") + noun.getBase();
    }

    protected String getDefiniteArticle(Phrase noun) {
        if (noun.getNounClass() == NounClass.MASCULINE || noun.getBase().startsWith("a") || noun.getBase().startsWith("ha"))
            return "el ";
        else
            return "la ";
    }

    protected String getSingular(Phrase noun) {
        final String res = noun.getVariant(SINGULAR);
        return res == null ? noun.getBase() : res;
    }

    protected String getPlural(Phrase noun) {
        final String res = noun.getVariant(PLURAL);
        return res == null ? getPlural(noun.getBase()) : res;
    }

    protected String getPlural(String noun) {
        return noun + 's';
    }
}