lib/TWiki/Plurals.pm
author Colas Nahaboo <colas@nahaboo.net>
Mon, 11 Aug 2008 20:33:37 +0200
changeset 2 7bc60a767fa4
parent 1 e2915a7cbdfa
permissions -rw-r--r--
TWiki-4.2.2 Release
     1 # TWiki Enterprise Collaboration Platform, http://TWiki.org/
     2 #
     3 # Copyright (C) 1999-2007 TWiki Contributors. All Rights Reserved.
     4 # TWiki Contributors are listed in the AUTHORS file in the root of
     5 # this distribution.
     6 # NOTE: Please extend that file, not this notice.
     7 #
     8 # Additional copyrights apply to some or all of the code in this
     9 # file as follows:
    10 # Copyright (C) 2005 Martin Cleaver.
    11 #
    12 # This program is free software; you can redistribute it and/or
    13 # modify it under the terms of the GNU General Public License
    14 # as published by the Free Software Foundation; either version 2
    15 # of the License, or (at your option) any later version. For
    16 # more details read LICENSE in the root of this distribution.
    17 #
    18 # This program is distributed in the hope that it will be useful,
    19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
    20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    21 #
    22 # As per the GPL, removal of this notice is prohibited.
    23 
    24 =pod
    25 
    26 ---+ package TWiki::Plurals
    27 
    28 Handle conversion of plural topic names to singular form.
    29 
    30 =cut
    31 
    32 package TWiki::Plurals;
    33 
    34 use strict;
    35 
    36 =pod
    37 
    38 ---++ StaticMethod singularForm($web, $pluralForm) -> $singularForm
    39 
    40 Try to singularise plural topic name.
    41    * =$web= - the web the topic must be in
    42    * =$pluralForm= - topic name
    43 Returns undef if no singular form exists, otherwise returns the
    44 singular form of the topic
    45 
    46 I18N - Only apply plural processing if site language is English, or
    47 if a built-in English-language web (Main, TWiki or Plugins).  Plurals
    48 apply to names ending in 's', where topic doesn't exist with plural
    49 name.
    50 
    51 Note that this is highly langauge specific, and need to be enabled
    52 on a per-installation basis with $TWiki::cfg{PluralToSingular}.
    53 
    54 =cut
    55 
    56 sub singularForm {
    57     my( $web, $pluralForm ) = @_;
    58     $web =~ s#\.#/#go;
    59 
    60     # Plural processing only if enabled in configure or one of the
    61     # distributed webs
    62     return undef unless( $TWiki::cfg{PluralToSingular} or 
    63                          $web eq $TWiki::cfg{UsersWebName} or 
    64                           $web eq $TWiki::cfg{SystemWebName} );
    65     return undef unless( $pluralForm =~ /s$/ );
    66 
    67     # Topic name is plural in form
    68     my $singularForm = $pluralForm;
    69     $singularForm =~ s/ies$/y/;      # plurals like policy / policies
    70     $singularForm =~ s/sses$/ss/;    # plurals like address / addresses
    71     $singularForm =~ s/ches$/ch/;    # plurals like search / searches
    72     $singularForm =~ s/(oes|os)$/o/; # plurals like veto / vetoes
    73     $singularForm =~ s/([Xx])es$/$1/;# plurals like box / boxes
    74     $singularForm =~ s/([^s])s$/$1/; # others, excluding ss like address(es)
    75     return $singularForm
    76 }
    77 
    78 1;