Java String Pluralize PluralTemplateHelper(int count, String singularTemplate, String pluralTemplate)

Here you can find the source of PluralTemplateHelper(int count, String singularTemplate, String pluralTemplate)

Description

For even more complex sentences, it may just be easiest to provide a template, which will be replaced, if the count is singular or plural.

License

Open Source License

Parameter

Parameter Description
count The count of items
singularTemplate The singular template
pluralTemplate The plural template

Declaration

public static String PluralTemplateHelper(int count, String singularTemplate, String pluralTemplate) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from w ww  . j  a  v  a 2 s.c  om
     * For even more complex sentences, it may just be easiest to provide a
     * template, which will be replaced, if the count is singular or plural.
     * Both singularTemplate and pluralTemplate are expected to be String.format
     * templates with a %d in them, which will be replaced with the actual count
     * number. If the count == 1, then the singularTemplate will be used, else
     * the pluralTemplate will be used. Usage example:
     *
     * <pre>
     * String message = PluralTemplateHelper(count, "I will buy %d car if it has a good price",
     * "I will buy %d cars if they have a good price");
     * </pre>
     *
     * @param count The count of items
     * @param singularTemplate The singular template
     * @param pluralTemplate The plural template
     * @return
     */
    public static String PluralTemplateHelper(int count, String singularTemplate, String pluralTemplate) {
        if (count == 1) {
            return String.format(singularTemplate, count);
        } else {
            return String.format(pluralTemplate, count);
        }
    }
}

Related

  1. pluralize(String word)
  2. pluralize(String word, int count)
  3. pluralize(String x, Integer y)
  4. pluralizeName(String name)
  5. pluralName(final String name)