A support class to pluralise English words in Magento

我写这个类更多地是为了补习一下英文。顺便说一下,我会逐渐把更多的 support classes 归于 Msdk module,意思是 Magento SDK.

An ideal way to learn IT and English as a foreign language at a time.


<?php
class Qian_Mdsk_Helper_Plural extends Mage_Core_Helper_Abstract
{
/**
* auto is a wrapper. it calls another method by language settings
* @param int $nr
* @param string $singularWord
* @return string
*/
public static function auto($nr, $singularWord) {
//if english
return self::english($nr, $singularWord, 's');
}

public static function english($nr, $singularWord, $pluralSuffix = 's') {
if (abs($nr) == 1) {
return "$nr $singularWord";
}

$no = Mage::helper('corex')->__("No");
//If a word has more than one form of plural, and if the commonly used form is regular, the word will not be included in this dictionary.
//e.g. roof => roofs (irregular) or rooves (regular)
$dictionary = array(
//sh rule exception
'fish' => 'fish',

//o rule exception
'canto' => 'cantos',
'homo' => 'homos',
'piano' => 'pianos',
'portico' => 'porticos',
'pro' => 'pros',
'quarto' => 'quartos',
'kimono' => 'kimonos',
'zoo' => 'zoos',
'igloo' => 'igloos',
'kangaroo' => 'kangaroo',
'kniazhestvo' => 'kniazhestva',

//x rule exception
'matrix' => 'matrices',
'vertex' => 'vertices',

//s rule exception
'alumnus' => 'alumni',
'corpus' => 'corpora',
'focus' => 'foci',
'genus' => 'genera',
'prospectus' => 'prospecti',
'radius' => 'radii',
'syllabus' => 'syllabi',
'viscus' => 'viscera',
'fungus' => 'fungi',
'terminus' => 'termini',
'uterus' => 'uteri',
'Atlas' => 'Atlantes',

'species' => 'species',
'series' => 'series',
'blues' => 'blues',
'axis' => 'axes',
'testis' => 'testes',

//y rule exception
'Germany' => 'Germanys',
'Harry' => 'Harrys',

//f rule exception
'staff' => 'staff',
'flagstaff' => 'flagstaffs',
'proof' => 'proofs',

//other exception
//singular and plural are identical
'people' => 'people',
'deer' => 'deer',
'moose' => 'moose',
'sheep' => 'sheep',
'bison' => 'bison',
'salmon' => 'salmon',
'pike' => 'pike',
'trout' => 'trout',
'swine' => 'swine',
'aircraft' => 'aircraft',
'head' => 'head',
'stone' => 'stone',
'benshi' => 'benshi',
'otaku' => 'otaku',
'samurai' => 'samurai',
'Māori' => 'Māori',
'marae' => 'marae',
'waka' => 'waka',

//very irregular exception
'child' => 'children',

'alumna' => 'alumnae',

'mouse' => 'mice',
'louse' => 'lice',

'tooth' => 'teeth',
'foot' => 'feet',
'goose' => 'geese',

'automaton' => 'automata',
'criterion' => 'criteria',
'phenomenon' => 'phenomena',
'polyhedron' => 'polyhedra',

'addendum' => 'addenda',
'agendum' => 'agenda',
'consortium' => 'consortia',
'corrigendum' => 'corrigenda',
'datum' => 'data',
'medium' => 'media',
'memorandum' => 'memoranda',
'millennium' => 'millennia',
'symposium' => 'symposia',

'stigma' => 'stigmata',
'stoma' => 'stomata',
'schema' => 'schemata',
'dogma' => 'dogmata',
'lemma' => 'lemmata',

'beau' => 'beaux',
'château' => 'châteaux',
'tableau' => 'tableaux',

'Inuk' => 'Inuit',
'inukshuk' => 'inukshuit',

'phalanx' => 'phalanges',

);

if (isset($dictionary[$singularWord])) {
$pluralWord = $dictionary[$singularWord];
}
else {//some clever conversion
$end = substr($singularWord, -3);
if ($end == 'man') { //checking last 3 characters
$pluralWord = substr($singularWord, 0, -3) . 'men';
}
elseif ($end == 'sis') {
$pluralWord = substr($singularWord, 0, -3) . 'ses';
}
else {
$end = substr($singularWord, -2);
if ($end == 'ch' || $end == 'sh') {
$pluralWord = $singularWord . 'es';
}
elseif ($end == 'fe') {
$pluralWord = substr($singularWord, 0, -2) . 'ves';  //e.g. knife => knives, wife => wives
}
else {
$end = substr($singularWord, -1);
if (strpos('sxo', $end) === false) { //checking last character
$pluralWord = $singularWord . 'es';
}
elseif ($end == 'f') {
$pluralWord = substr($singularWord, 0, -1) . 'ves'; //e.g. half => halves
}
elseif ($end == 'y') {
$secondLast = substr($singularWord, -2, 1);
if (strpos('aeiou', $secondLast) === false) {
$pluralWord = substr($singularWord, 0, -1) . 'ies'; //e.g. lady => ladies
}
else {
$pluralWord = $singularWord . $pluralSuffix; //e.g. boy => boys
}
}
else { //last rule
$pluralWord = $singularWord . $pluralSuffix;
}
}
}

}

if ($nr == 0) {
return "$no $pluralWord";
}
else {
return "$nr $pluralWord";
}
}

}

Leave a comment

Your email address will not be published. Required fields are marked *