Returns noun to english Plural - Java Internationalization

Java examples for Internationalization:English

Description

Returns noun to english Plural

Demo Code

/*//from w ww.j  a  v  a2  s  . c o  m
 * Copyright 2009-2012 Michael Tamm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        long amount = 42;
        String noun = "java2s.com";
        System.out.println(amountString(amount, noun));
    }

    /**
     * Returns <code>"1 " + noun</code>, if <code>amount == 1</code>,
     * or <code>amount + " " + {@link #englishPluralOf englishPluralOf}(noun)</code> otherwise.
     */
    public static String amountString(long amount, String noun) {
        return (amount == 1 ? "1 " + noun : amount + " "
                + englishPluralOf(noun));
    }

    public static String englishPluralOf(String noun) {
        final String result;
        if (noun.endsWith("is")) {
            // E.g. "thesis" => "theses"
            result = noun.substring(0, noun.length() - 2) + "es";
        } else if (noun.endsWith("fe")) {
            // E.g. "knife" => "knifes"
            result = noun.substring(0, noun.length() - 2) + "ves";
        } else if (noun.endsWith("ay") || noun.endsWith("ey")
                || noun.endsWith("iy") || noun.endsWith("oy")
                || noun.endsWith("uy")) {
            // E.g. "key" => "keys"
            result = noun + "s";
        } else if (noun.endsWith("ch") || noun.endsWith("s")
                || noun.endsWith("sh") || noun.endsWith("x")
                || noun.endsWith("z")) {
            // E.g. "box" => "boxes"
            result = noun + "es";
        } else if (noun.endsWith("y")) {
            // E.g. "entry" => "entries"
            result = noun.substring(0, noun.length() - 1) + "ies";
        } else if (noun.endsWith("f")) {
            // E.g. "self" => "selves"
            result = noun.substring(0, noun.length() - 1) + "ves";
        } else {
            result = noun + "s";
        }
        return result;
    }
}

Related Tutorials