Class IntegerPluralizer
- java.lang.Object
-
- ca.cgjennings.i18n.IntegerPluralizer
-
public class IntegerPluralizer extends java.lang.ObjectA pluralizer for integer quantities. A pluralizer is used to select the correct localized plural form for a given quantity. For example, in English one writes "1 orange" but "2 oranges"; "1 ox" but "2 oxen". Depending on the locale, a word may have one plural form or many. The pluralizer understands the pluralization rules for the various locales that it supports, and it can supply the correct form automatically so long as you set up your localization files in the way that it expects.Although you can request a pluralizer for any locale by calling
create(java.util.Locale), you do not normally create a pluralizer yourself. Instead, you obtain a pluralizer from the appropriateLanguage. For example, to pluralize text in the user interface language, you would callLanguage.getInterface().getPluralizer()to get the pluralizer for the user interface language.The most common way to set up your
.propertiesfile when you want to add a pluralizable string is to add a key for the singular form, then add a separate key with "-pl" on the end for the plural. For example:elephant-count = I see %d elephant. elephant-count-pl = I see %d elephants.
For locales with more than one plural form, additional keys would be defined with the names
elephant-count-pl2,elephant-count-pl3, and so on. Locales that don't have plural forms would simply define the singular form key.Using a
Languageinstance that has this.propertiesfile loaded, you could obtain a properly formatted and localized plural by calling code similar to the following:language.getPluralizer().pluralize( numElephants, "elephant-count", numElephants );
(The secondnumElephantsargument is used to replace the%din the plural string.)- Author:
- Chris Jennings
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedIntegerPluralizer()Creates a new pluralizer instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static IntegerPluralizercreate()Create a pluralizer for the default locale.static IntegerPluralizercreate(java.util.Locale loc)Create a pluralizer for the requested locale.java.lang.StringcreateKey(int pluralForm, java.lang.String basePrefix, java.lang.String baseSuffix, java.lang.String singularInfix, java.lang.String pluralInfix)Returns a key name based on a plural form index and base key name parts.LanguagegetLanguage()Returns the default language instance that will be used by this pluralizer to look up plural form strings.intgetPluralForm(int number)Returns the plural form index to use fornumber.intgetPluralForm(long number)Returns the plural form index to use for anumber.intgetPluralFormCount()Returns the number of plural forms used by this pluralizer.java.lang.StringgetPluralFormDescription()Returns a brief description, in English, of the rule used by the pluralizer to select a plural form.booleanisFallbackPluralizer()If this returnstrue, then there is no built-in pluralizer for this pluralizer's locale and a default pluralizer is being used that follows the same rules as English.java.lang.Stringpluralize(int number, java.lang.String resourceKeyBase)Returns an appropriate plural form string based on an integer value.java.lang.Stringpluralize(int number, java.lang.String resourceKeyBase, java.lang.Object... formatObjects)Returns a pluralized string that has been formatted using the supplied objects.java.lang.Stringpluralize(Language language, int number, java.lang.String resourceKeyBase)Returns an appropriate plural form string based on an integer value.java.lang.Stringpluralize(Language language, int number, java.lang.String resourceKeyBase, java.lang.Object... formatObjects)Returns a pluralized string that has been formatted using the supplied objects.voidsetLanguage(Language language)Sets the language instance to use when looking up plural form strings.java.lang.StringtoString()
-
-
-
Method Detail
-
create
public static IntegerPluralizer create()
Create a pluralizer for the default locale. If no more specific pluralizer is available, a default pluralizer is returned that follows the same rules as English.- Returns:
- a pluralizer for the default locale
-
create
public static IntegerPluralizer create(java.util.Locale loc)
Create a pluralizer for the requested locale.The base class includes built-in support for a number of languages. If none of these is suitable, then it will attempt to instantiate a class in this package with the name
IntegerPluralizer_<i>xx</i>, where xx is the language code for the locale. If this class cannot be found and instantiated, then a default pluralizer is returned that follows the same rules as English.- Parameters:
loc- the locale, ornullto use the default locale- Returns:
- a pluralizer for the locale
-
getPluralForm
public int getPluralForm(int number)
Returns the plural form index to use fornumber. For example, the English pluralizer returns plural form 0 (singular) ifnumber == 1, and otherwise returns 1 (first plural form).Subclasses that implement support for specific locales must override this method.
- Parameters:
number- the number to choose a plural form for- Returns:
- the plural form index for the chosen number in this pluralizer's locale
- See Also:
createKey(int, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
-
getPluralForm
public int getPluralForm(long number)
Returns the plural form index to use for anumber. This is the long-sized analogue togetPluralForm(int).- Parameters:
number- the number to choose a plural form for- Returns:
- the plural form index for the chosen number in this pluralizer's locale
- See Also:
createKey(int, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
-
getPluralFormCount
public int getPluralFormCount()
Returns the number of plural forms used by this pluralizer. For example, an English pluralizer would return 2, a Japanese pluralizer would return 1 (since Japanese doesn't use different word forms based on quantity), and a Polish pluralizer would return 3.- Returns:
- the number of plural forms used by this pluralizer; this is one
more than the maximum value that may be returned by
getPluralForm(int).
-
getPluralFormDescription
public java.lang.String getPluralFormDescription()
Returns a brief description, in English, of the rule used by the pluralizer to select a plural form. Although the description is in English, it follows a specific format so that it can be readily parsed to format it for display. Each plural form is described on a separate line, in increasing order, and each line is divided by a colon. Before the colon comes a description of when the form applies, and after the colon comes an example key name for a key with the base name "key". For example:N is 1: key N ends in 2-4, except if it ends in 12-14: key-pl Everything else: key-pl2
- Returns:
- a brief description of the pluralization rule
-
createKey
public java.lang.String createKey(int pluralForm, java.lang.String basePrefix, java.lang.String baseSuffix, java.lang.String singularInfix, java.lang.String pluralInfix)Returns a key name based on a plural form index and base key name parts. The value ofpluralFormis 0 for the singular form, 1 for the first plural form, 2 for the second plural form, and so on. (The exact meaning of the plural form index is locale-dependent.) The returned key name is composed by following this pattern:basePrefix + singularInfix + baseSuffix (plural form 0) basePrefix + pluralInfix + baseSuffix (plural form 1) basePrefix + pluralInfix + "2" + baseSuffix (plural form 2) basePrefix + pluralInfix + "3" + baseSuffix (plural form 3) ...
- Parameters:
basePrefix- the prefix used for all keys (may be empty)baseSuffix- the suffix used for all keys (may be empty)singularInfix- the infix used if the plural index is for the singular formpluralInfix- the infix used for the first plural form, and as the basis of subsequent plural forms- Returns:
- a key composed for the specific plural form
-
pluralize
public java.lang.String pluralize(int number, java.lang.String resourceKeyBase)Returns an appropriate plural form string based on an integer value. This is a convenience method that is equivalent topluralize( getLanguage(), number, resourceKeyBase ).- Parameters:
number- the number to for which a plural form should be selectedresourceKeyBase- the base key to use to generate a key set- Returns:
- the appropriate plural form, or an error message
- See Also:
pluralize(resources.Language, int, java.lang.String)
-
pluralize
public java.lang.String pluralize(Language language, int number, java.lang.String resourceKeyBase)
Returns an appropriate plural form string based on an integer value. A set of keys is generated as if by callingcreateKeys( resourceKeyBase, "", "", "-pl" ). This means that the following keys will be used:
<i>resourceKeyBase</i>singular form
<i>resourceKeyBase</i>-plfirst plural form
<i>resourceKeyBase</i>-pl2second plural form
...Of these keys, the appropriate key will be selected based on the value of
number, and then the value of this key inlanguagereturned.- Parameters:
language- the language to use to look up the plural stringnumber- the number to for which a plural form should be selectedresourceKeyBase- the base key to use to generate a key set- Returns:
- the appropriate plural form, or an error message
- Throws:
java.lang.NullPointerException- if the language or base key isnull
-
pluralize
public java.lang.String pluralize(int number, java.lang.String resourceKeyBase, java.lang.Object... formatObjects)Returns a pluralized string that has been formatted using the supplied objects. This is a convenience for callingString.formaton the result of a call topluralize(int, java.lang.String).- Parameters:
number- the number to for which a plural form should be selectedresourceKeyBase- the base key to use to generate a key setformatObjects- the objects to use when formatting the resulting string- Returns:
- a localized, pluralized, and formatted string
- Throws:
java.lang.NullPointerException- if the base key isnull
-
pluralize
public java.lang.String pluralize(Language language, int number, java.lang.String resourceKeyBase, java.lang.Object... formatObjects)
Returns a pluralized string that has been formatted using the supplied objects. This is a convenience for callingString.formaton the result of a call topluralize(resources.Language, int, java.lang.String).- Parameters:
language- the language to use to look up the plural stringnumber- the number to for which a plural form should be selectedresourceKeyBase- the base key to use to generate a key setformatObjects- the objects to use when formatting the resulting string- Returns:
- a localized, pluralized, and formatted string
- Throws:
java.lang.NullPointerException- if the language or base key isnull
-
getLanguage
public Language getLanguage()
Returns the default language instance that will be used by this pluralizer to look up plural form strings.- Returns:
- the default language that this pluralizer will use to look up pluralized strings
-
setLanguage
public void setLanguage(Language language)
Sets the language instance to use when looking up plural form strings. (The default for new pluralizer instances isLanguage.getInterface().)- Parameters:
language- the new default language for this instance- Throws:
java.lang.NullPointerException- if the language isnull
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
isFallbackPluralizer
public final boolean isFallbackPluralizer()
If this returnstrue, then there is no built-in pluralizer for this pluralizer's locale and a default pluralizer is being used that follows the same rules as English. When starting a translation, you can test if pluralizer support needs to be added by running script code similar to the following (where ll is the relevant language code):println( ca.cgjennings.i18n.IntegerPluralizer.create( new java.util.Locale("ll") ).isFallbackPluralizer() );If this printstrue, then you should request that pluralization support be added for your language. Alternatively, if you are familiar with Java, you can add support yourself by writing a class with the nameIntegerPluralizer_<i>ll</i>. The class must be in the same package as this class, and it must subclassIntegerPluralizerand override thegetPluralForm(int),getPluralFormCount(), andgetPluralFormDescription()methods to implement the pluralization rule for the locale.- Returns:
trueif pluralizer support for your locale needs to be added
-
-